import AysncRequestUtils from './utils.js';
import PersonalisationUtils from '../../../../utils/Personalisation/personalisationUtils.js';
import { FiltersType } from '../../../../components/_filters/filtersTypeEnum.js';
import { ErrorHandler } from '../../../../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 AysncRequestUtils.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(error).formatError();
            next(req.body.error);
        }
    };
    // Request report
    POST = async (req, res, next) => {
        try {
            const executionData = await AysncRequestUtils.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 = AysncRequestUtils.getFiltersFromReqBody(req);
            req.body = {
                title: 'Request Failed',
                errorDescription: `Your ${req.params['type']} has failed to generate.`,
                error: new ErrorHandler(error).formatError(),
                retry: true,
                filters,
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
    saveDefaultFilterValues = async (req, res, next) => {
        try {
            await PersonalisationUtils.saveDefaults(FiltersType.REQUEST, res, req, this.services);
            res.redirect(`${req.baseUrl}?defaultsSaved=true`);
        }
        catch (error) {
            req.body = {
                title: 'Failed to save defaults',
                error: new ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
    removeDefaultFilterValues = async (req, res, next) => {
        try {
            await PersonalisationUtils.removeDefaults(FiltersType.REQUEST, res, req, this.services);
            res.redirect(req.baseUrl);
        }
        catch (error) {
            req.body = {
                title: 'Failed to remove defaults',
                error: new ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
}

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