import localsHelper from '../utils/localsHelper.js';
import { ReportType } from '../types/UserReports.js';

const reportAuthoriser = (services, layoutPath) => {
    return async (req, res, next) => {
        const { token } = localsHelper.getValues(res);
        const { reportId, id, variantId, type } = req.params;
        const dataProductDefinitionsPath = req.query['dataProductDefinitionsPath'];
        const definitionSummary = await services.reportingService.getDefinitionSummary(token, reportId, dataProductDefinitionsPath);
        const service = type === ReportType.REPORT ? services.reportingService : services.dashboardService;
        res.locals['definition'] = await service.getDefinition(token, reportId, (variantId || id), dataProductDefinitionsPath);
        if (definitionSummary?.authorised !== undefined && !definitionSummary.authorised) {
            res.render(`dpr/routes/journeys/view-report/unauthorised`, {
                layoutPath,
                ...req.body,
            });
        }
        else {
            next();
        }
    };
};

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