'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

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

const setupCurrentDefinition = (services) => {
    return async (req, res, next) => {
        const { token, dprUser, definitionsPath } = localsHelper.default.getValues(res);
        const { reportId, id, type, tableId } = req.params;
        if (!token || !dprUser)
            return next();
        const definitionSummary = await services.reportingService.getDefinitionSummary(token, reportId, definitionsPath);
        res.locals['reportDefinitionSummary'] = definitionSummary;
        let queryData;
        if (tableId) {
            const requestData = await services.requestedReportService.getReportByTableId(tableId, dprUser.id);
            queryData = requestData?.query?.data;
        }
        const { fields, definition } = await definitionUtils.getDefinitionByType(type, services, token, reportId, id, definitionsPath, queryData);
        res.locals['fields'] = fields ?? [];
        res.locals['definition'] = definition;
        return next();
    };
};

exports.default = setupCurrentDefinition;
exports.setupCurrentDefinition = setupCurrentDefinition;
//# sourceMappingURL=setupCurrentDefinition.js.map
