'use strict';

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

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

class ViewAsyncDashboardController {
    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'] || [];
            // Get dashboard render data
            const dashboardData = await utils$1.default.renderAsyncDashboard({ req, res, services: this.services, next });
            // If report is expired redirect the the polling page to show expired status
            if (dashboardData.expired) {
                await utils.updateStateToExpiredAndRedirect(req, res, this.services);
                return;
            }
            res.render(`dpr/routes/journeys/view-report/dashboard`, {
                layoutPath: this.layoutPath,
                ...dashboardData,
                validationErrors,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler.ErrorHandler(error).formatError();
            req.body ??= {};
            req.body.title = `Failed to retrieve dashboard`;
            req.body.error = dprError;
            next(error);
        }
    };
    applyFilters = async (req, res, _next) => {
        await utils.default.applyDashboardInteractiveQuery(req, res, '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);
        }
    };
}

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