package com.storlead.knowledge.utils;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class UrlBuilder {

    public static String build(String url, Map<String, ?> queryParams) {
        if (queryParams == null || queryParams.isEmpty()) {
            return url;
        }

        StringBuilder sb = new StringBuilder(url);
        sb.append(url.contains("?") ? "&" : "?");

        queryParams.forEach((k, v) -> {
            sb.append(URLEncoder.encode(k, StandardCharsets.UTF_8));
            sb.append("=");
            sb.append(URLEncoder.encode(v.toString(), StandardCharsets.UTF_8));
            sb.append("&");
        });

        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}