"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendQueryParams = void 0;
/** Append custom parameters for settings to provided URL object.
   *  Make sure custom parameters do no override query parameters already part of the URL (lower case comparison)
   * @internal
   */
function appendQueryParams(url, queryParams) {
    if (!queryParams)
        return url;
    // create a lower-case array of keys
    const currentParams = [];
    const currentUrl = new URL(url);
    currentUrl.searchParams.forEach((_value, key, _parent) => {
        currentParams.push(key.toLowerCase());
    });
    const urlParamsFromIndexArray = (indexArray, result) => {
        const urlParams = (result ? result : new URLSearchParams());
        if (!indexArray)
            return urlParams;
        Object.keys(indexArray).forEach((key) => {
            if (!currentParams.includes(key.toLowerCase()))
                urlParams.append(key, indexArray[key]);
        });
        return urlParams;
    };
    const params = urlParamsFromIndexArray(queryParams);
    const getSeparator = (u) => {
        let separator = "&";
        if (u.includes("?")) {
            if (u.endsWith("?"))
                separator = "";
        }
        else {
            separator = "?";
        }
        return separator;
    };
    if (params.size > 0) {
        url = `${url}${getSeparator(url)}${params.toString()}`;
    }
    return url;
}
exports.appendQueryParams = appendQueryParams;
//# sourceMappingURL=UrlUtils.js.map