import { FilterType } from '../filter-input/enum.js';
import DateRangeInputUtils from '../../_inputs/date-range/utils.js';
import { DateMapper } from '../../../utils/DateMapper/DateMapper.js';

/* eslint-disable no-param-reassign */
const setDateDisplayFormat = (dateValue) => {
    const dateMapper = new DateMapper();
    const type = dateMapper.getDateType(dateValue);
    if (type !== 'none') {
        dateValue = dateMapper.toDateString(dateValue, 'local-date') || dateValue;
    }
    return dateValue;
};
const setSelectedDateRange = (f, prefix) => {
    const startValue = f.value.start || 'Open start';
    const endValue = f.value.end || 'Open end';
    const value = [`"${startValue}"`, `"${endValue}"`];
    const name = `${prefix}${f.name}`;
    const key = [`${name}.start`, `${name}.end`];
    const displayStartValue = setDateDisplayFormat(startValue);
    const displayEndValue = setDateDisplayFormat(endValue);
    let displayValue = `${displayStartValue} - ${displayEndValue}`;
    const { relative } = f.value;
    if (relative) {
        key.push(`${name}.relative-duration`);
        value.push(`"${relative}"`);
        displayValue = DateRangeInputUtils.mapRelativeValue(relative);
    }
    const constraints = setMinMaxContraints(f, key);
    const { disabled, cantRemoveClass, displayValue: disabledDisplayValue } = disabledDateRange(f, value, displayValue);
    return {
        key,
        value,
        displayValue: disabledDisplayValue || displayValue,
        disabled,
        cantRemoveClass,
        constraints,
    };
};
const setMinMaxContraints = (f, key, singleDate = false) => {
    const constraints = [];
    const { min, max } = f;
    if (min) {
        constraints.push({
            key: key[0],
            value: min,
        });
    }
    if (max && !singleDate) {
        constraints.push({
            key: key[1],
            value: max,
        });
    }
    return constraints.length ? constraints : undefined;
};
const getOptionsValue = (f, prefix) => {
    const displayOption = f.options.find((opt) => opt.value === f.value);
    const displayValue = displayOption ? displayOption.text : '';
    const key = [`${prefix}${f.name}`];
    const value = [`"${f.value}"`];
    return {
        key,
        value,
        displayValue,
    };
};
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 : '';
    })
        .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 setSelectedGranularDateRange = (f, prefix) => {
    const startValue = f.value.start || 'Open start';
    const endValue = f.value.end || 'Open end';
    const quickFilterValue = f.value.quickFilter?.value;
    const quickFilterDisplay = f.value.quickFilter?.display;
    const granularityValue = f.value.granularity.value;
    const name = `${prefix}${f.name}`;
    const key = [`${name}.start`, `${name}.end`, `${name}.granularity`];
    const value = [`"${startValue}"`, `"${endValue}"`, `"${granularityValue}"`];
    const constraints = setMinMaxContraints(f, key);
    let displayValue;
    const granularityDisplay = f.value.granularity.display;
    if (quickFilterValue && quickFilterValue !== 'none' && quickFilterDisplay) {
        key.push(`${name}.quick-filter`);
        value.push(`"${quickFilterValue}"`);
        displayValue = `${quickFilterDisplay}: ${granularityDisplay}`;
    }
    else {
        const displayStartValue = setDateDisplayFormat(startValue);
        const displayEndValue = setDateDisplayFormat(endValue);
        displayValue = `${displayStartValue} - ${displayEndValue}: ${granularityDisplay}`;
    }
    const { disabled, cantRemoveClass, displayValue: disabledDisplayValue } = disabledDateRange(f, value, displayValue);
    return {
        key,
        value,
        displayValue: disabledDisplayValue || displayValue,
        disabled,
        constraints,
        cantRemoveClass,
    };
};
const disabledDateRange = (f, value, displayValue) => {
    const { min, max } = f;
    if (min && value[0].includes(min) && max && value[1].includes(max)) {
        return {
            disabled: true,
            cantRemoveClass: ' interactive-remove-filter-button--disabled',
            displayValue: `${displayValue} (maximum range)`,
        };
    }
    return {
        disabled: false,
        cantRemoveClass: '',
        displayValue,
    };
};
const getQuerySummary = (reqQuery, filters) => {
    const prefix = 'filters.';
    return filters
        .filter((f) => reqQuery[`filters.${f.name}`] || reqQuery[`filters.${f.name}.start`] || reqQuery[`filters.${f.name}.end`])
        .map((f) => {
        let { value } = f;
        if (f.type.toLowerCase() === FilterType.multiselect.toLowerCase()) {
            value = getMultiselectValues(f, 'filters').displayValue;
        }
        if (f.type.toLowerCase() === FilterType.autocomplete.toLowerCase() ||
            f.type.toLowerCase() === FilterType.radio.toLowerCase() ||
            f.type.toLowerCase() === FilterType.select.toLowerCase()) {
            value = getOptionsValue(f, prefix).displayValue;
        }
        if (f.type.toLowerCase() === FilterType.date.toLowerCase()) {
            value = setDateDisplayFormat(f.value);
        }
        if (f.type.toLowerCase() === FilterType.dateRange.toLowerCase()) {
            value = setSelectedDateRange(f, prefix).displayValue;
        }
        if (f.type.toLowerCase() === FilterType.granularDateRange.toLowerCase()) {
            value = setSelectedGranularDateRange(f, prefix).displayValue;
        }
        return {
            id: f.name,
            name: f.text,
            value: value,
        };
    });
};
var SelectedFiltersUtils = {
    getQuerySummary,
};

export { SelectedFiltersUtils as default, getQuerySummary };
//# sourceMappingURL=utils.js.map
