'use strict';

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

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

class ViewSyncReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    GET = async (req, res, next) => {
        try {
            // Redirect the same path to attach query string
            if (utils.default.redirectWithDefaults(res, req)) {
                return;
            }
            // Get the validation errors
            const validationErrors = res.locals['validationErrors'] || [];
            // Render the report
            const renderData = await utils$1.renderReport({ req, res, services: this.services });
            res.render(`dpr/routes/journeys/view-report/report`, {
                layoutPath: this.layoutPath,
                ...renderData,
                validationErrors,
            });
        }
        catch (error) {
            req.body ??= {};
            req.body.title = `Report Failed`;
            req.body.errorDescription = 'We were unable to show this report for the following reason:';
            req.body.error = new ErrorHandler.ErrorHandler(error).formatError();
            next(error);
        }
    };
    // -----------------------------
    //  Save Defaults
    // -----------------------------
    saveDefaultFilterValues = async (req, res, next) => {
        try {
            personalisationUtils.default.saveDefaults(filtersTypeEnum.FiltersType.INTERACTIVE, 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 {
            personalisationUtils.default.removeDefaults(filtersTypeEnum.FiltersType.INTERACTIVE, 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);
        }
    };
    // -----------------------------
    //  Filters
    // -----------------------------
    applyFilters = async (req, res, _next) => {
        await utils.default.applyReportInteractiveQuery(req, res, this.services, 'filters', UserReports.LoadType.SYNC);
    };
    resetFilters = async (req, res, next) => {
        try {
            const { id, reportId } = req.params;
            const sessionKey = { id, reportId };
            await utils.default.resetFilters(req, res, sessionKey);
        }
        catch (error) {
            req.body = {
                title: 'Failed to reset filters',
                error: new ErrorHandler.ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
    // -----------------------------
    //  Columns
    // -----------------------------
    applyColumns = async (req, res, _next) => {
        await utils.default.applyReportInteractiveQuery(req, res, this.services, 'columns', UserReports.LoadType.SYNC);
    };
    resetColumns = async (req, res, next) => {
        try {
            const { id, reportId } = req.params;
            // Create the final querystring
            const finalQuery = utils.default.resetColumnsQueryString(req, { id, reportId });
            // Redirect with new query string - query string will handle all rendered elements
            if (finalQuery) {
                res.redirect(`${req.baseUrl}?${finalQuery}`);
            }
        }
        catch (error) {
            req.body = {
                title: 'Failed to reset filters',
                error: new ErrorHandler.ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
}

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