'use strict';

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

var utils = require('./utils.js');
var personalisationUtils = require('../../../../utils/Personalisation/personalisationUtils.js');
var filtersTypeEnum = require('../../../../components/_filters/filtersTypeEnum.js');
var ErrorHandler = require('../../../../utils/ErrorHandler/ErrorHandler.js');

class RequestReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    // Render request page
    GET = async (req, res, next) => {
        try {
            const requestRenderData = await utils.default.renderRequest({
                req,
                res,
                services: this.services,
                next,
            });
            res.render(`dpr/routes/journeys/request-report/filters/view`, {
                layoutPath: this.layoutPath,
                ...requestRenderData,
            });
        }
        catch (error) {
            req.body ??= {};
            req.body.title = 'Request failed';
            req.body.errorDescription = `Your ${req.params['type']} has failed to generate.`;
            req.body.error = new ErrorHandler.ErrorHandler(error).formatError();
            next(req.body.error);
        }
    };
    // Request report
    POST = async (req, res, next) => {
        try {
            const executionData = await utils.default.request({
                req,
                res,
                services: this.services,
                next,
            });
            const { executionId, dataProductDefinitionsPath } = executionData;
            if (executionId) {
                const redirect = dataProductDefinitionsPath
                    ? `${executionId}/status?dataProductDefinitionsPath=${dataProductDefinitionsPath}`
                    : `${executionId}/status`;
                res.redirect(redirect);
            }
            else {
                res.end();
            }
        }
        catch (error) {
            const filters = utils.default.getFiltersFromReqBody(req);
            req.body = {
                title: 'Request Failed',
                errorDescription: `Your ${req.params['type']} has failed to generate.`,
                error: new ErrorHandler.ErrorHandler(error).formatError(),
                retry: true,
                filters,
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
    saveDefaultFilterValues = async (req, res, next) => {
        try {
            await personalisationUtils.default.saveDefaults(filtersTypeEnum.FiltersType.REQUEST, res, req, this.services);
            res.redirect(`${req.baseUrl}?defaultsSaved=true`);
        }
        catch (error) {
            req.body = {
                title: 'Failed to save defaults',
                error: new ErrorHandler.ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
    removeDefaultFilterValues = async (req, res, next) => {
        try {
            await personalisationUtils.default.removeDefaults(filtersTypeEnum.FiltersType.REQUEST, res, req, this.services);
            res.redirect(req.baseUrl);
        }
        catch (error) {
            req.body = {
                title: 'Failed to remove defaults',
                error: new ErrorHandler.ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
}

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