import { ReportType } from '../../../../../types/UserReports.js';
import localsHelper from '../../../../../utils/localsHelper.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';
import { updateLastViewedSync } from '../../utils.js';

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, fields } = 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);
    const stateData = {
        type: ReportType.DASHBOARD,
        reportId,
        id,
        reportName: reportDefinition.name,
        name: reportDefinition.name,
        description: dashboardDefinition.description || reportDefinition.description || '',
    };
    await updateLastViewedSync(req, services, stateData, dprUser.id, fields);
    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
