import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2023-2026 GoodData Corporation
import { isEmpty } from "lodash-es";
import { areObjRefsEqual, getHierarchyAttributes, isCatalogAttributeHierarchy, } from "@gooddata/sdk-model";
import { AttributeHierarchyDialog } from "@gooddata/sdk-ui-ext";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { useDashboardUserInteraction } from "../../../../../model/react/useDashboardUserInteraction.js";
import { selectAllCatalogAttributeHierarchies } from "../../../../../model/store/catalog/catalogSelectors.js";
import { AttributeHierarchyDropdown } from "./AttributeHierarchyDropdown.js";
import { EmptyAttributeHierarchyInfo } from "./EmptyAttributeHierarchyInfo.js";
import { useAttributeHierarchy } from "./useAttributeHierarchy.js";
export function DrillTargetAttributeHierarchyItem({ config, onSelect, onDeleteInteraction, }) {
    const catalogAttributeHierarchies = useDashboardSelector(selectAllCatalogAttributeHierarchies);
    const userInteraction = useDashboardUserInteraction();
    const { editingAttributeHierarchyRef, shouldDisplayAttributeHierarchyDialog, onDeleteAttributeHierarchy, onSaveAttributeHierarchy, onOpenAttributeHierarchyDialog, onCloseAttributeHierarchyDialog, } = useAttributeHierarchy({ onDeleteInteraction });
    const attributeDescriptor = config.attributes.find((attr) => attr.attributeHeader.localIdentifier === config.originLocalIdentifier);
    const existInHierarchies = catalogAttributeHierarchies.filter((hierarchy) => {
        const attributesRef = getHierarchyAttributes(hierarchy);
        return attributesRef.some((ref) => areObjRefsEqual(ref, attributeDescriptor?.attributeHeader.formOf.ref));
    });
    const shouldShowEmptyMessage = isEmpty(existInHierarchies) && !config.complete;
    const onOpenDialog = (item) => {
        if (!item || isCatalogAttributeHierarchy(item)) {
            onOpenAttributeHierarchyDialog(item);
        }
    };
    return (_jsxs(_Fragment, { children: [shouldShowEmptyMessage ? (_jsx(EmptyAttributeHierarchyInfo, { onOpenAttributeHierarchyDialog: onOpenAttributeHierarchyDialog })) : (_jsx(AttributeHierarchyDropdown, { config: config, onSelect: onSelect, attributeDescriptor: attributeDescriptor, onOpenAttributeHierarchyDialog: onOpenDialog })), shouldDisplayAttributeHierarchyDialog ? (_jsx(AttributeHierarchyDialog, { initialAttributeRef: attributeDescriptor?.attributeHeader.formOf.ref, editingAttributeHierarchy: editingAttributeHierarchyRef.current, onClose: onCloseAttributeHierarchyDialog, onSaveOrUpdateSuccess: onSaveAttributeHierarchy, onDeleteSuccess: onDeleteAttributeHierarchy, onAddAttributeClicked: () => {
                    userInteraction.attributeHierarchiesInteraction("attributeHierarchyAddAttributeClicked");
                }, onCreateHierarchyClicked: () => {
                    userInteraction.attributeHierarchiesInteraction("attributeHierarchyCreateClicked");
                } })) : null] }));
}
//# sourceMappingURL=DrillTargetAttributeHierarchyItem.js.map