'use strict';

var UserReports = require('../../../../../types/UserReports.js');
var Report = require('../../../../../components/_reports/Report.js');
var localsHelper = require('../../../../../utils/localsHelper.js');
var utils = require('../../utils.js');

const renderReport = async ({ req, res, services }) => {
    const { token, dprUser, definitionsPath } = localsHelper.default.getValues(res);
    const { id, tableId, reportId } = req.params;
    const requestData = await services.requestedReportService.getReportByTableId(tableId, dprUser.id);
    // get pre-filter query data required by getDefinition
    const queryData = requestData?.query?.data;
    // Get the definition
    const definition = res.locals['definition'] ??
        (await services.reportingService.getDefinition(token, reportId, id, definitionsPath, queryData));
    // Create the report config
    const reportConfig = await new Report(services, res, req, definition, UserReports.LoadType.ASYNC, requestData).build();
    const { renderData } = reportConfig;
    if (renderData && requestData && Object.keys(requestData).length) {
        // Save the data to redis
        await utils.updateLastViewedAsync(req, services, requestData, dprUser.id, renderData.fields || []);
    }
    return reportConfig;
};

exports.renderReport = renderReport;
//# sourceMappingURL=utils.js.map
