'use strict';

var definitionUtils = require('../../../utils/definitionUtils.js');
var filterDisplay = require('../../../utils/filterDisplay.js');

/**
 * Builds the query summary
 *
 * @param {Record<string, unknown>} body
 * @param {components['schemas']['FieldDefinition'][]} fields
 * @return {*}  {QuerySummaryItem[]}
 */
const buildQuerySummary = (body, fields) => {
    const filterStateByField = collectFilterState(body);
    const filterSummaries = Array.from(filterStateByField.entries())
        .map(([fieldId, state]) => {
        const field = definitionUtils.getField(fields, fieldId);
        if (!field)
            return null;
        const value = filterDisplay.formatFilterDisplay(field, state);
        if (!value)
            return null;
        return {
            name: definitionUtils.getFieldDisplayName(fields, fieldId) ?? fieldId,
            value,
        };
    })
        .filter(Boolean);
    const sortSummaries = buildSortSummaries(body, fields);
    return [...filterSummaries, ...sortSummaries];
};
/**
 * Builds the filter state into predictable format
 *
 * @param {Record<string, unknown>} body
 * @return {*}  {Map<string, FilterDisplayState>}
 */
const collectFilterState = (body) => {
    const map = new Map();
    Object.entries(body).forEach(([key, value]) => {
        if (value == null || value === '' || value === 'no-filter')
            return;
        if (!key.startsWith('filters.'))
            return;
        const [, fieldId, suffix] = key.split('.');
        const state = map.get(fieldId) ?? { values: [] };
        switch (suffix) {
            case 'start':
                state.start = String(value);
                break;
            case 'end':
                state.end = String(value);
                break;
            case 'relative-duration':
                state.relativeDuration = String(value);
                break;
            case 'quick-filter':
                state.quickFilter = String(value);
                break;
            case 'granularity':
                state.granularity = String(value);
                break;
            default:
                state.values.push(String(value));
        }
        map.set(fieldId, state);
    });
    return map;
};
/**
 * Builds the sort summary
 *
 * @param {Record<string, unknown>} body
 * @param {components['schemas']['FieldDefinition'][]} fields
 * @return {*}  {QuerySummaryItem[]}
 */
const buildSortSummaries = (body, fields) => {
    const summary = [];
    Object.entries(body).forEach(([key, value]) => {
        if (value == null || value === '')
            return;
        if (key === 'sortColumn') {
            const columns = Array.isArray(value) ? value.map(String) : [String(value)];
            const displayNames = columns.map((col) => definitionUtils.getField(fields, col)?.display ?? col);
            summary.push({
                name: 'Sort Column',
                value: displayNames.join(', '),
            });
        }
        if (key === 'sortedAsc') {
            summary.push({
                name: 'Sort Direction',
                value: value === 'true' ? 'Ascending' : 'Descending',
            });
        }
    });
    return summary;
};

exports.buildQuerySummary = buildQuerySummary;
//# sourceMappingURL=utils.js.map
