import localsHelper from '../localsHelper.js';
import { ReportType } from '../../types/UserReports.js';
import { FilterType } from '../../components/_filters/filter-input/enum.js';
import DateRangeInputUtils from '../../components/_inputs/date-range/utils.js';
import GranularDateRangeInputUtils from '../../components/_inputs/granular-date-range/utils.js';
import { getRequestParam } from '../indexedAccesHelper.js';
import { uiDateToApi } from '../dateHelper.js';

const saveDefaults = async (type, res, req, services) => {
    const defaultValuesForReport = await getDefaultValues(req, res, services, type);
    const { dprUser } = localsHelper.getValues(res);
    const id = getRequestParam({ req, param: 'id' });
    const reportId = getRequestParam({ req, param: 'reportId' });
    const { defaultFilterValuesService } = services;
    return defaultFilterValuesService
        ? defaultFilterValuesService.save(dprUser.id, reportId, id, defaultValuesForReport)
        : undefined;
};
const removeDefaults = async (type, res, req, services) => {
    const { dprUser } = localsHelper.getValues(res);
    const { reportId, id } = req.params;
    const { defaultFilterValuesService } = services;
    return defaultFilterValuesService ? defaultFilterValuesService.delete(dprUser.id, reportId, id, type) : undefined;
};
const getDefaultValues = async (req, res, services, filtersType) => {
    const { token, definitionsPath } = localsHelper.getValues(res);
    const { reportId, id, type } = req.params;
    let definition;
    let fields = [];
    if (type === ReportType.REPORT) {
        definition = await services.reportingService.getDefinition(token, reportId, id, definitionsPath);
        fields = definition.variant.specification?.fields || [];
    }
    else {
        definition = await services.dashboardService.getDefinition(token, reportId, id, definitionsPath);
        fields = definition.filterFields || [];
    }
    const bodyFilterValues = Object.keys(req.body)
        .filter((k) => {
        return k.includes('filters.') || k.includes('sortColumn') || k.includes('sortedAsc');
    })
        .map((k) => {
        return { name: k.replace('filters.', ''), value: req.body[k] };
    })
        .map((k) => {
        const n = k.name.split('.')[0];
        const field = fields.find((f) => f.name === n);
        let { value, name } = k;
        if (field) {
            const filterType = field.filter?.type.toLocaleLowerCase();
            switch (filterType) {
                case FilterType.multiselect.toLocaleLowerCase():
                    value = Array.isArray(k.value) ? k.value.join(',') : k.value;
                    break;
                case FilterType.dateRange.toLocaleLowerCase():
                    ({ value, name } = DateRangeInputUtils.setDefaultValue(req, name));
                    break;
                case FilterType.granularDateRange.toLocaleLowerCase():
                    ({ name, value } = GranularDateRangeInputUtils.setDefaultValue(req, name));
                    break;
            }
        }
        return {
            name,
            value,
            type: filtersType,
        };
    });
    const defaultValuesConfig = Array.from(new Set(bodyFilterValues.map((a) => a.name)))
        .map((name) => {
        return bodyFilterValues.find((a) => a.name === name);
    })
        .filter((c) => c !== undefined);
    const result = defaultValuesConfig.filter((defaultValue) => {
        return defaultValue ? defaultValue.value !== '' : false;
    });
    return result;
};
const setUserContextDefaults = (res, filters) => {
    const { dprUser } = localsHelper.getValues(res);
    const { activeCaseLoadId } = dprUser;
    filters.forEach((filter) => {
        if (filter.type.toLocaleLowerCase() === FilterType.autocomplete.toLocaleLowerCase() &&
            filter.text.toLocaleLowerCase().includes('establishment') &&
            activeCaseLoadId?.length) {
            const f = filter;
            const option = f.options.find((opt) => opt.value === activeCaseLoadId);
            if (option) {
                f.value = option.text;
                f.staticOptionNameValue = activeCaseLoadId;
            }
        }
    });
    return filters;
};
/**
 * Createa a query string from the saved defaults
 *
 * @param {defaultFilterValue[]} defaults
 * @return {*}  {string}
 */
const createQsFromSavedDefaults = (defaults, fieldDefinitions) => {
    const fieldMap = new Map(fieldDefinitions.map((f) => [f.name, f]));
    return defaults
        .flatMap(({ name, value }) => {
        const keyBase = `filters.${name}`;
        const fieldDef = fieldMap.get(name);
        // --------------------------------------------
        // STRING
        // --------------------------------------------
        if (typeof value === 'string') {
            const isMultiSelect = fieldDef?.filter?.type === 'multiselect';
            if (isMultiSelect) {
                return value
                    .split(',')
                    .map((v) => v.trim())
                    .filter(Boolean)
                    .map((v) => [keyBase, v]);
            }
            return [[keyBase, uiDateToApi(value) || value]];
        }
        // --------------------------------------------
        // GRANULAR DATERANGE
        // --------------------------------------------
        if ('granularity' in value) {
            return [
                [`${keyBase}.start`, uiDateToApi(value.start) || ''],
                [`${keyBase}.end`, uiDateToApi(value.end) || ''],
                [`${keyBase}.granularity`, String(value.granularity)],
                [`${keyBase}.quickFilter`, String(value.quickFilter)],
            ];
        }
        // --------------------------------------------
        // DATE RANGE
        // --------------------------------------------
        const pairs = [
            [`${keyBase}.start`, uiDateToApi(value.start) || ''],
            [`${keyBase}.end`, uiDateToApi(value.end) || ''],
        ];
        if (value.relative !== undefined) {
            pairs.push([`${keyBase}.relative-duration`, value.relative]);
        }
        return pairs;
    })
        .reduce((params, [key, value]) => {
        params.append(key, value);
        return params;
    }, new URLSearchParams())
        .toString();
};
var PersonalisationUtils = { saveDefaults, removeDefaults, setUserContextDefaults };

export { createQsFromSavedDefaults, PersonalisationUtils as default, removeDefaults, saveDefaults, setUserContextDefaults };
//# sourceMappingURL=personalisationUtils.js.map
