import { ErrorHandler } from '../../../../../utils/ErrorHandler/ErrorHandler.js';
import localsHelper from '../../../../../utils/localsHelper.js';
import DashboardUtils from './utils.js';
import ViewReportUtils from '../../utils.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 {
            const params = { req, res, services: this.services, next };
            const renderData = await DashboardUtils.renderAsyncDashboard(params);
            res.render(`dpr/routes/journeys/view-report/dashboard`, {
                layoutPath: this.layoutPath,
                ...renderData,
            });
        }
        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, this.services, 'filters');
    };
}

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