'use strict';

var definitionUtils = require('../utils/definitionUtils.js');
var buildFilterSchema = require('./buildFilterSchema.js');
var queryMappers = require('../utils/queryMappers.js');
var urlHelper = require('../utils/urlHelper.js');
var sessionHelper = require('../utils/sessionHelper.js');
var UserReports = require('../types/UserReports.js');

/**
 * Middleware to validate a report or dashboard filter request
 *
 * - creates a schema dynamically from the filter definitions
 * - checks the form data against it
 * - sets errors in req.flash if there are errors
 * - and redirects back to current page
 *
 * @param {*} req
 * @param {*} res
 * @param {*} next
 * @return {*}
 */
const validateFilters = ({ interactive, loadType = UserReports.LoadType.ASYNC }) => (req, res, next) => {
    const { type: reportType } = req.params;
    const definition = res.locals.definition;
    if (!definition) {
        next(new Error('Definition missing from res.locals'));
    }
    else {
        // Normalise the filters
        const rawBody = req.body;
        const normalisedFilters = rawBody && typeof rawBody === 'object' ? queryMappers.extractFiltersFromBody(rawBody) : {};
        // Get the fields
        const fields = reportType === UserReports.ReportType.REPORT
            ? definitionUtils.getFields(definition)
            : definitionUtils.getDashboardFields(definition);
        // Get only the relevant applicable fields for the form type
        const applicableFields = definitionUtils.getFieldsWithFilters(fields).filter((field) => {
            const isInteractive = field.filter?.interactive === true;
            return interactive ? isInteractive : !isInteractive;
        });
        // Build the schema based on the fields filters and check it
        const schema = buildFilterSchema.buildFilterSchemaFromFields(applicableFields);
        const result = schema.safeParse(normalisedFilters);
        if (result.success) {
            // All good - go to request the report middleware
            res.locals.validatedFilters = result.data;
            next();
        }
        else {
            const errors = result.error.issues.map((issue) => ({
                href: `#filters.${issue.path.join('.')}`,
                text: issue.message,
            }));
            req.flash('DPR_ERRORS', JSON.stringify(errors));
            let redirect = req.originalUrl;
            if (interactive) {
                const currentParams = buildRedirectParams(req, rawBody, loadType);
                redirect = `${req.baseUrl}?${currentParams}`;
            }
            res.redirect(redirect);
        }
    }
};
/**
 * Builds the redirect params from the form body
 * - used for interactive report filters
 *
 * @param {Request} req
 * @param {Record<string, unknown>} rawBody
 * @param {LoadType} loadType
 * @return {*}
 */
const buildRedirectParams = (req, rawBody, loadType) => {
    const { tableId, id, reportId } = req.params;
    // Create the qs from the form body
    const filtersQs = queryMappers.formBodyToQs(rawBody);
    // Get the current query string params
    const sessionKey = loadType === UserReports.LoadType.ASYNC ? { id, tableId, reportId } : { id, reportId };
    const currentColumnsSearch = sessionHelper.getActiveJourneyValue(req, sessionKey, 'currentReportColumnsSearch');
    const currentSortSearch = sessionHelper.getActiveJourneyValue(req, sessionKey, 'currentSortSearch');
    const currentPageSizeSearch = sessionHelper.getActiveJourneyValue(req, sessionKey, 'currentPageSizeSearch');
    return urlHelper.joinQueryStrings(filtersQs, currentColumnsSearch, currentSortSearch, currentPageSizeSearch, 'preventDefault=true');
};

exports.validateFilters = validateFilters;
//# sourceMappingURL=validateFilters.js.map
