import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2022-2026 GoodData Corporation
import { useMemo } from "react";
import { invariant } from "ts-invariant";
import { dashboardAttributeFilterItemDisplayForm, dashboardAttributeFilterItemTitle, isAttributeMetadataObject, 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 } from "../../../../model/store/catalog/catalogSelectors.js";
import { selectFilterContextAttributeFilterItems } from "../../../../model/store/tabs/filterContext/filterContextSelectors.js";
import { AttributeFilterConfigurationItem } from "./AttributeFilterConfigurationItem.js";
import { getAttributeByDisplayForm } from "./utils.js";
export function AttributeFilterConfiguration({ widget }) {
    const attributeFilters = useDashboardSelector(selectFilterContextAttributeFilterItems);
    const getAttributeFilterDisplayFormFromMap = useAttributeFilterDisplayFormFromMap();
    const attrMap = useDashboardSelector(selectAllCatalogAttributesMap);
    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) {
        return null;
    }
    return (_jsx("div", { className: "s-attribute-filter-configuration", children: attributeFilters.map((filter) => {
            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;
            return (_jsx(AttributeFilterConfigurationItem, { displayFormRef: displayForm.ref, title: dashboardAttributeFilterItemTitle(filter) ?? attributeTitle, widget: widget }, objRefToString(displayForm.ref)));
        }) }));
}
