import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useMemo } from "react";
import { invariant } from "ts-invariant";
import { dashboardAttributeFilterItemDisplayForm, dashboardAttributeFilterItemLocalIdentifier, dashboardAttributeFilterItemTitle, insightFilters, isAttributeMetadataObject, isDashboardAttributeFilterItem, isDashboardDateFilterWithDimension, isDashboardMeasureValueFilter, isInsightWidget, isRankingFilter, objRefToString, } from "@gooddata/sdk-model";
import { useAttributeFilterDisplayFormFromMap } from "../../../../_staging/sharedHooks/useAttributeFilterDisplayFormFromMap.js";
import { useAttributes } from "../../../../_staging/sharedHooks/useAttributes.js";
import { useDashboardSelector } from "../../../../model/react/DashboardStoreProvider.js";
import { selectAllCatalogAttributesMap, selectAllCatalogDateDatasetsMap, selectAllCatalogMeasuresMap, selectCatalogIsLoaded, } from "../../../../model/store/catalog/catalogSelectors.js";
import { selectInsightByRef } from "../../../../model/store/insights/insightsSelectors.js";
import { selectAttributeFilterConfigsDisplayAsLabelMap } from "../../../../model/store/tabs/attributeFilterConfigs/attributeFilterConfigsSelectors.js";
import { selectFilterContextFilters } from "../../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { AttributeFilterConfigurationItem } from "./AttributeFilterConfigurationItem.js";
import { DateFilterConfigurationItem } from "./DateFilterConfigurationItem.js";
import { MeasureValueFilterConfigurationItem } from "./MeasureValueFilterConfigurationItem.js";
import { useMeasureValueFilterCompatibility } from "./useMeasureValueFilterCompatibility.js";
import { getAttributeByDisplayForm } from "./utils.js";
export function FilterConfiguration({ widget }) {
    const allFilters = useDashboardSelector(selectFilterContextFilters);
    const getAttributeFilterDisplayFormFromMap = useAttributeFilterDisplayFormFromMap();
    const attrMap = useDashboardSelector(selectAllCatalogAttributesMap);
    const ddsMap = useDashboardSelector(selectAllCatalogDateDatasetsMap);
    const measureMap = useDashboardSelector(selectAllCatalogMeasuresMap);
    const isCatalogLoaded = useDashboardSelector(selectCatalogIsLoaded);
    const displayAsLabelMap = useDashboardSelector(selectAttributeFilterConfigsDisplayAsLabelMap);
    const draggableFilters = useMemo(() => {
        return allFilters.filter((f) => isDashboardAttributeFilterItem(f) ||
            isDashboardDateFilterWithDimension(f) ||
            isDashboardMeasureValueFilter(f));
    }, [allFilters]);
    const attributeFilters = useMemo(() => {
        return draggableFilters.filter(isDashboardAttributeFilterItem);
    }, [draggableFilters]);
    const displayForms = useMemo(() => {
        return attributeFilters.map((filter) => dashboardAttributeFilterItemDisplayForm(filter));
    }, [attributeFilters]);
    const { attributes, attributesLoading } = useAttributes(displayForms);
    const measureValueFilters = useMemo(() => draggableFilters.filter(isDashboardMeasureValueFilter), [draggableFilters]);
    const insight = useDashboardSelector(selectInsightByRef(isInsightWidget(widget) ? widget.insight : undefined));
    const { isCompatible } = useMeasureValueFilterCompatibility(insight, measureValueFilters);
    // Backend rejects executions that combine MVFs with ranking filters. When the widget's
    // insight already has a ranking filter, disable every dashboard MVF row and explain why.
    const isBlockedByRankingFilter = useMemo(() => (insight ? insightFilters(insight).some(isRankingFilter) : false), [insight]);
    if (attributesLoading) {
        return _jsx("span", { className: "gd-spinner small s-attribute-filter-configuration-loading" });
    }
    if (!attributes && ddsMap.size) {
        return null;
    }
    return (_jsx("div", { className: "s-attribute-filter-configuration", children: draggableFilters.map((filter) => {
            if (isDashboardAttributeFilterItem(filter)) {
                if (!attributes) {
                    return null;
                }
                const filterDisplayForm = dashboardAttributeFilterItemDisplayForm(filter);
                const displayForm = getAttributeFilterDisplayFormFromMap(filterDisplayForm);
                invariant(displayForm, "Inconsistent state in AttributeFilterConfiguration");
                const attributeByDisplayForm = getAttributeByDisplayForm(attributes, displayForm.attribute);
                const attribute = attrMap.get(displayForm.attribute) || attributeByDisplayForm;
                invariant(attribute, "Inconsistent state in AttributeFilterConfiguration");
                const attributeTitle = isAttributeMetadataObject(attribute)
                    ? attribute.title
                    : attribute.attribute.title;
                const localIdentifier = dashboardAttributeFilterItemLocalIdentifier(filter);
                const displayAsLabel = localIdentifier
                    ? displayAsLabelMap.get(localIdentifier)
                    : undefined;
                return (_jsx(AttributeFilterConfigurationItem, { displayFormRef: displayForm.ref, displayAsLabel: displayAsLabel, title: dashboardAttributeFilterItemTitle(filter) ?? attributeTitle, widget: widget }, objRefToString(displayForm.ref)));
            }
            else if (isDashboardDateFilterWithDimension(filter)) {
                return (_jsx(DateFilterConfigurationItem, { dataSetRef: filter.dateFilter.dataSet, widget: widget }, objRefToString(filter.dateFilter.dataSet)));
            }
            else if (isDashboardMeasureValueFilter(filter)) {
                const { measure, localIdentifier, title } = filter.dashboardMeasureValueFilter;
                const catalogMeasure = measureMap.get(measure);
                const isMissingMeasure = isCatalogLoaded && !catalogMeasure;
                const measureTitle = title ?? catalogMeasure?.measure.title ?? objRefToString(measure);
                return (_jsx(MeasureValueFilterConfigurationItem, { measureRef: measure, title: measureTitle, widget: widget, isCompatible: isCompatible(measure), isBlockedByRankingFilter: isBlockedByRankingFilter, isMissingMeasure: isMissingMeasure }, localIdentifier));
            }
            else {
                console.warn("Unsupported filter type in FilterConfiguration.", filter);
                return null;
            }
        }) }));
}
