'use strict';

var sessionHelper = require('../../../utils/sessionHelper.js');
var queryMappers = require('../../../utils/queryMappers.js');
var definitionUtils = require('../../../utils/definitionUtils.js');
var _enum = require('../filter-input/enum.js');
var filterDisplay = require('../../../utils/filterDisplay.js');

/**
 * -------------------------------------------------------------------------------------
 * Public API
 * -------------------------------------------------------------------------------------
 */
function buildAppliedFilters(req, sessionKey, fields) {
    const currentReportFiltersSearch = sessionHelper.getActiveJourneyValue(req, sessionKey, 'currentReportFiltersSearch');
    let appliedFilters = [];
    if (currentReportFiltersSearch) {
        const query = queryMappers.qsToQueryObject(currentReportFiltersSearch, 'filters.');
        const filterFields = definitionUtils.getFieldsWithFilters(fields);
        appliedFilters = buildAppliedFilterChips(query, filterFields);
    }
    return appliedFilters;
}
function buildAppliedFilterChips(query, fields) {
    return fields
        .filter((field) => Boolean(field.filter))
        .map((field) => buildChipForField(field, query))
        .filter(Boolean);
}
/**
 * -------------------------------------------------------------------------------------
 * Orchestrator (semantic parity with Selected Filters)
 * -------------------------------------------------------------------------------------
 */
function buildChipForField(field, query) {
    const filter = field.filter;
    const baseKey = `filters.${field.name}`;
    switch (filter.type.toLowerCase()) {
        case _enum.FilterType.dateRange.toLowerCase():
            return buildDateRangeChip(field, baseKey, query);
        case _enum.FilterType.granularDateRange.toLowerCase():
            return buildGranularDateRangeChip(field, baseKey, query);
        case _enum.FilterType.multiselect.toLowerCase():
            return buildMultiSelectChip(field, baseKey, query);
        case _enum.FilterType.date.toLowerCase():
            return buildDateChip(field, baseKey, query);
        default:
            return buildSingleValueChip(field, baseKey, query);
    }
}
/**
 * -------------------------------------------------------------------------------------
 * Builders
 * -------------------------------------------------------------------------------------
 */
/**
 * Date range
 * - Renders if either bound exists
 * - Missing bounds rendered as 'unset'
 */
function buildDateRangeChip(field, baseKey, query) {
    const relativeDuration = query[`${baseKey}.relative-duration`];
    const start = query[`${baseKey}.start`];
    const end = query[`${baseKey}.end`];
    // Render if anything date-related is set
    if (!relativeDuration && !start && !end)
        return null;
    return {
        displayName: field.display,
        displayValue: filterDisplay.formatFilterDisplay(field, {
            values: [],
            ...(start !== undefined && { start }),
            ...(end !== undefined && { end }),
            ...(relativeDuration !== undefined && { relativeDuration }),
        }),
        reset: {
            keys: Object.keys(query).filter((key) => key.startsWith(baseKey)),
        },
    };
}
/**
 * Granular date range
 * - Renders if quick-filter OR either bound exists
 * - Quick-filter takes precedence
 * - Missing values rendered as 'unset'
 */
function buildGranularDateRangeChip(field, baseKey, query) {
    const quickFilter = query[`${baseKey}.quick-filter`];
    const granularity = query[`${baseKey}.granularity`];
    const start = query[`${baseKey}.start`];
    const end = query[`${baseKey}.end`];
    // Render if anything date-related is set
    if (!quickFilter && !start && !end)
        return null;
    return {
        displayName: field.display,
        displayValue: filterDisplay.formatFilterDisplay(field, {
            values: [],
            ...(quickFilter !== undefined && { quickFilter }),
            ...(granularity !== undefined && { granularity }),
            ...(start !== undefined && { start }),
            ...(end !== undefined && { end }),
        }),
        reset: {
            keys: Object.keys(query).filter((key) => key.startsWith(baseKey)),
        },
    };
}
/**
 * Multiselect
 * - Combined into single chip
 * - Truncated after 3 values
 */
function buildMultiSelectChip(field, key, query) {
    const raw = query[key];
    const values = Array.isArray(raw) ? raw : (typeof raw === 'string' && [raw]) || [];
    if (!values.length)
        return null;
    return {
        displayName: field.display,
        displayValue: filterDisplay.formatFilterDisplay(field, {
            values,
        }),
        reset: {
            keys: [key],
        },
    };
}
/**
 * Date
 */
function buildDateChip(field, key, query) {
    const raw = query[key];
    const value = Array.isArray(raw) ? raw[0] : (typeof raw === 'string' && raw) || undefined;
    if (!value)
        return null;
    return {
        displayName: field.display,
        displayValue: filterDisplay.formatFilterDisplay(field, {
            values: [value],
        }),
        reset: {
            keys: [key],
        },
    };
}
/**
 * Single value
 * - radio / select / text / autocomplete
 */
function buildSingleValueChip(field, key, query) {
    const raw = query[key];
    const value = Array.isArray(raw) ? raw[0] : (typeof raw === 'string' && raw) || undefined;
    if (!value)
        return null;
    return {
        displayName: field.display,
        displayValue: filterDisplay.formatFilterDisplay(field, {
            values: [value],
        }),
        reset: {
            keys: [key],
        },
    };
}

exports.buildAppliedFilterChips = buildAppliedFilterChips;
exports.buildAppliedFilters = buildAppliedFilters;
//# sourceMappingURL=utils.js.map
