// (C) 2023-2026 GoodData Corporation
import { useRef, useState } from "react";
import { createAttributeHierarchyRequested, deleteAttributeHierarchyRequested, } from "../../../../../model/events/attributeHierarchies.js";
import { useDashboardDispatch } from "../../../../../model/react/DashboardStoreProvider.js";
import { useDashboardEventDispatch } from "../../../../../model/react/useDashboardEventDispatch.js";
import { catalogActions } from "../../../../../model/store/catalog/index.js";
export const useAttributeHierarchy = (params) => {
    const { onDeleteInteraction } = params;
    const dispatch = useDashboardDispatch();
    const eventDispatch = useDashboardEventDispatch();
    const [shouldDisplayAttributeHierarchyDialog, setDisplayAttributeHierarchyDialog] = useState(false);
    const editingAttributeHierarchyRef = useRef(undefined);
    const onOpenAttributeHierarchyDialog = (attributeHierarchy) => {
        editingAttributeHierarchyRef.current = attributeHierarchy;
        setDisplayAttributeHierarchyDialog(true);
    };
    const onCloseAttributeHierarchyDialog = () => {
        setDisplayAttributeHierarchyDialog(false);
    };
    const onSaveAttributeHierarchy = (attributeHierarchy) => {
        eventDispatch(createAttributeHierarchyRequested());
        if (editingAttributeHierarchyRef.current && attributeHierarchy) {
            dispatch(catalogActions.updateAttributeHierarchy(attributeHierarchy));
            return;
        }
        if (attributeHierarchy) {
            onDeleteInteraction();
            dispatch(catalogActions.addAttributeHierarchy(attributeHierarchy));
        }
    };
    const onDeleteAttributeHierarchy = () => {
        if (editingAttributeHierarchyRef.current) {
            dispatch(catalogActions.deleteAttributeHierarchy(editingAttributeHierarchyRef.current));
            eventDispatch(deleteAttributeHierarchyRequested());
        }
    };
    return {
        shouldDisplayAttributeHierarchyDialog,
        editingAttributeHierarchyRef,
        onOpenAttributeHierarchyDialog,
        onCloseAttributeHierarchyDialog,
        onSaveAttributeHierarchy,
        onDeleteAttributeHierarchy,
    };
};
//# sourceMappingURL=useAttributeHierarchy.js.map