'use strict';

require('../../_filters/filter-input/enum.js');
require('../../_inputs/date-range/utils.js');
require('../../_inputs/date-input/utils.js');
require('../../_inputs/granular-date-range/utils.js');
require('../../../types/UserReports.js');
var dateHelper = require('../../../utils/dateHelper.js');

/**
 * Builds filter data from req.body filters.
 *
 * - Processes only `filters.*` keys
 * - Strips `filters.` prefix
 * - Drops empty values
 * - Normalizes UI dates via uiDateToApi
 * - Collapses arrays to CSV (BE contract)
 *
 * @param {Record<string, unknown>} body
 * @return {*}  {Record<string, string>}
 */
const buildFilterData = (body) => {
    return Object.entries(body).reduce((acc, [key, value]) => {
        if (!key.startsWith('filters.'))
            return acc;
        if (value === undefined || value === null || value === '')
            return acc;
        const shortName = key.replace(/^filters\./, '');
        if (Array.isArray(value)) {
            const csv = value
                .filter((v) => v != null && v !== '')
                .map(String)
                .join(',');
            if (csv) {
                acc[shortName] = csv;
            }
            return acc;
        }
        const stringValue = String(value);
        acc[shortName] = dateHelper.uiDateToApi(stringValue) ?? stringValue;
        return acc;
    }, {});
};
/**
 * Builds sort data from the request body.
 *
 * - Supports single (radio) or multiple (checkbox) sort columns
 * - Uses a global sort direction
 * - Collapses columns to CSV (API contract)
 *
 * @param {Record<string, unknown>} body
 * @return {*}  {Record<string, string>}
 */
const buildSortData = (body) => {
    const rawColumns = body['sortColumn'];
    if (rawColumns == null || rawColumns === '') {
        return undefined;
    }
    const columns = Array.isArray(rawColumns)
        ? rawColumns.filter((v) => v != null && v !== '').map(String)
        : [String(rawColumns)];
    if (columns.length === 0) {
        return undefined;
    }
    const direction = body['sortedAsc'] === 'false' ? 'false' : 'true';
    return {
        sortColumn: columns.join(','),
        sortedAsc: direction,
    };
};

exports.buildFilterData = buildFilterData;
exports.buildSortData = buildSortData;
//# sourceMappingURL=utils.js.map
