import localsHelper from '../utils/localsHelper.js';
import { getDefinitionByType } from '../utils/definitionUtils.js';

const setupCurrentDefinition = (services) => {
    return async (req, res, next) => {
        const { token, dprUser, definitionsPath } = localsHelper.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 getDefinitionByType(type, services, token, reportId, id, definitionsPath, queryData);
        res.locals['fields'] = fields ?? [];
        res.locals['definition'] = definition;
        return next();
    };
};

export { setupCurrentDefinition as default, setupCurrentDefinition };
//# sourceMappingURL=setupCurrentDefinition.js.map
