package com.iqser.red.service.persistence.management.v1.processor.utils;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class StringEncodingUtils {

    Character COMMA = ',';


    public static String urlEncode(String input) {

        if (input == null) {
            return null;
        }

        var result = new StringBuilder();
        for (var i = 0; i < input.length(); i++) {
            if (StringUtils.isAsciiPrintable(Character.toString(input.charAt(i))) && input.charAt(i) != COMMA) {
                result.append(input.charAt(i));
            } else {
                result.append(URLEncoder.encode(Character.toString(input.charAt(i)), StandardCharsets.UTF_8));
            }
        }
        return result.toString();
    }

}
