import { Router } from 'express';
import { captureException } from '@sentry/node';
import { storeActiveReportSessionData } from '../../../middleware/setUpActiveReport.js';
import { RequestReportController } from './controller.js';
import routes from './filters/routes.js';
import routes$1 from './status/routes.js';
import reportAuthoriser from '../../../middleware/reportAuthoriser.js';

/* eslint-disable no-param-reassign */
function Routes({ layoutPath, services }) {
    const router = Router({ mergeParams: true });
    const controller = new RequestReportController(layoutPath, services);
    router.post(`/:type/:reportId/:id/:executionId/cancel`, controller.CANCEL);
    router.use(`/:type/:reportId/:id/filters`, reportAuthoriser(services, layoutPath), storeActiveReportSessionData({ services }), routes({ layoutPath, services }), controller.errorHandler);
    router.use(`/:type/:reportId/:id/:executionId/status`, reportAuthoriser(services, layoutPath), storeActiveReportSessionData({ services }), routes$1({ layoutPath, services }), controller.errorHandler);
    router.get(`/:type/:reportId/:id/:executionId/cancel/failed`, (req, res, _next) => {
        const body = JSON.parse(req.flash('ERROR_BODY')?.[0] || '');
        const params = JSON.parse(req.flash('ERROR_PARAMS')?.[0] || '');
        const error = req.flash('ERROR');
        captureException(error);
        res.render(`dpr/routes/journeys/view-report/error`, {
            layoutPath,
            ...(body && { ...body }),
            ...(params && { ...params }),
            error,
            params,
        });
    });
    return router;
}
const RequestReportRoutes = ({ services, path, layoutPath, }) => {
    const router = Router({ mergeParams: true });
    router.use(path, Routes({ services, layoutPath }));
    return router;
};

export { RequestReportRoutes, Routes };
//# sourceMappingURL=routes.js.map
