import localsHelper from '../../../../../utils/localsHelper.js';
import AsyncReportUtils from './utils.js';
import ViewReportUtils from '../../utils.js';
import { ErrorHandler } from '../../../../../utils/ErrorHandler/ErrorHandler.js';

class ViewAyncReportController {
    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 AsyncReportUtils.renderReport(params);
            res.render(`dpr/routes/journeys/view-report/report`, {
                layoutPath: this.layoutPath,
                ...renderData,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler(error).formatError();
            let refreshLink;
            const { recentlyViewedService } = this.services;
            if (dprError.status === 'EXPIRED' && recentlyViewedService) {
                const { dprUser } = localsHelper.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);
        }
    };
    applyFilters = async (req, res, _next) => {
        await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, 'filters');
    };
    applyColumns = async (req, res, _next) => {
        await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, 'columns');
    };
}

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