// (C) 2026 GoodData Corporation
import { useMemo } from "react";
import { filterIsEmpty, filterLocalIdentifier, isAttributeFilter, isNoopAllTimeDateFilter, } from "@gooddata/sdk-model";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { useWidgetFilters } from "../../../../model/react/useWidgetFilters.js";
import { selectCrossFilteringItems } from "../../../../model/store/drill/drillSelectors.js";
export function useWidgetAutomationFilters(widget, insight) {
    const widgetFiltersQuery = useWidgetFilters(widget, insight);
    const crossFilteringItems = useDashboardSelector(selectCrossFilteringItems);
    return useMemo(() => {
        const { result: widgetFilters, status: widgetFiltersStatus } = widgetFiltersQuery;
        const sanitizedWidgetFilters = widgetFilters
            ? sanitizeWidgetFilters(widgetFilters, crossFilteringItems)
            : undefined;
        if (widgetFiltersStatus === "success" && sanitizedWidgetFilters) {
            return {
                ...widgetFiltersQuery,
                result: sanitizedWidgetFilters,
            };
        }
        return widgetFiltersQuery;
    }, [crossFilteringItems, widgetFiltersQuery]);
}
function sanitizeWidgetFilters(filters, crossFilteringItems) {
    const withoutCrossFiltering = removeCrossFilteringFilters(filters, crossFilteringItems);
    const withoutAllTimeDateFilters = removeAllTimeDateFilters(withoutCrossFiltering);
    return removeEmptyAttributeFilters(withoutAllTimeDateFilters);
}
function removeCrossFilteringFilters(filters, crossFilteringItems) {
    const crossFilteringFilterLocalIdentifiers = new Set(crossFilteringItems.flatMap((c) => c.filterLocalIdentifiers));
    return filters.filter((filter) => {
        const filterLocalId = filterLocalIdentifier(filter);
        return filterLocalId ? !crossFilteringFilterLocalIdentifiers.has(filterLocalId) : true;
    });
}
function removeEmptyAttributeFilters(filters) {
    return filters.filter((filter) => {
        if (isAttributeFilter(filter)) {
            return !filterIsEmpty(filter);
        }
        return true;
    });
}
function removeAllTimeDateFilters(filters) {
    return filters.filter((filter) => !isNoopAllTimeDateFilter(filter));
}
