// (C) 2026 GoodData Corporation
import { objRefToString, } from "@gooddata/sdk-model";
import { computeHydratedRuntimeOverride, workspaceParametersByRef, } from "../../../store/tabs/parameters/parametersHelpers.js";
import { pickTabParametersSource, } from "../../../store/tabs/parameters/parametersState.js";
import { DEFAULT_TAB_ID } from "../../../store/tabs/tabsState.js";
/**
 * Builds the parameter slice entries from the dashboard's persisted parameters and the
 * workspace parameter catalog.
 *
 * - When the persisted entry has an explicit `value`, use it as `runtimeOverride`.
 * - Otherwise, look up the workspace default.
 * - When neither is available (catalog gated off / failed / ref missing), `runtimeOverride`
 *   stays `undefined` so the backend keeps using the parameter's own default at execution time.
 */
export function hydrateParameterEntries(dashboardParameters, workspaceParameters) {
    if (!dashboardParameters || dashboardParameters.length === 0) {
        return [];
    }
    const workspaceByRef = workspaceParametersByRef(workspaceParameters);
    return dashboardParameters.map((parameter) => ({
        parameter,
        runtimeOverride: computeHydratedRuntimeOverride(parameter, workspaceByRef.get(objRefToString(parameter.ref))),
    }));
}
/**
 * Distributes the dashboard's persisted parameters into per-tab hydrated entry lists, applying
 * the V1 → per-tab migration rule (see {@link pickTabParametersSource}). Each tab's parameter
 * list is hydrated against the workspace catalog via {@link hydrateParameterEntries}.
 *
 * For legacy single-tab dashboards (no `tabs[]`), a synthetic tab with `DEFAULT_TAB_ID` is used
 * so the V1 root-level `parameters` migrate transparently.
 *
 * `activeTabOverride` patches `runtimeOverride` on its named tab for refs that match an entry;
 * non-matching refs are silently ignored (see `overrideDefaultParameters` on DashboardConfig).
 */
export function distributeParametersToTabs(tabs, rootParameters, workspaceParameters, activeTabOverride) {
    const effectiveTabs = tabs ?? [
        { localIdentifier: DEFAULT_TAB_ID, title: "", parameters: rootParameters },
    ];
    const result = {};
    for (const tab of effectiveTabs) {
        const source = pickTabParametersSource(tab, effectiveTabs, rootParameters);
        const entries = hydrateParameterEntries(source, workspaceParameters);
        result[tab.localIdentifier] =
            tab.localIdentifier === activeTabOverride?.tabId
                ? applyRuntimeOverrides(entries, activeTabOverride.overrides)
                : entries;
    }
    return result;
}
function applyRuntimeOverrides(entries, overrides) {
    const overrideByRef = new Map(overrides.map((o) => [objRefToString(o.ref), o.value]));
    return entries.map((entry) => {
        const key = objRefToString(entry.parameter.ref);
        return overrideByRef.has(key) ? { ...entry, runtimeOverride: overrideByRef.get(key) } : entry;
    });
}
