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, isAttributeMetadataObject, isDashboardAttributeFilterItem, isDashboardDateFilterWithDimension, 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, } from "../../../../model/store/catalog/catalogSelectors.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 { getAttributeByDisplayForm } from "./utils.js";
export function FilterConfiguration({ widget }) {
    const allFilters = useDashboardSelector(selectFilterContextFilters);
    const getAttributeFilterDisplayFormFromMap = useAttributeFilterDisplayFormFromMap();
    const attrMap = useDashboardSelector(selectAllCatalogAttributesMap);
    const ddsMap = useDashboardSelector(selectAllCatalogDateDatasetsMap);
    const displayAsLabelMap = useDashboardSelector(selectAttributeFilterConfigsDisplayAsLabelMap);
    const draggableFilters = useMemo(() => {
        return allFilters.filter((f) => isDashboardAttributeFilterItem(f) || isDashboardDateFilterWithDimension(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);
    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 {
                return (_jsx(DateFilterConfigurationItem, { dataSetRef: filter.dateFilter.dataSet, widget: widget }, objRefToString(filter.dateFilter.dataSet)));
            }
        }) }));
}
//# sourceMappingURL=FilterConfiguration.js.map