'use strict';

var _enum = require('../components/_filters/filter-input/enum.js');

/* eslint-disable @typescript-eslint/no-explicit-any */
const clearFilterValue = '~clear~';
/**
 * Merges two query objects to produce a query string
 *
 * @param {Record<string, unknown>} currentQuery
 * @param {(Record<string, string | undefined>)} updates
 * @return {*}  {string}
 */
const mergeAndStringifyQuery = (currentQuery, updates, fields) => {
    const params = new URLSearchParams();
    Object.entries({ ...currentQuery, ...updates }).forEach(([key, value]) => {
        if (value == null || value === '')
            return;
        // Special handling for filters.*
        if (key.startsWith('filters.') && typeof value === 'string') {
            const fieldName = key.replace('filters.', '');
            const fieldDef = fields.find((f) => f.name === fieldName);
            // Only expand CSV for multiselect filters
            if (fieldDef?.filter?.type?.toLowerCase() === _enum.FilterType.multiselect.toLowerCase() && value.includes(',')) {
                value
                    .split(',')
                    .filter(Boolean)
                    .forEach((v) => params.append(key, v));
                return;
            }
        }
        // Arrays = repeated params
        if (Array.isArray(value)) {
            value.forEach((v) => params.append(key, String(v)));
            return;
        }
        // single param
        params.set(key, String(value));
    });
    return `?${params.toString()}`;
};
/**
 * Joins two query strings together
 *
 * @param {(...Array<string | undefined>)} parts
 * @return {*}
 */
const joinQueryStrings = (...parts) => {
    return parts
        .filter((p) => Boolean(p && p.length))
        .map((p) => p.replace(/^\?/, ''))
        .join('&');
};
const getDpdPathSuffix = (dpdsPath) => {
    if (dpdsPath && dpdsPath !== '') {
        return `?dataProductDefinitionsPath=${dpdsPath}`;
    }
    return '';
};
const setNestedPath = (url, baseUrl) => {
    return baseUrl && baseUrl !== 'undefined' ? `${baseUrl}${url}` : url;
};

exports.clearFilterValue = clearFilterValue;
exports.getDpdPathSuffix = getDpdPathSuffix;
exports.joinQueryStrings = joinQueryStrings;
exports.mergeAndStringifyQuery = mergeAndStringifyQuery;
exports.setNestedPath = setNestedPath;
//# sourceMappingURL=urlHelper.js.map
