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

class ViewSyncDashboardController {
    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 (ViewReportUtils.redirectWithDefaults(res, req)) {
                return;
            }
            // Get the validation errors
            const validationErrors = res.locals['validationErrors'] || [];
            // Render the dashaboad
            const renderData = await SyncDashboardUtils.renderSyncDashboard({ req, res, services: this.services });
            res.render(`dpr/routes/journeys/view-report/dashboard`, {
                layoutPath: this.layoutPath,
                ...renderData,
                validationErrors,
            });
        }
        catch (error) {
            req.body ??= {};
            req.body.title = `Dashboard Failed`;
            req.body.errorDescription = 'We were unable to show this dashboard for the following reason:';
            req.body.error = new ErrorHandler(error).formatError();
            next(error);
        }
    };
    applyFilters = async (req, res, _next) => {
        await ViewReportUtils.applyDashboardInteractiveQuery(req, res, 'filters', LoadType.SYNC);
    };
    resetFilters = async (req, res, next) => {
        try {
            const { id, reportId } = req.params;
            const sessionKey = { id, reportId };
            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 { ViewSyncDashboardController, ViewSyncDashboardController as default };
//# sourceMappingURL=controller.js.map
