'use strict';

var captureError = require('../../utils/captureError.js');
var localsHelper = require('../../utils/localsHelper.js');
var getReportStatus = require('../../utils/ReportStatus/getReportStatus.js');

const createReportPollingHandler = (services, renderFn) => async (req, res, _next) => {
    try {
        const { executionId } = req.params;
        const { token, dprUser } = localsHelper.getValues(res);
        const requestedReport = await services.requestedReportService.getReportByExecutionId(executionId, dprUser.id);
        if (!requestedReport) {
            return res.sendStatus(404);
        }
        const { resolution, updated } = await getReportStatus.evaluateAndUpdateReportStatus({
            stored: requestedReport,
            services,
            token,
            res,
        });
        if (resolution.type === 'NO_CHANGE' || !updated) {
            return res.sendStatus(204);
        }
        const { template, data } = renderFn(updated, resolution, req, res);
        return res.render(template, data, (err, html) => {
            if (err)
                return res.sendStatus(500);
            return res.type('text/html').send(html);
        });
    }
    catch (error) {
        captureError.captureDprError(error);
        return res.sendStatus(500);
    }
};

exports.createReportPollingHandler = createReportPollingHandler;
//# sourceMappingURL=createReportPollingHandler.js.map
