import { renderReport } from './utils.js';
import ViewReportUtils, { updateStateToExpiredAndRedirect } from '../../utils.js';
import { ErrorHandler } from '../../../../../utils/ErrorHandler/ErrorHandler.js';
import { LoadType } from '../../../../../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) => {
        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 the report config
            const reportData = await renderReport({
                req,
                res,
                services: this.services,
                next,
            });
            // If report is expired redirect the the polling page to show expired status
            if (reportData.expired) {
                await updateStateToExpiredAndRedirect(req, res, this.services);
                return;
            }
            // Render the report
            res.render(`dpr/routes/journeys/view-report/report`, {
                layoutPath: this.layoutPath,
                ...reportData,
                validationErrors,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler(error).formatError();
            req.body ??= {};
            req.body.title = `Failed to retrieve report`;
            req.body.error = dprError;
            next(error);
        }
    };
    // -----------------------------
    //  Filters
    // -----------------------------
    applyFilters = async (req, res, _next) => {
        await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, '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);
        }
    };
    // -----------------------------
    //  Columns
    // -----------------------------
    applyColumns = async (req, res, _next) => {
        await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, 'columns', LoadType.ASYNC);
    };
    resetColumns = async (req, res, next) => {
        try {
            const { id, reportId, tableId } = req.params;
            // Create the final querystring
            const finalQuery = ViewReportUtils.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(error).formatError(),
                ...(req.body && { ...req.body }),
            };
            next(error);
        }
    };
}

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