'use strict';

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

var UserReports = require('../../../../../types/UserReports.js');
var utils$3 = require('../../../../../components/_filters/filters-selected/utils.js');
var localsHelper = require('../../../../../utils/localsHelper.js');
var UserStoreItemBuilder = require('../../../../../utils/UserStoreItemBuilder.js');
var utils = require('../../async/dashboard/utils.js');
var utils$4 = require('../../../../../components/_reports/report-heading/report-actions/utils.js');
var utils$2 = require('../../../../../components/bookmark/utils.js');
var utils$1 = require('../../../../../components/_dashboards/dashboard-section/utils.js');

const setAsRecentlyViewed = async ({ req, services, reportName, name, description, reportId, id, userId, filters, }) => {
    const stateData = {
        type: UserReports.ReportType.DASHBOARD,
        reportId,
        id,
        reportName,
        description,
        name,
    };
    const interactiveQueryData = {
        query: req.query,
        querySummary: utils$3.default.getQuerySummary(req.query, filters),
    };
    const recentlyViewedData = new UserStoreItemBuilder.UserStoreItemBuilder(stateData)
        .addInteractiveQuery(interactiveQueryData)
        .addStatus(UserReports.RequestStatus.READY)
        .addTimestamp()
        .addReportUrls(req)
        .build();
    await services.recentlyViewedService.setRecentlyViewed(recentlyViewedData, userId);
};
const renderSyncDashboard = async ({ req, res, services, }) => {
    const { token, csrfToken, dprUser } = localsHelper.default.getValues(res);
    const { reportId, id } = req.params;
    const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
    const { query, filters, reportDefinition, dashboardDefinition, appliedFilters } = await utils.default.getDefinitionData({
        req,
        res,
        services,
    });
    const dashboardData = await services.dashboardService.getSyncDashboard(token, id, reportId, query);
    const flattenedData = dashboardData.flat();
    // Get the dashboard parts
    const dashboardFeatureFlags = res.app.locals['featureFlags'].flags;
    const sections = utils$1.createDashboardSections(dashboardDefinition, flattenedData, query, dashboardFeatureFlags);
    const bookmarkConfig = utils$2.setUpBookmark(res, req, services.bookmarkService);
    await setAsRecentlyViewed({
        req,
        services,
        reportName: reportDefinition.name,
        name: dashboardDefinition.name,
        description: dashboardDefinition.description || reportDefinition.description || '',
        reportId,
        id,
        userId: dprUser.id,
        filters,
    });
    return {
        dashboardData: {
            token,
            id,
            reportId,
            name: dashboardDefinition.name,
            description: dashboardDefinition.description,
            reportName: reportDefinition.name,
            bookmarkConfig,
            csrfToken,
            sections,
            filters,
            type: UserReports.ReportType.DASHBOARD,
            actions: setActions(dashboardDefinition, reportDefinition, fullUrl),
            appliedFilters,
        },
    };
};
const setActions = (definition, summaryDefinition, url) => {
    const { name: reportName } = summaryDefinition;
    const { name } = definition;
    return utils$4.default.getActions({
        share: {
            reportName,
            name,
            url,
        },
        copy: {
            url,
        },
    });
};
var SyncDashboardUtils = {
    renderSyncDashboard,
};

exports.default = SyncDashboardUtils;
exports.renderSyncDashboard = renderSyncDashboard;
exports.setActions = setActions;
//# sourceMappingURL=utils.js.map
