import { ReportType, RequestStatus } from '../../../../../types/UserReports.js';
import SelectedFiltersUtils from '../../../../../components/_filters/filters-selected/utils.js';
import localsHelper from '../../../../../utils/localsHelper.js';
import { UserStoreItemBuilder } from '../../../../../utils/UserStoreItemBuilder.js';
import DashboardUtils from '../../async/dashboard/utils.js';
import ReportActionsUtils from '../../../../../components/_reports/report-heading/report-actions/utils.js';
import { setUpBookmark } from '../../../../../components/bookmark/utils.js';
import { createDashboardSections } from '../../../../../components/_dashboards/dashboard-section/utils.js';

const setAsRecentlyViewed = async ({ req, services, reportName, name, description, reportId, id, userId, filters, }) => {
    const stateData = {
        type: ReportType.DASHBOARD,
        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);
};
const renderSyncDashboard = async ({ req, res, services, }) => {
    const { token, csrfToken, dprUser } = localsHelper.getValues(res);
    const { reportId, id } = req.params;
    const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
    const { query, filters, reportDefinition, dashboardDefinition, appliedFilters } = await DashboardUtils.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 = createDashboardSections(dashboardDefinition, flattenedData, query, dashboardFeatureFlags);
    const bookmarkConfig = 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: ReportType.DASHBOARD,
            actions: setActions(dashboardDefinition, reportDefinition, fullUrl),
            appliedFilters,
        },
    };
};
const setActions = (definition, summaryDefinition, url) => {
    const { name: reportName } = summaryDefinition;
    const { name } = definition;
    return ReportActionsUtils.getActions({
        share: {
            reportName,
            name,
            url,
        },
        copy: {
            url,
        },
    });
};
var SyncDashboardUtils = {
    renderSyncDashboard,
};

export { SyncDashboardUtils as default, renderSyncDashboard, setActions };
//# sourceMappingURL=utils.js.map
