import SortHelper from './sortByTemplate.js';
import DefinitionUtils from '../../../utils/definitionUtils.js';
import { DateMapper } from '../../../utils/DateMapper/DateMapper.js';
import FiltersUtils from '../../_filters/utils.js';
import DateRangeInputUtils from '../../_inputs/date-range/utils.js';
import AutocompleteUtils from '../../_inputs/autocomplete-text-input/utils.js';
import { FilterType } from '../../_filters/filter-input/enum.js';

/**
 * Initialises the sortData from the definition
 */
const getSortByFromDefinition = (fields) => {
    const sortBy = SortHelper.sortByTemplate();
    const options = fields
        .filter((f) => f.sortable)
        .map((f) => {
        if (f.defaultsort)
            sortBy[0].value = f.name;
        const field = { value: f.name, text: f.display };
        sortBy[1].value = f.sortDirection ? `${f.sortDirection === 'asc'}` : 'true';
        return field;
    });
    if (options.length) {
        const sortWithOptions = sortBy[0];
        sortWithOptions.options = options;
        sortBy[0] = sortWithOptions;
        return sortBy;
    }
    return [];
};
const setDurationStartAndEnd = (name, value, query, filterData, querySummary, fields) => {
    const { startDate, endDate } = DateRangeInputUtils.calcDates(value);
    const startDateDisplayString = startDate ? startDate.format('YYYY-MM-DD').toString() : '';
    const endDateDisplayString = endDate ? endDate.format('YYYY-MM-DD').toString() : '';
    const fieldId = name.split('.')[1];
    const field = fields.find((f) => {
        return f.name === fieldId;
    });
    query[`filters.${fieldId}.start`] = startDateDisplayString;
    query[`filters.${fieldId}.end`] = endDateDisplayString;
    filterData[name] = value;
    let queryValue = `${value.charAt(0).toUpperCase() + value.slice(1).replaceAll('-', ' ')}`;
    queryValue = `${queryValue} (${startDateDisplayString} - ${endDateDisplayString})`;
    querySummary.push({
        name: field ? `${field.display}` : name,
        value: queryValue,
    });
    return {
        querySummary,
        filterData,
        query,
    };
};
/**
 * Returns the data required for rendering the async filters component
 *
 * @param {AsyncReportUtilsParams} { req, res, dataSources }
 * @return {*}
 */
const renderFilters = async (fields) => {
    return {
        filters: FiltersUtils.getFiltersFromDefinition(fields, false),
        sortBy: getSortByFromDefinition(fields),
    };
};
const setQueryFromFilters = (req, fields) => {
    let query = {};
    let filterData = {};
    let querySummary = [];
    const sortData = {};
    const dateMapper = new DateMapper();
    const urlParams = new URLSearchParams(req.body.search);
    Object.keys(req.body)
        .filter((name) => name !== '_csrf' && req.body[name] !== '')
        .forEach((name) => {
        const shortName = name.replace('filters.', '');
        const queryValue = req.body[name];
        let summaryValue = queryValue;
        if (name.startsWith('filters.') && queryValue !== '' && !query[name] && queryValue !== 'no-filter') {
            if (name.includes('relative-duration')) {
                ({ query, filterData, querySummary } = setDurationStartAndEnd(name, queryValue, query, filterData, querySummary, fields));
            }
            else {
                const fieldId = name.split('.')[1];
                const filter = fields.find((f) => f.name === fieldId)?.filter;
                let urlParamValue = urlParams.getAll(name);
                urlParamValue = !urlParamValue || urlParamValue.length === 0 ? queryValue : urlParamValue;
                urlParamValue = urlParamValue.length === 1 ? `${urlParamValue[0]}` : `${urlParamValue}`;
                let dateDisplayValue;
                if (dateMapper.isDate(queryValue)) {
                    dateDisplayValue = dateMapper.toDateString(queryValue, 'local-date');
                    const isoFormatDate = dateMapper.toDateString(queryValue, 'iso');
                    if (isoFormatDate) {
                        query[name] = isoFormatDate;
                        filterData[shortName] = isoFormatDate;
                    }
                }
                else {
                    query[name] = urlParamValue;
                    filterData[shortName] = urlParamValue;
                }
                if (filter?.type === FilterType.autocomplete.toLowerCase()) {
                    summaryValue = AutocompleteUtils.getDisplayValue(filter, queryValue);
                }
                else {
                    summaryValue = dateDisplayValue || summaryValue;
                }
                const fieldDisplayName = DefinitionUtils.getFieldDisplayName(fields, shortName);
                querySummary.push({
                    name: fieldDisplayName || shortName,
                    value: summaryValue,
                });
            }
        }
        else if (name.startsWith('sort')) {
            query[name] = queryValue;
            sortData[name] = queryValue;
            const fieldDef = DefinitionUtils.getField(fields, queryValue);
            let displayName = 'Sort Direction';
            let displayValue = queryValue === 'true' ? 'Ascending' : 'Descending';
            if (fieldDef) {
                displayName = 'Sort Column';
                displayValue = fieldDef.display;
            }
            querySummary.push({
                name: displayName,
                value: displayValue,
            });
        }
    });
    return {
        query,
        filterData,
        querySummary,
        sortData,
    };
};
var FiltersFormUtils = {
    renderFilters,
    setQueryFromFilters,
};

export { FiltersFormUtils as default, getSortByFromDefinition, renderFilters, setDurationStartAndEnd, setQueryFromFilters };
//# sourceMappingURL=utils.js.map
