package com.binance.connector.client.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/UrlBuilder.class */
public final class UrlBuilder {
    private static final int DIFF_TILL_POSITION_INDEX = 1;
    private static final int MAX_DECIMAL_DIGITS = 30;

    /* renamed from: df */
    private static DecimalFormat f0df;

    private UrlBuilder() {
    }

    public static String buildFullUrl(String baseUrl, String urlPath, Map<String, Object> parameters) {
        StringBuilder sb = new StringBuilder(baseUrl).append(urlPath);
        if (parameters != null && !parameters.isEmpty()) {
            sb.append("?");
            sb.append(joinQueryParameters(parameters));
        }
        return sb.toString();
    }

    public static String buildStreamUrl(String baseUrl, ArrayList<String> streams) {
        StringBuilder sb = new StringBuilder(baseUrl).append("/stream");
        if (streams != null && !streams.isEmpty()) {
            sb.append("?streams=");
            sb.append(joinStreamUrls(streams));
        }
        return sb.toString();
    }

    public static String joinQueryParameters(Map<String, Object> params) {
        return joinQueryParameters(new StringBuilder(), params).toString();
    }

    public static StringBuilder joinQueryParameters(StringBuilder sb, Map<String, Object> params) {
        String value;
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                sb.append(key);
                sb.append("=");
                if (params.get(key) instanceof Double) {
                    value = getFormatter().format(params.get(key));
                } else {
                    value = params.get(key).toString();
                }
                sb.append(urlEncode(value));
                sb.append("&");
            }
            sb.deleteCharAt(sb.length() - DIFF_TILL_POSITION_INDEX);
        }
        return sb;
    }

    public static String joinStreamUrls(ArrayList<String> streams) {
        return joinStreamUrls(new StringBuilder(), streams).toString();
    }

    public static StringBuilder joinStreamUrls(StringBuilder sb, ArrayList<String> streams) {
        if (streams != null && !streams.isEmpty()) {
            Iterator<String> it = streams.iterator();
            while (it.hasNext()) {
                sb.append(it.next());
                sb.append("/");
            }
            sb.deleteCharAt(sb.length() - DIFF_TILL_POSITION_INDEX);
        }
        return sb;
    }

    public static String urlEncode(String s) {
        try {
            return URLEncoder.encode(s, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(StandardCharsets.UTF_8.name() + " is unsupported", e);
        }
    }

    public static String buildTimestamp() {
        return String.valueOf(System.currentTimeMillis());
    }

    private static DecimalFormat getFormatter() {
        if (null == f0df) {
            f0df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(Locale.ENGLISH));
            f0df.setMaximumFractionDigits(MAX_DECIMAL_DIGITS);
            f0df.setGroupingUsed(false);
        }
        return f0df;
    }
}
