'use strict';

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

var _enum = require('../filter-input/enum.js');
var utils = require('../../_inputs/date-range/utils.js');
var DateMapper = require('../../../utils/DateMapper/DateMapper.js');

/* eslint-disable no-param-reassign */
const getSelectedFilters = (filters, prefix) => {
    const emptyValues = [undefined, null, ''];
    return filters
        .filter((f) => {
        return ((f.value &&
            Object.prototype.hasOwnProperty.call(f.value, 'start') &&
            !emptyValues.includes(f.value.start)) ||
            (f.value &&
                Object.prototype.hasOwnProperty.call(f.value, 'end') &&
                !emptyValues.includes(f.value.end)) ||
            (f.value &&
                !Object.prototype.hasOwnProperty.call(f.value, 'start') &&
                !Object.prototype.hasOwnProperty.call(f.value, 'end')));
    })
        .filter((f) => {
        return f.value && f.value !== 'no-filter';
    })
        .map((f) => {
        let displayValue = `${f.value}`;
        let value = [`"${displayValue}"`];
        let key = [`${prefix}${f.name}`];
        let cantRemoveClass = '';
        let disabled = false;
        let constraints;
        if (f.type.toLowerCase() === _enum.FilterType.dateRange.toLowerCase()) {
            ({ key, value, disabled, cantRemoveClass, displayValue, constraints } = setSelectedDateRange(f, prefix));
        }
        if (f.type.toLowerCase() === _enum.FilterType.granularDateRange.toLowerCase()) {
            ({ key, value, disabled, cantRemoveClass, displayValue, constraints } = setSelectedGranularDateRange(f, prefix));
        }
        if (f.type.toLowerCase() === _enum.FilterType.date.toLowerCase()) {
            ({ key, value, disabled, cantRemoveClass, displayValue, constraints } = getSelectedDate(f, prefix));
        }
        if (f.type.toLowerCase() === _enum.FilterType.autocomplete.toLowerCase() ||
            f.type.toLowerCase() === _enum.FilterType.radio.toLowerCase() ||
            f.type.toLowerCase() === _enum.FilterType.select.toLowerCase()) {
            ({ key, value, displayValue } = getOptionsValue(f, prefix));
        }
        if (f.type.toLowerCase() === _enum.FilterType.multiselect.toLowerCase()) {
            ({ key, value, displayValue } = getMultiselectValues(f, prefix));
        }
        let ariaLabel;
        if (disabled) {
            ariaLabel = `Selected Filter: ${f.text}: ${displayValue}. This filter cant be removed. Update the filter input to change the value`;
        }
        else {
            ariaLabel = `Selected Filter: ${f.text}: ${displayValue}. Click to clear this filter`;
        }
        return {
            text: f.text,
            displayValue,
            key: JSON.stringify(key),
            value,
            disabled,
            constraints,
            classes: `interactive-remove-filter-button${cantRemoveClass}`,
            attributes: {
                'aria-label': ariaLabel,
            },
        };
    });
};
const setDateDisplayFormat = (dateValue) => {
    const dateMapper = new DateMapper.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 = utils.default.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 getSelectedDate = (f, prefix) => {
    const key = [`${prefix}${f.name}`];
    const value = [`"${f.value}"`];
    const displayValue = setDateDisplayFormat(f.value);
    const constraints = setMinMaxContraints(f, key, true);
    const { disabled, cantRemoveClass, displayValue: disabledDisplayValue } = disabledDate(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 disabledDate = (f, value, displayValue) => {
    const { min, max } = f;
    if ((min && value[0].includes(min)) || (max && value[0].includes(max))) {
        let valueType;
        if (min && value[0].includes(min))
            valueType = 'min';
        if (max && value[0].includes(max))
            valueType = 'max';
        return {
            disabled: true,
            cantRemoveClass: ' interactive-remove-filter-button--disabled',
            displayValue: `${displayValue} (${valueType} date)`,
        };
    }
    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() === _enum.FilterType.multiselect.toLowerCase()) {
            value = getMultiselectValues(f, 'filters').displayValue;
        }
        if (f.type.toLowerCase() === _enum.FilterType.autocomplete.toLowerCase() ||
            f.type.toLowerCase() === _enum.FilterType.radio.toLowerCase() ||
            f.type.toLowerCase() === _enum.FilterType.select.toLowerCase()) {
            value = getOptionsValue(f, prefix).displayValue;
        }
        if (f.type.toLowerCase() === _enum.FilterType.date.toLowerCase()) {
            value = setDateDisplayFormat(f.value);
        }
        if (f.type.toLowerCase() === _enum.FilterType.dateRange.toLowerCase()) {
            value = setSelectedDateRange(f, prefix).displayValue;
        }
        if (f.type.toLowerCase() === _enum.FilterType.granularDateRange.toLowerCase()) {
            value = setSelectedGranularDateRange(f, prefix).displayValue;
        }
        return {
            id: f.name,
            name: f.text,
            value: value,
        };
    });
};
var SelectedFiltersUtils = {
    getSelectedFilters,
    getQuerySummary,
};

exports.default = SelectedFiltersUtils;
exports.disabledDate = disabledDate;
exports.disabledDateRange = disabledDateRange;
exports.getMultiselectValues = getMultiselectValues;
exports.getOptionsValue = getOptionsValue;
exports.getQuerySummary = getQuerySummary;
exports.getSelectedDate = getSelectedDate;
exports.getSelectedFilters = getSelectedFilters;
exports.setDateDisplayFormat = setDateDisplayFormat;
exports.setMinMaxContraints = setMinMaxContraints;
exports.setSelectedDateRange = setSelectedDateRange;
exports.setSelectedGranularDateRange = setSelectedGranularDateRange;
//# sourceMappingURL=utils.js.map
