'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var localsHelper = require('../../../../utils/localsHelper.js');
var utils$1 = require('../../../../components/my-reports/my-reports-list-item/utils.js');
var types = require('../../../../components/my-reports/types.js');
var createReportPollingHandler = require('../../../../controllers/reportPolling/createReportPollingHandler.js');
var utils = require('../../../../components/my-reports/utils.js');
var captureError = require('../../../../utils/captureError.js');

class RequestedReportsController {
    services;
    GET;
    constructor(services) {
        this.services = services;
        /**
         * Gets an individual My Reports row and renders it as a string
         */
        this.GET = createReportPollingHandler.createReportPollingHandler(this.services, (updated, resolution, req, res) => {
            const viewModel = utils$1.buildMyReportListRow(updated, resolution.newStatus, req, res, types.ListType.REQUESTED);
            return {
                template: 'dpr/components/my-reports/my-reports-list-item/row.njk',
                data: { item: viewModel },
            };
        });
    }
    /**
     * Remove requested report action
     *
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    POST = async (req, res) => {
        const { dprUser } = localsHelper.default.getValues(res);
        const { executionId } = req.params;
        // Remove the report
        await this.services.requestedReportService.removeReport(executionId, dprUser.id);
        // Update the locals
        res.locals['requestedReports'] = await this.services.requestedReportService.getAllReports(dprUser.id);
        try {
            // get the data for the requested list
            const maxRows = req.body?.maxRows !== undefined ? Number(req.body.maxRows) : undefined;
            const viewModel = await utils.initRequested(req, res, {
                ...(maxRows && { maxRows }),
            });
            return res.render('dpr/components/my-reports/my-reports-list/view.njk', { viewModel }, (err, html) => {
                if (err)
                    return res.sendStatus(500);
                return res.type('text/html').send(html);
            });
        }
        catch (error) {
            captureError.captureDprError(error, 'Failed to refresh list after removal');
            return res.sendStatus(500);
        }
    };
}

exports.RequestedReportsController = RequestedReportsController;
exports.default = RequestedReportsController;
//# sourceMappingURL=controller.js.map
