import { LoadType, ReportType, RequestStatus } from '../../../../../types/UserReports.js';
import Report from '../../../../../components/_reports/Report.js';
import localsHelper from '../../../../../utils/localsHelper.js';
import { UserStoreItemBuilder } from '../../../../../utils/UserStoreItemBuilder.js';
import SelectedFiltersUtils from '../../../../../components/_filters/filters-selected/utils.js';

const renderReport = async ({ req, res, services }) => {
    const { token, dprUser, definitionsPath } = localsHelper.getValues(res);
    const { id, reportId } = req.params;
    // Get the definition
    const definition = await services.reportingService.getDefinition(token, reportId, id, definitionsPath);
    // Create the report config
    const reportConfig = await new Report(services, res, req, definition, LoadType.SYNC).build();
    // Save the data to redis
    if (reportConfig && Object.keys(reportConfig.renderData).length) {
        const { renderData } = reportConfig;
        await setAsRecentlyViewed({
            req,
            services,
            reportName: renderData.reportName,
            name: renderData.name,
            description: renderData.description || '',
            reportId,
            id,
            userId: dprUser.id,
            filters: renderData.filterData,
        });
    }
    return reportConfig;
};
/**
 * TODO: Consolidate this with the async one.
 *
 * @param {{
 *   req: Request
 *   services: Services
 *   reportName: string
 *   name: string
 *   description: string
 *   reportId: string
 *   id: string
 *   userId: string
 *   filters: FilterValue[]
 * }} {
 *   req,
 *   services,
 *   reportName,
 *   name,
 *   description,
 *   reportId,
 *   id,
 *   userId,
 *   filters,
 * }
 */
const setAsRecentlyViewed = async ({ req, services, reportName, name, description, reportId, id, userId, filters, }) => {
    const stateData = {
        type: ReportType.REPORT,
        reportId,
        id,
        reportName,
        description,
        name,
    };
    const interactiveQueryData = {
        query: req.query,
        querySummary: SelectedFiltersUtils.getQuerySummary(req.query, filters),
    };
    const recentlyViewedData = new UserStoreItemBuilder(stateData)
        .addInteractiveQuery(interactiveQueryData)
        .addStatus(RequestStatus.READY)
        .addTimestamp()
        .addReportUrls(req)
        .build();
    await services.recentlyViewedService?.setRecentlyViewed(recentlyViewedData, userId);
};

export { renderReport };
//# sourceMappingURL=utils.js.map
