'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

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

/* eslint-disable @typescript-eslint/no-explicit-any */
const clearFilterValue = '~clear~';
const createUrlForParameters = (currentQueryParams, updateQueryParams, fields) => {
    let queryParams;
    if (updateQueryParams) {
        queryParams = {
            ...currentQueryParams,
            selectedPage: '1',
        };
        Object.keys(updateQueryParams).forEach((q) => {
            if (updateQueryParams[q]) {
                queryParams[q] = updateQueryParams[q];
            }
            else {
                Object.keys(queryParams)
                    .filter((key) => key === q || key.startsWith(`${q}.`))
                    .forEach((key) => {
                    queryParams[key] = clearFilterValue;
                });
            }
        });
    }
    else {
        queryParams = {
            selectedPage: '1',
            pageSize: currentQueryParams['pageSize'],
            sortColumn: currentQueryParams['sortColumn'],
            sortedAsc: currentQueryParams['sortedAsc'],
            dataProductDefinitionsPath: currentQueryParams['dataProductDefinitionsPath'],
        };
    }
    const nonEmptyQueryParams = {};
    Object.keys(queryParams)
        .filter((key) => queryParams[key])
        .forEach((key) => {
        nonEmptyQueryParams[key] = queryParams[key];
    });
    return createQuerystringFromObject(nonEmptyQueryParams, fields);
};
const createQuerystringFromObject = (source, fields) => {
    const querystring = Object.keys(source)
        .flatMap((key) => {
        const fieldDef = fields?.find((f) => `filters.${f.name}` === key);
        const value = source[key] || '';
        if (Array.isArray(value)) {
            return value.map((v) => `${encodeURI(key)}=${encodeURI(v)}`);
        }
        if (fieldDef && fieldDef.filter && fieldDef.filter.type.toLowerCase() === _enum.FilterType.multiselect.toLowerCase()) {
            const values = value.split(',');
            return values.map((v) => {
                return `${encodeURI(key)}=${encodeURI(v)}`;
            });
        }
        return [`${encodeURI(key)}=${encodeURI(value)}`];
    })
        .join('&');
    return `?${querystring}`;
};
const getDpdPathSuffix = (dpdsPath) => {
    if (dpdsPath && dpdsPath !== '') {
        return `?dataProductDefinitionsPath=${dpdsPath}`;
    }
    return '';
};
const setNestedPath = (url, baseUrl) => {
    return baseUrl && baseUrl !== 'undefined' ? `${baseUrl}${url}` : url;
};
/**
 * Normalizes a querystring parameter into a consistent string array.
 *
 * Needed because Express/qs may return repeated query params as strings, arrays,
 * or numeric‑keyed objects depending on parser settings and arrayLimit behavior
 *
 * @param value The raw query parameter value from req.query.
 * @returns An array of strings representing the normalized parameter.
 */
const normalizeQueryStringArray = (queryParamValue) => {
    if (queryParamValue == null)
        return undefined;
    if (Array.isArray(queryParamValue))
        return queryParamValue;
    if (typeof queryParamValue === 'object') {
        const obj = queryParamValue;
        const numericKeys = Object.keys(obj)
            .filter((k) => /^\d+$/.test(k))
            .sort((a, b) => Number(a) - Number(b));
        return numericKeys.length > 0 ? numericKeys.map((k) => obj[k]) : undefined;
    }
    return typeof queryParamValue === 'string' ? [queryParamValue] : undefined;
};

exports.clearFilterValue = clearFilterValue;
exports.createQuerystringFromObject = createQuerystringFromObject;
exports.createUrlForParameters = createUrlForParameters;
exports.default = createUrlForParameters;
exports.getDpdPathSuffix = getDpdPathSuffix;
exports.normalizeQueryStringArray = normalizeQueryStringArray;
exports.setNestedPath = setNestedPath;
//# sourceMappingURL=urlHelper.js.map
