import localsHelper from '../../../../utils/localsHelper.js';
import { buildMyReportListRow } from '../../../../components/my-reports/my-reports-list-item/utils.js';
import { ListType } from '../../../../components/my-reports/types.js';
import { createReportPollingHandler } from '../../../../controllers/reportPolling/createReportPollingHandler.js';
import { initRequested } from '../../../../components/my-reports/utils.js';
import { captureDprError } from '../../../../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(this.services, (updated, resolution, req, res) => {
            const viewModel = buildMyReportListRow(updated, resolution.newStatus, req, res, 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.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 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) {
            captureDprError(error, 'Failed to refresh list after removal');
            return res.sendStatus(500);
        }
    };
}

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