import { ErrorHandler } from '../../../../../utils/ErrorHandler/ErrorHandler.js';
import localsHelper from '../../../../../utils/localsHelper.js';
import DashboardUtils from './utils.js';
import ViewReportUtils from '../../utils.js';
import { LoadType } from '../../../../../types/UserReports.js';

class ViewAsyncDashboardController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    GET = async (req, res, next) => {
        const { type, tableId } = req.params;
        try {
            // Redirect the same path to attach query string
            if (ViewReportUtils.redirectWithDefaults(res, req)) {
                return;
            }
            // Get the validation errors
            const validationErrors = res.locals['validationErrors'] || [];
            // Get dashboard render data
            const renderData = await DashboardUtils.renderAsyncDashboard({ req, res, services: this.services, next });
            res.render(`dpr/routes/journeys/view-report/dashboard`, {
                layoutPath: this.layoutPath,
                ...renderData,
                validationErrors,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler(error).formatError();
            let refreshLink;
            if (dprError.status === 'EXPIRED') {
                const { dprUser } = localsHelper.getValues(res);
                refreshLink = await this.services.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);
        }
    };
    applyFilters = async (req, res, _next) => {
        await ViewReportUtils.applyDashboardInteractiveQuery(req, res, 'filters', LoadType.ASYNC);
    };
    resetFilters = async (req, res, next) => {
        try {
            const { id, reportId, tableId } = req.params;
            const sessionKey = { id, reportId, tableId };
            await ViewReportUtils.resetFilters(req, res, sessionKey);
        }
        catch (error) {
            req.body = {
                title: 'Failed to reset filters',
                error: new ErrorHandler(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
}

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