'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 appendMultiSelectValues = (params, fieldName, value) => {
    value
        .split(',')
        .map((v) => v.trim())
        .filter(Boolean)
        .forEach((val) => {
        params.append(`filters.${fieldName}`, val);
    });
};
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 = {
    setValueFromRequest,
    getMultiselectValues,
    setFilterValuesFromSavedDefault,
};

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