import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2023-2026 GoodData Corporation
import { useEffect, useState } from "react";
import { useIntl } from "react-intl";
import { messages } from "@gooddata/sdk-ui";
import { Dropdown, DropdownButton } from "@gooddata/sdk-ui-kit";
import { AttributeDropdownBody } from "./AttributeDropdownBody.js";
import { useAttributeHierarchyDialog } from "./AttributeHierarchyDialogProvider.js";
export function AttributeDropdown({ rowIndex }) {
    const { formatMessage } = useIntl();
    const { getValidAttributes, onCompleteAttribute } = useAttributeHierarchyDialog();
    const [items, setItems] = useState([]);
    const [isLoading, setLoading] = useState(true);
    const handleSelect = (selectedItem) => {
        onCompleteAttribute(selectedItem, rowIndex);
    };
    useEffect(() => {
        void getValidAttributes(rowIndex).then((attributes) => {
            setItems(attributes);
            setLoading(false);
        });
    }, [getValidAttributes, rowIndex]);
    const chooseAttributeText = formatMessage(messages["hierarchyAttributeDropdown"]);
    return (_jsx(Dropdown, { className: "attribute-hierarchy-attribute-dropdown s-attribute-hierarchy-attribute-dropdown", closeOnParentScroll: true, closeOnMouseDrag: true, closeOnOutsideClick: true, openOnInit: true, renderButton: ({ isOpen, toggleDropdown }) => (_jsx(DropdownButton, { className: "attribute-heirarchy-attribute-dropdown-button s-attribute-heirarchy-attribute-dropdown-button", value: chooseAttributeText, isOpen: isOpen, isSmall: true, onClick: toggleDropdown })), renderBody: ({ closeDropdown }) => {
            return (_jsx(AttributeDropdownBody, { items: items, isLoading: isLoading, onSelect: handleSelect, closeDropdown: closeDropdown }));
        } }));
}
//# sourceMappingURL=AttributeDropdown.js.map