'use strict';

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

const setValueFromRequest = (filter, req, prefix) => {
    const queryValue = req.query[`${prefix}${filter.name}`];
    let valueArr = [];
    let valueString = '';
    if (queryValue?.length) {
        valueArr = Array.isArray(queryValue) ? queryValue : [queryValue];
        valueString = valueArr.join(',');
    }
    return {
        requestfilterValue: valueString || null,
        requestfilterValues: valueArr,
    };
};
const getQueryFromDefinition = (filter, name, filterPrefix) => {
    const values = filter.defaultValue ? filter.defaultValue.split(',') : [];
    return values
        .map((val) => {
        return `${filterPrefix}${name}=${val}`;
    })
        .join('&');
};
const getMultiselectValues = (f, prefix) => {
    const MAX_VALUES = 3;
    const splitValues = f.value.split(',');
    let displayValue = splitValues
        .map((v) => {
        const displayOption = f.options?.find((opt) => opt.value === v);
        return displayOption ? displayOption.text : null;
    })
        .filter((_v, i) => {
        return i < MAX_VALUES;
    })
        .join(', ');
    displayValue =
        splitValues.length > MAX_VALUES ? `${displayValue} + ${splitValues.length - MAX_VALUES} more` : displayValue;
    const value = splitValues.map((v) => `"${v}"`);
    const key = [`${prefix}${f.name}`];
    return {
        key,
        value,
        displayValue,
    };
};
const setFilterValuesFromSavedDefault = (filter, hasDefaults, defaultValue) => {
    let value = hasDefaults ? '' : filter.value;
    value = defaultValue ? defaultValue.value : value;
    let values = hasDefaults ? [] : filter.values;
    values = defaultValue ? defaultValue.value.split(',') : values;
    return {
        ...filter,
        value,
        values,
    };
};
var MultiSelectUtils = {
    getQueryFromDefinition,
    setValueFromRequest,
    getMultiselectValues,
    setFilterValuesFromSavedDefault,
};

exports.default = MultiSelectUtils;
exports.getMultiselectValues = getMultiselectValues;
exports.getQueryFromDefinition = getQueryFromDefinition;
exports.setFilterValuesFromSavedDefault = setFilterValuesFromSavedDefault;
exports.setValueFromRequest = setValueFromRequest;
//# sourceMappingURL=utils.js.map
