'use strict';

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

var node = require('@sentry/node');
var localsHelper = require('../../../../utils/localsHelper.js');

class MissingReportFormController {
    layoutPath;
    missingReportService;
    reportingService;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.missingReportService = services.missingReportService;
        this.reportingService = services.reportingService;
    }
    GET = async (req, res, next) => {
        const { token, csrfToken, definitionsPath, dprUser } = localsHelper.default.getValues(res);
        const { reportId, variantId } = req.params;
        const reportDefinition = await this.reportingService.getDefinition(token, reportId, variantId, definitionsPath ?? 'definitions/prisons/missing');
        const { variant, name } = reportDefinition;
        try {
            res.render(`dpr/routes/journeys/request-missing-report/form/view`, {
                title: 'This report is not available',
                report: {
                    reportId,
                    variantId,
                    reportName: name,
                    name: variant.name,
                    description: variant.description || reportDefinition.description,
                },
                user: dprUser,
                csrfToken,
                layoutPath: this.layoutPath,
                postEndpoint: `/dpr/request-missing-report/${reportId}/${variantId}/form/submit`,
            });
        }
        catch (error) {
            next(error);
        }
    };
    POST = async (req, res, next) => {
        const { body } = req;
        const { reportId, variantId, reportName, variantName, 'more-detail': requestDetails } = body;
        const { token } = localsHelper.default.getValues(res);
        try {
            const submission = this.missingReportService.submitMissingReportEntry(token, reportId, variantId, requestDetails);
            if (submission) {
                submission.then(() => {
                    const queryParams = `reportName=${reportName}&name=${variantName}&reportId=${reportId}&variantId=${variantId}`;
                    const redirect = `./submitted?${queryParams}`;
                    res.redirect(redirect);
                }, (err) => {
                    node.captureException(err);
                    next(err);
                });
            }
        }
        catch (error) {
            node.captureException(error);
            next(error);
        }
    };
}

exports.MissingReportFormController = MissingReportFormController;
exports.default = MissingReportFormController;
//# sourceMappingURL=controller.js.map
