// (C) 2021-2026 GoodData Corporation
import { useMemo } from "react";
import { invariant } from "ts-invariant";
import { dashboardAttributeFilterItemFilterElementsBy, dashboardAttributeFilterItemLocalIdentifier, } from "@gooddata/sdk-model";
import { dashboardAttributeFilterItemToAttributeFilter } from "../../../converters/filterConverters.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectSupportsSettingConnectingAttributes } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectIsApplyFiltersAllAtOnceEnabledAndSet } from "../../../model/store/config/configSelectors.js";
import { selectFilterContextAttributeFilterItems, selectFilterContextAttributeFilterItemsForTab, selectWorkingFilterContextAttributeFilterItems, selectWorkingFilterContextAttributeFilterItemsForTab, } from "../../../model/store/tabs/filterContext/filterContextSelectors.js";
/**
 * Returns parent filtering input props for {@link @gooddata/sdk-ui-filters#AttributeFilter} for particular dashboard attribute filter.
 *
 * @param filter - dashboard filter to get the parent filter-related data
 * @param tabId - optional tab identifier to read filter context from a specific tab instead of the active one
 *
 * @public
 */
export const useParentFilters = (filter, tabId) => {
    const isApplyAllAtOnceEnabledAndSet = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    // Use tab-specific selectors when tabId is provided
    const allAppliedAttributeFilterItems = useDashboardSelector(tabId
        ? selectFilterContextAttributeFilterItemsForTab(tabId)
        : selectFilterContextAttributeFilterItems);
    const allWorkingAttributeFilterItems = useDashboardSelector(tabId
        ? selectWorkingFilterContextAttributeFilterItemsForTab(tabId)
        : selectWorkingFilterContextAttributeFilterItems);
    const allAttributeFilterItems = isApplyAllAtOnceEnabledAndSet
        ? allWorkingAttributeFilterItems
        : allAppliedAttributeFilterItems;
    const supportsSettingConnectingAttributes = useDashboardSelector(selectSupportsSettingConnectingAttributes);
    const filterElementsBy = dashboardAttributeFilterItemFilterElementsBy(filter);
    const parentFiltersData = useMemo(() => {
        if (!filterElementsBy) {
            return undefined;
        }
        return filterElementsBy.map((parent) => {
            const matchingFilter = allAttributeFilterItems.find((f) => dashboardAttributeFilterItemLocalIdentifier(f) === parent.filterLocalIdentifier);
            invariant(matchingFilter); // if this blows up, the state is inconsistent
            return { filter: matchingFilter, over: parent.over.attributes[0] };
        });
    }, [allAttributeFilterItems, filterElementsBy]);
    const parentFilters = useMemo(() => {
        return parentFiltersData?.map((item) => dashboardAttributeFilterItemToAttributeFilter(item.filter));
    }, [parentFiltersData]);
    const parentOverLookup = useMemo(() => {
        // no parents -> no need for the lookup function
        // no support for connecting attributes -> no need for the lookup function
        if (!parentFiltersData?.length || !supportsSettingConnectingAttributes) {
            return undefined;
        }
        return (_parentFilter, index) => parentFiltersData[index].over;
    }, [parentFiltersData, supportsSettingConnectingAttributes]);
    return {
        parentFilters,
        parentFilterOverAttribute: parentOverLookup,
    };
};
