'use strict';

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

var localsHelper = require('../../../../../utils/localsHelper.js');
var utils$1 = require('./utils.js');
var utils = require('../../utils.js');
var ErrorHandler = require('../../../../../utils/ErrorHandler/ErrorHandler.js');
var UserReports = require('../../../../../types/UserReports.js');

class ViewAyncReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    /**
     * Renders the report
     *
     * - ensures first render includes correct query string
     * - which ensures first load always get the correct data
     *
     * @param {*} req
     * @param {*} res
     * @param {*} next
     * @return {*}
     */
    GET = async (req, res, next) => {
        const { type, tableId } = req.params;
        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'] || [];
            // get the report config
            const renderData = await utils$1.renderReport({
                req,
                res,
                services: this.services,
                next,
            });
            // Render the report
            res.render(`dpr/routes/journeys/view-report/report`, {
                layoutPath: this.layoutPath,
                ...renderData,
                validationErrors,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler.ErrorHandler(error).formatError();
            let refreshLink;
            const { recentlyViewedService } = this.services;
            if (dprError.status === 'EXPIRED' && recentlyViewedService) {
                const { dprUser } = localsHelper.default.getValues(res);
                refreshLink = await recentlyViewedService.asyncSetToExpiredByTableId(tableId, dprUser.id);
            }
            req.body ??= {};
            req.body.title = `Failed to retrieve ${type}`;
            req.body.error = dprError;
            if (refreshLink) {
                req.body.refreshLink = refreshLink;
            }
            next(error);
        }
    };
    // -----------------------------
    //  Filters
    // -----------------------------
    applyFilters = async (req, res, _next) => {
        await utils.default.applyReportInteractiveQuery(req, res, this.services, 'filters', UserReports.LoadType.ASYNC);
    };
    resetFilters = async (req, res, next) => {
        try {
            const { id, reportId, tableId } = req.params;
            const sessionKey = { id, reportId, tableId };
            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.ASYNC);
    };
    resetColumns = async (req, res, next) => {
        try {
            const { id, reportId, tableId } = req.params;
            // Create the final querystring
            const finalQuery = utils.default.resetColumnsQueryString(req, { id, reportId, tableId });
            // 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.ViewAyncReportController = ViewAyncReportController;
exports.default = ViewAyncReportController;
//# sourceMappingURL=controller.js.map
