// (C) 2007-2026 GoodData Corporation
import { defineMessage } from "react-intl";
import { isFreemiumEdition } from "../utils/featureFlags.js";
/**
 * @internal
 */
export const HEADER_ITEM_ID_DASHBOARDS = defineMessage({ id: "gs.header.dashboards" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_REPORTS = defineMessage({ id: "gs.header.reports" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_KPIS_NEW = defineMessage({ id: "gs.header.kpis.new" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_KPIS = defineMessage({ id: "gs.header.kpis" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_ANALYZE = defineMessage({ id: "gs.header.analyze" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_METRICS = defineMessage({ id: "gs.header.metrics" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_LOAD = defineMessage({ id: "gs.header.load" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_DATA = defineMessage({ id: "gs.header.data" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_MANAGE = defineMessage({ id: "gs.header.manage" }).id;
/**
 * @internal
 */
export const HEADER_ITEM_ID_CATALOG = defineMessage({ id: "gs.header.catalog" }).id;
/**
 * @internal
 */
export function generateHeaderMenuItemsGroups(featureFlags, workspacePermissions, hasAnalyticalDashboards = false, workspaceId, hasNoDataSet = false, backendSupportsDataItem = false, backendSupportsCsvUploader = true, hasMeasures = false, hasManage = true, baseUrl = "") {
    if (!workspaceId) {
        return [];
    }
    const pixelPerfectItemsGroup = [];
    const insightItemsGroup = createInsightsItemsGroup(featureFlags, workspaceId, workspacePermissions, hasAnalyticalDashboards, hasMeasures, backendSupportsCsvUploader, backendSupportsDataItem, hasNoDataSet, baseUrl);
    const manageItemsGroup = createManageItemsGroup(workspacePermissions, workspaceId, hasManage);
    return [pixelPerfectItemsGroup, insightItemsGroup, manageItemsGroup].filter((itemsGroup) => itemsGroup.length > 0);
}
function createManageItemsGroup(workspacePermissions, workspaceId, hasManage) {
    const { canManageMetric } = workspacePermissions;
    const manageItemsGroup = [];
    const manageUrl = manageItemUrl(workspaceId);
    pushConditionally(manageItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_MANAGE, "s-menu-manage", manageUrl), canManageMetric && hasManage);
    return manageItemsGroup;
}
function createInsightsItemsGroup(featureFlags, workspaceId, workspacePermissions, hasAnalyticalDashboards, hasMeasures, backendSupportsCsvUploader, backendSupportsDataItem, hasNoDataSet, baseUrl) {
    const isFreemiumCustomer = isFreemiumEdition(featureFlags.platformEdition);
    const insightItemsGroup = [];
    const kpisUrl = kpisItemUrl(baseUrl, workspaceId, Boolean(featureFlags.enableShellApplication_dashboards));
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_KPIS_NEW, "s-menu-kpis", kpisUrl), canShowKpisItem(workspacePermissions, hasAnalyticalDashboards));
    const analyzeUrl = analyzeItemUrl(baseUrl, workspaceId, featureFlags);
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_ANALYZE, "s-menu-analyze", analyzeUrl), canShowAnalyzeItem(workspacePermissions));
    const measuresUrl = measuresItemUrl(baseUrl, workspaceId, featureFlags);
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_METRICS, "s-menu-metrics", measuresUrl), canShowMetricsItem(hasMeasures, workspacePermissions));
    const dataUrl = dataItemUrl(workspaceId, workspacePermissions, backendSupportsDataItem, hasNoDataSet, baseUrl, featureFlags);
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_DATA, "s-menu-data", dataUrl), canShowDataItem(featureFlags, workspacePermissions));
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_CATALOG, "s-menu-workspace-catalog", catalogItemUrl(workspaceId, featureFlags), true), canShowCatalogItem(featureFlags, workspacePermissions));
    const loadUrl = loadItemUrl(baseUrl, workspaceId);
    pushConditionally(insightItemsGroup, createIHeaderMenuItem(HEADER_ITEM_ID_LOAD, "s-menu-load", loadUrl), canShowLoadItem(featureFlags, workspacePermissions, isFreemiumCustomer, backendSupportsCsvUploader));
    return insightItemsGroup;
}
function createIHeaderMenuItem(key, className, href, isActiveByStartWith) {
    return {
        key,
        className,
        href,
        isActiveByStartWith,
    };
}
function pushConditionally(items, item, cond) {
    if (cond) {
        items.push(item);
    }
}
const withBaseUrl = (baseUrl, uri) => `${baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length - 2) : baseUrl}${uri}`;
function catalogItemUrl(workspaceId, featureFlags) {
    // When the host app and the catalog pluggable-app migration are both on,
    // link directly to the singular host route. Otherwise fall back to the
    // legacy plural URL owned by gdc-home-ui, which re-redirects to the host
    // route on arrival. Skipping the indirection avoids a double redirect when
    // switching from Dashboards/Analyze/Metrics/Data to Catalog. See LX-2426.
    if (featureFlags.enableShellApplication && featureFlags.enableShellApplication_catalog) {
        return `/workspace/${workspaceId}/catalog`;
    }
    return `/workspaces/${workspaceId}/catalog`;
}
function canShowCatalogItem(featureFlags, workspacePermissions) {
    return (!!featureFlags["enableAnalyticalCatalog"] &&
        // WS.Analyze and above can access the catalog
        workspacePermissions.canCreateVisualization);
}
function manageItemUrl(workspaceId) {
    return `/#s=/gdc/workspaces/${workspaceId}|dataPage|`;
}
function measuresItemUrl(baseUrl, workspaceId, featureFlags) {
    if (featureFlags.enableShellApplication_metricEditor) {
        return withBaseUrl(baseUrl, `/workspace/${workspaceId}/metrics`);
    }
    return withBaseUrl(baseUrl, `/metrics/#/${workspaceId}`);
}
function canShowMetricsItem(hasMetrics, workspacePermissions) {
    return Boolean(workspacePermissions.canManageMetric === true && hasMetrics);
}
function kpisItemUrl(baseUrl, workspaceId, useHostRoute) {
    return withBaseUrl(baseUrl, useHostRoute ? `/workspace/${workspaceId}/dashboards` : `/dashboards/#/workspace/${workspaceId}`);
}
function canShowKpisItem(workspacePermissions, hasAnalyticalDashboards) {
    return Boolean(hasAnalyticalDashboards || workspacePermissions.canCreateAnalyticalDashboard === true);
}
function analyzeItemUrl(baseUrl, workspaceId, featureFlags) {
    if (featureFlags.enableShellApplication_analyticalDesigner) {
        return withBaseUrl(baseUrl, `/workspace/${workspaceId}/analyze/#/reportId/edit`);
    }
    return withBaseUrl(baseUrl, `/analyze/#/${workspaceId}/reportId/edit`);
}
function canShowAnalyzeItem(workspacePermissions) {
    return Boolean(workspacePermissions.canCreateVisualization === true);
}
function dataItemUrl(workspaceId, workspacePermissions, backendSupportsDataItem, hasNoDataSet, baseUrl, featureFlags) {
    // When the LDM Modeler runs as a pluggable host application, link to its host route
    // (/workspace/{ws}/modeler) instead of the legacy standalone /modeler/#/ path.
    const ldmModelerUrl = featureFlags.enableShellApplication_ldmModeler
        ? `/workspace/${workspaceId}/modeler`
        : undefined;
    if (backendSupportsDataItem) {
        return withBaseUrl(baseUrl, ldmModelerUrl ?? `/modeler/#/${workspaceId}`);
    }
    if (workspacePermissions.canManageProject && hasNoDataSet) {
        return withBaseUrl(baseUrl, `/admin/connect/#/workspaces/${workspaceId}/datasource`);
    }
    return withBaseUrl(baseUrl, ldmModelerUrl ?? `/modeler/#/workspaces/${workspaceId}`);
}
function canShowDataItem(featureFlags, workspacePermissions) {
    return Boolean(featureFlags.enableDataSection &&
        (workspacePermissions.canInitData || workspacePermissions.canRefreshData));
}
function loadItemUrl(baseUrl, workspaceId) {
    return withBaseUrl(baseUrl, `/data/#/workspaces/${workspaceId}/datasets`);
}
function canShowLoadItem(featureFlags, workspacePermissions, isFreemiumCustomer, backendSupportsCsvUploader) {
    const canAccessLoadCsvPage = workspacePermissions.canUploadNonProductionCSV === true;
    const canShowLoadCsvItem = featureFlags.enableDataSection
        ? !isFreemiumCustomer && canAccessLoadCsvPage
        : canAccessLoadCsvPage;
    return Boolean(canShowLoadCsvItem && backendSupportsCsvUploader);
}
