import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2020-2026 GoodData Corporation
import { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
import { isDashboardAttributeFilter, isDashboardTextAttributeFilter, } from "@gooddata/sdk-model";
import { useBackendStrict, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { dashboardFilterContextDefinition } from "../../../../../_staging/dashboard/dashboardFilterContext.js";
import { defaultDateFilterConfig } from "../../../../../_staging/dateFilterConfig/defaultConfig.js";
import { safeSerializeObjRef } from "../../../../../_staging/metadata/safeSerializeObjRef.js";
function getTargetDashboardFilterData(dashboard, targetTabLocalIdentifier) {
    const targetTab = targetTabLocalIdentifier
        ? dashboard.tabs?.find((tab) => tab.localIdentifier === targetTabLocalIdentifier)
        : dashboard.tabs?.[0];
    const targetDashboard = targetTab
        ? {
            ...dashboard,
            filterContext: targetTab.filterContext,
            dateFilterConfig: targetTab.dateFilterConfig,
            dateFilterConfigs: targetTab.dateFilterConfigs,
            attributeFilterConfigs: targetTab.attributeFilterConfigs,
        }
        : dashboard;
    const targetDashboardFilters = dashboardFilterContextDefinition(targetDashboard, defaultDateFilterConfig).filters;
    return {
        targetDashboardFilters,
        targetDashboardAttributeFilters: targetDashboardFilters.filter(isDashboardAttributeFilter),
        targetDashboardTextAttributeFilters: targetDashboardFilters.filter(isDashboardTextAttributeFilter),
        targetDashboardAttributeFilterConfigs: targetDashboard.attributeFilterConfigs ?? [],
    };
}
function createEmptyCacheEntry(status) {
    return {
        status,
        targetDashboardFilters: [],
        targetDashboardAttributeFilters: [],
        targetDashboardTextAttributeFilters: [],
        targetDashboardAttributeFilterConfigs: [],
    };
}
export function getTargetDashboardFiltersCacheKey(dashboardRef, dashboardTab) {
    return `${safeSerializeObjRef(dashboardRef)}::${dashboardTab ?? "__default__"}`;
}
const TargetDashboardFiltersContext = createContext(undefined);
TargetDashboardFiltersContext.displayName = "TargetDashboardFiltersContext";
/**
 * @internal
 */
export function TargetDashboardFiltersProvider({ children }) {
    const backend = useBackendStrict();
    const workspace = useWorkspaceStrict();
    const [cache, setCache] = useState({});
    const cacheRef = useRef({});
    const setCacheEntry = useCallback((cacheKey, entry) => {
        const nextCache = {
            ...cacheRef.current,
            [cacheKey]: entry,
        };
        cacheRef.current = nextCache;
        setCache(nextCache);
    }, [setCache]);
    const fetchTargetDashboardFilters = useCallback((dashboardRef, dashboardTab) => {
        const cacheKey = getTargetDashboardFiltersCacheKey(dashboardRef, dashboardTab);
        const cacheEntry = cacheRef.current[cacheKey];
        if (cacheEntry) {
            return;
        }
        setCacheEntry(cacheKey, createEmptyCacheEntry("loading"));
        void backend
            .workspace(workspace)
            .dashboards()
            .getDashboard(dashboardRef)
            .then((targetDashboard) => {
            const targetDashboardFilterData = getTargetDashboardFilterData(targetDashboard, dashboardTab);
            setCacheEntry(cacheKey, {
                status: "success",
                ...targetDashboardFilterData,
            });
        })
            .catch(() => {
            setCacheEntry(cacheKey, createEmptyCacheEntry("error"));
        });
    }, [backend, workspace, setCacheEntry]);
    const value = useMemo(() => ({
        cache,
        fetchTargetDashboardFilters,
    }), [cache, fetchTargetDashboardFilters]);
    return (_jsx(TargetDashboardFiltersContext.Provider, { value: value, children: children }));
}
export function useTargetDashboardFiltersContext() {
    const context = useContext(TargetDashboardFiltersContext);
    if (!context) {
        throw new Error("useTargetDashboardFiltersContext must be used within TargetDashboardFiltersProvider");
    }
    return context;
}
//# sourceMappingURL=TargetDashboardFiltersContext.js.map