import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2023-2025 GoodData Corporation
import { createContext, useContext, useEffect, useMemo, useState } from "react";
import stableStringify from "json-stable-stringify";
import { isEmpty } from "lodash-es";
import { useBackendProvider } from "./useBackendProvider.js";
import { appendEmptyAttribute, convertToCatalogAttributeDataByRefs, findCatalogAttributeByRef, } from "./utils.js";
export const AttributeHierarchyDialogProviderContext = createContext({
    isEditing: false,
    isDirty: false,
    attributes: [],
    title: "",
    isLoading: true,
    setLoading: () => { },
    shouldDisplayDeleteConfirmation: false,
    setDisplayDeleteConfirmation: () => { },
    onClose: () => { },
    onUpdateTitle: () => { },
    onAddEmptyAttribute: () => { },
    onCompleteAttribute: () => { },
    onDeleteAttribute: () => { },
    onSaveAttributeHierarchy: () => { },
    onDeleteAttributeHierarchy: () => { },
    getValidAttributes: () => Promise.resolve([]),
});
export const useAttributeHierarchyDialog = () => useContext(AttributeHierarchyDialogProviderContext);
export function AttributeHierarchyDialogProvider({ initialAttributeRef, editingAttributeHierarchy, children, onClose, onSaveOrUpdateSuccess, onDeleteSuccess, onAddAttributeClicked, onCreateHierarchyClicked, }) {
    const initialTitle = editingAttributeHierarchy?.attributeHierarchy?.title ?? "";
    const [isLoading, setLoading] = useState(true);
    const [title, setTitle] = useState(initialTitle);
    const [initialAttributes, setInitialAttributes] = useState();
    const [attributes, setAttributes] = useState([]);
    const [shouldDisplayDeleteConfirmation, setDisplayDeleteConfirmation] = useState();
    const isEditing = !!editingAttributeHierarchy;
    const isDirty = useMemo(() => {
        return initialTitle !== title || stableStringify(attributes) !== stableStringify(initialAttributes);
    }, [initialTitle, title, attributes, initialAttributes]);
    const { catalogAttributesMap, onSaveAttributeHierarchy, onDeleteAttributeHierarchy, getValidAttributes } = useBackendProvider({
        setLoading,
        title,
        attributes,
        editingAttributeHierarchy,
        onClose,
        onSaveOrUpdateSuccess,
        onDeleteSuccess,
        onCreateHierarchyClicked,
    });
    const handleAddEmptyAttribute = (baseRowIndex) => {
        setAttributes(appendEmptyAttribute(attributes, baseRowIndex));
        if (onAddAttributeClicked) {
            onAddAttributeClicked();
        }
    };
    const handleCompeteAttribute = (selectedAttribute, rowIndex) => {
        const attribute = {
            ...selectedAttribute,
            completed: true,
        };
        const newAttributes = [...attributes];
        newAttributes[rowIndex] = attribute;
        setAttributes(newAttributes);
    };
    const handleDeleteAttribute = (rowIndex) => {
        const newAttributes = [...attributes];
        newAttributes.splice(rowIndex, 1);
        setAttributes(newAttributes);
    };
    useEffect(() => {
        if (editingAttributeHierarchy && !isEmpty(catalogAttributesMap)) {
            const refs = editingAttributeHierarchy.attributeHierarchy.attributes;
            const initialAttributes = convertToCatalogAttributeDataByRefs(catalogAttributesMap, refs).map((attribute) => ({
                ...attribute,
                completed: true,
            }));
            setAttributes(initialAttributes);
            setInitialAttributes(initialAttributes);
            return;
        }
        if (initialAttributeRef && !isEmpty(catalogAttributesMap)) {
            const initialAttribute = findCatalogAttributeByRef(catalogAttributesMap, initialAttributeRef);
            if (initialAttribute) {
                const initialAttributes = [
                    {
                        ...initialAttribute,
                        completed: true,
                    },
                ];
                setAttributes(initialAttributes);
                setInitialAttributes(initialAttributes);
            }
        }
    }, [initialAttributeRef, editingAttributeHierarchy, catalogAttributesMap]);
    return (_jsx(AttributeHierarchyDialogProviderContext.Provider, { value: {
            isEditing,
            isDirty,
            attributes,
            title,
            isLoading,
            setLoading,
            shouldDisplayDeleteConfirmation,
            setDisplayDeleteConfirmation,
            onClose,
            onUpdateTitle: setTitle,
            onAddEmptyAttribute: handleAddEmptyAttribute,
            onCompleteAttribute: handleCompeteAttribute,
            getValidAttributes,
            onDeleteAttribute: handleDeleteAttribute,
            onSaveAttributeHierarchy,
            onDeleteAttributeHierarchy,
        }, children: children }));
}
//# sourceMappingURL=AttributeHierarchyDialogProvider.js.map