import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import { isEmpty } from "lodash-es";
import { useIntl } from "react-intl";
import { DashboardAttributeFilterConfigModeValues, dashboardAttributeFilterItemFilterElementsBy, dashboardAttributeFilterItemFilterElementsByDate, dashboardAttributeFilterItemLocalIdentifier, dashboardFilterLocalIdentifier, getSelectedElementsCount, isDashboardArbitraryAttributeFilter, isDashboardAttributeFilter, isDashboardMatchAttributeFilter, } from "@gooddata/sdk-model";
import { AttributeFilterDependencyTooltip, FilterGroup, useDeepEqualRefStablizer, } from "@gooddata/sdk-ui-filters";
import { convertDashboardAttributeFilterElementsUrisToValues } from "../../../_staging/dashboard/legacyFilterConvertors.js";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectBackendCapabilities, selectSupportsElementUris, } from "../../../model/store/backendCapabilities/backendCapabilitiesSelectors.js";
import { selectEnableArbitraryFilterKD, selectEnableMatchFilterKD, selectIsApplyFiltersAllAtOnceEnabledAndSet, } from "../../../model/store/config/configSelectors.js";
import { selectAttributeFilterConfigsDisplayAsLabelMap, selectEffectiveAttributeFiltersModeMap, } from "../../../model/store/tabs/attributeFilterConfigs/attributeFilterConfigsSelectors.js";
import { DefaultDashboardAttributeFilter } from "../attributeFilter/DefaultDashboardAttributeFilter.js";
import { isFilterBarAttributeFilter, } from "./useFiltersWithAddedPlaceholder.js";
/**
 * @alpha
 */
export function DefaultDashboardFilterGroup(props) {
    const { groupItem, onAttributeFilterChanged, DashboardAttributeFilterComponent: CustomDashboardAttributeFilterComponent, } = props;
    const intl = useIntl();
    const supportElementUris = useDashboardSelector(selectSupportsElementUris);
    const isApplyAllAtOnceEnabledAndSet = useDashboardSelector(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    const capabilities = useDashboardSelector(selectBackendCapabilities);
    const attributeFiltersDisplayAsLabelMap = useDashboardSelector(selectAttributeFilterConfigsDisplayAsLabelMap);
    const attributeFiltersModeMap = useDashboardSelector(selectEffectiveAttributeFiltersModeMap);
    const enableArbitraryFilter = useDashboardSelector(selectEnableArbitraryFilterKD);
    const enableMatchFilter = useDashboardSelector(selectEnableMatchFilterKD);
    const getFilterIdentifier = useCallback((filter) => {
        return (dashboardAttributeFilterItemLocalIdentifier(filter.filter) ??
            dashboardFilterLocalIdentifier(filter.filter));
    }, []);
    const hasSelectedElements = useCallback((filter) => {
        if (isDashboardAttributeFilter(filter.filter)) {
            return getSelectedElementsCount(filter.filter) > 0;
        }
        if (isDashboardArbitraryAttributeFilter(filter.filter)) {
            return filter.filter.arbitraryAttributeFilter.values.length > 0;
        }
        if (isDashboardMatchAttributeFilter(filter.filter)) {
            return filter.filter.matchAttributeFilter.literal.length > 0;
        }
        return true;
    }, []);
    const renderFilter = useCallback((filter, AttributeFilterComponent) => {
        const localId = dashboardAttributeFilterItemLocalIdentifier(filter.filter);
        const displayAsLabel = attributeFiltersDisplayAsLabelMap.get(localId);
        const attributeFilterMode = attributeFiltersModeMap.get(localId);
        const DashboardAttributeFilterComponent = CustomDashboardAttributeFilterComponent ?? DefaultDashboardAttributeFilter;
        return (_jsx(DashboardAttributeFilterComponent, { filter: filter.filter, AttributeFilterComponent: AttributeFilterComponent, onFilterChanged: onAttributeFilterChanged, workingFilter: isApplyAllAtOnceEnabledAndSet ? filter.workingFilter : undefined, displayAsLabel: displayAsLabel, readonly: attributeFilterMode === DashboardAttributeFilterConfigModeValues.READONLY, passDropdownButton: false }));
    }, [
        onAttributeFilterChanged,
        isApplyAllAtOnceEnabledAndSet,
        attributeFiltersDisplayAsLabelMap,
        attributeFiltersModeMap,
        CustomDashboardAttributeFilterComponent,
    ]);
    const itemFilters = useMemo(() => {
        return groupItem.filters
            .map((filter) => {
            if (!isFilterBarAttributeFilter(filter)) {
                return undefined;
            }
            // Text filter types (arbitrary, match) don't need URI conversion
            if (!isDashboardAttributeFilter(filter.filter)) {
                return filter;
            }
            if (supportElementUris) {
                return filter;
            }
            return {
                ...filter,
                filter: convertDashboardAttributeFilterElementsUrisToValues(filter.filter),
            };
        })
            .filter((filter) => filter !== undefined)
            .filter((filter) => {
            const localId = dashboardAttributeFilterItemLocalIdentifier(filter.filter);
            return (attributeFiltersModeMap.get(localId) !== DashboardAttributeFilterConfigModeValues.HIDDEN);
        })
            .filter((filter) => {
            // Gate text filter types by feature flags
            if (isDashboardArbitraryAttributeFilter(filter.filter)) {
                return enableArbitraryFilter;
            }
            if (isDashboardMatchAttributeFilter(filter.filter)) {
                return enableMatchFilter;
            }
            return true;
        });
    }, [
        groupItem.filters,
        supportElementUris,
        attributeFiltersModeMap,
        enableArbitraryFilter,
        enableMatchFilter,
    ]);
    const filterDependenciesByLocalIdUnstable = useMemo(() => {
        return new Map(itemFilters.map((filter) => {
            const localId = dashboardAttributeFilterItemLocalIdentifier(filter.filter);
            // Match filter type has no parent filter dependencies by design
            if (isDashboardMatchAttributeFilter(filter.filter)) {
                return [localId, false];
            }
            const filterElementsBy = dashboardAttributeFilterItemFilterElementsBy(filter.filter);
            const filterElementsByDate = dashboardAttributeFilterItemFilterElementsByDate(filter.filter);
            const isDependent = !isEmpty(filterElementsBy) || !isEmpty(filterElementsByDate);
            return [localId, isDependent];
        }));
    }, [itemFilters]);
    const filterDependenciesByLocalId = useDeepEqualRefStablizer(filterDependenciesByLocalIdUnstable);
    const getTitleExtension = useCallback((filterIdentifier, filterTitle) => {
        if (!capabilities.supportsKeepingDependentFiltersSelection) {
            return null;
        }
        const isDependent = filterDependenciesByLocalId.get(filterIdentifier);
        if (!isDependent) {
            return null;
        }
        const filterDependencyIconTooltip = intl.formatMessage({ id: "filter.dependency.icon.tooltip" }, {
            filterTitle,
            strong: (chunks) => _jsx("strong", { children: chunks }),
        });
        return _jsx(AttributeFilterDependencyTooltip, { tooltipContent: filterDependencyIconTooltip });
    }, [capabilities.supportsKeepingDependentFiltersSelection, filterDependenciesByLocalId, intl]);
    return (_jsx(FilterGroup, { title: groupItem.groupConfig.title, filters: itemFilters, getFilterIdentifier: getFilterIdentifier, hasSelectedElements: hasSelectedElements, renderFilter: renderFilter, getTitleExtension: getTitleExtension }));
}
//# sourceMappingURL=DefaultDashboardFilterGroup.js.map