import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { forwardRef, useCallback } from "react";
import { isElementTextInput } from "../utils/domUtilities.js";
export const DropdownButtonKeyboardWrapper = forwardRef(function DropdownButtonKeyboardWrapper({ onToggle, isOpen, closeOnEscape = true, children, enabled = true, ...divProps }, ref) {
    const handleKeyDown = useCallback((event) => {
        const isInputFocused = isElementTextInput(document.activeElement);
        if (!enabled) {
            return;
        }
        if (event.code === "Enter" || (event.code === "Space" && !isInputFocused)) {
            onToggle();
            event.preventDefault();
            event.stopPropagation();
        }
        if (event.code === "Escape" && isOpen && closeOnEscape) {
            onToggle(false);
            event.preventDefault();
            event.stopPropagation();
        }
        if (event.code === "ArrowUp" && isOpen) {
            onToggle(false);
            event.preventDefault();
            event.stopPropagation();
        }
        if (event.code === "ArrowDown" && !isOpen) {
            onToggle(true);
            event.preventDefault();
            event.stopPropagation();
        }
    }, [isOpen, onToggle, closeOnEscape, enabled]);
    return (_jsx("div", { onKeyDown: handleKeyDown, className: "gd-dropdown-button-wrapper", ...divProps, ref: ref, children: children }));
});
