// (C) 2022-2026 GoodData Corporation
import { areObjRefsEqual, getAttributeElementsItems, isDashboardAttributeFilter, } from "@gooddata/sdk-model";
import { useBackendStrict, useCancelablePromise, useWorkspaceStrict } from "@gooddata/sdk-ui";
import { useFiltersNamings } from "../../../_staging/sharedHooks/useFiltersNamings.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectAttributeFilterConfigsOverrides } from "../../../model/store/tabs/attributeFilterConfigs/attributeFilterConfigsSelectors.js";
import { selectDateFilterConfigOverrides } from "../../../model/store/tabs/dateFilterConfig/dateFilterConfigSelectors.js";
import { selectDateFilterConfigsOverrides } from "../../../model/store/tabs/dateFilterConfigs/dateFilterConfigsSelectors.js";
import { selectFilterContextFilters } from "../../../model/store/tabs/filterContext/filterContextSelectors.js";
export function useDashboardRelatedFilters(run) {
    const backend = useBackendStrict();
    const workspaceId = useWorkspaceStrict();
    const dashboardFilters = useDashboardSelector(selectFilterContextFilters);
    const dateFilterConfig = useDashboardSelector(selectDateFilterConfigOverrides);
    const dateFiltersConfig = useDashboardSelector(selectDateFilterConfigsOverrides);
    const attributeFiltersConfig = useDashboardSelector(selectAttributeFilterConfigsOverrides);
    const { result, status } = useCancelablePromise({
        promise: async () => {
            if (!run) {
                return dashboardFilters;
            }
            return Promise.all(dashboardFilters.map((f) => updateLabelElements(backend, workspaceId, attributeFiltersConfig, f)));
        },
    }, [run, dashboardFilters]);
    const filters = useFiltersNamings(result ?? []);
    const all = filters.filter((f) => f && !f.all).map((f) => {
        if (f?.common) {
            return {
                ...f,
                mode: dateFilterConfig?.mode,
            };
        }
        else if (f?.type === "dateFilter") {
            return {
                ...f,
                mode: dateFiltersConfig.find((c) => areObjRefsEqual(c.dateDataSet, f?.dataSet))?.config?.mode,
            };
        }
        return {
            ...f,
            mode: attributeFiltersConfig.find((c) => c.localIdentifier === f?.id)?.mode,
        };
    });
    return {
        isLoading: status === "loading" || status === "pending",
        isError: status === "error",
        isSuccess: status === "success",
        dateFilters: all.filter((f) => f?.type === "dateFilter"),
        attributeFilters: all.filter((f) => f?.type === "attributeFilter"),
    };
}
async function updateLabelElements(backend, workspaceId, attributeFiltersConfig, dashboardFilter) {
    // Skip filters that are not attribute filters
    if (isDashboardAttributeFilter(dashboardFilter)) {
        const config = attributeFiltersConfig.find((c) => c.localIdentifier === dashboardFilter.attributeFilter.localIdentifier);
        const values = getAttributeElementsItems(dashboardFilter.attributeFilter.attributeElements);
        const query = backend
            .workspace(workspaceId)
            .attributes()
            .elements()
            .forDisplayForm(config?.displayAsLabel ?? dashboardFilter.attributeFilter.displayForm)
            .withOptions({
            elements: {
                values,
            },
            filterByPrimaryLabel: true,
        });
        const data = await query.query();
        const all = await data.all();
        return {
            ...dashboardFilter,
            attributeFilter: {
                ...dashboardFilter.attributeFilter,
                attributeElements: {
                    values: all.map((a) => a.title),
                },
            },
        };
    }
    return dashboardFilter;
}
//# sourceMappingURL=useDashboardRelatedFilters.js.map