import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2025-2026 GoodData Corporation
import { useEffect, useMemo, useRef } from "react";
import cx from "classnames";
import { UiAutofocus } from "../UiFocusManager/UiAutofocus.js";
import { typedUiMenuContextStore } from "./context.js";
import { useCustomContentKeyNavigation, useKeyNavigation, useUiMenuContextValue } from "./hooks.js";
import { getContentItem, getSiblingItems } from "./itemUtils.js";
import { b, e } from "./menuBem.js";
function ContentWrapper(props) {
    return (
    // autofocus always first element in the custom content for now
    _jsx(UiAutofocus, { children: _jsx("div", { className: e("content-wrapper"), onKeyDown: props.keyboardNavigationHandler, children: props.children }) }));
}
/**
 * An accessible menu component that can be navigated by keyboard.
 * Usable in a <Dropdown /> component.
 * Should implement https://www.w3.org/WAI/ARIA/apg/patterns/menubar/
 *
 * @internal
 */
export function UiMenu(props) {
    const { size = "medium", dataTestId, minWidth, maxWidth, maxHeight, ariaAttributes, onUnhandledKeyDown, shouldKeyboardActionPreventDefault, shouldKeyboardActionStopPropagation, containerBottomPadding = "none", containerTopPadding = "none", } = props;
    const menuComponentRef = useRef(null);
    const itemsContainerRef = useRef(null);
    const UiMenuContextStore = typedUiMenuContextStore();
    const contextStoreValue = useUiMenuContextValue(props, menuComponentRef, itemsContainerRef);
    const handleKeyDown = useKeyNavigation({
        menuContextValue: contextStoreValue,
        onUnhandledKeyDown,
        shouldKeyboardActionPreventDefault,
        shouldKeyboardActionStopPropagation,
    });
    const handleKeyDownInCustomContent = useCustomContentKeyNavigation({
        menuContextValue: contextStoreValue,
        onUnhandledKeyDown,
        shouldKeyboardActionPreventDefault,
        shouldKeyboardActionStopPropagation,
    });
    const { focusedItem, items, controlType, setControlType, MenuHeader, ItemComponent, Content, makeItemId, shownCustomContentItemId, } = contextStoreValue;
    const focusedId = focusedItem?.id;
    const currentMenuLevelItems = useMemo(() => (focusedId === undefined ? [] : (getSiblingItems(items, focusedId) ?? [])), [items, focusedId]);
    useEffect(() => {
        // Only focus when shownCustomContentItemId becomes undefined (was previously set)
        if (shownCustomContentItemId === undefined && menuComponentRef.current) {
            menuComponentRef.current.focus({ preventScroll: true });
        }
    }, [shownCustomContentItemId, menuComponentRef]);
    const menuDataTestId = typeof dataTestId === "function" ? dataTestId(contextStoreValue) : dataTestId;
    const maxHeightValue = typeof maxHeight === "function" ? maxHeight(contextStoreValue) : maxHeight;
    return (_jsx(UiMenuContextStore, { value: contextStoreValue, children: _jsx("div", { className: cx(b({ controlType, size })), style: { minWidth, maxWidth, maxHeight: maxHeightValue }, onKeyDownCapture: () => setControlType("keyboard"), onMouseMoveCapture: () => setControlType("mouse"), "data-testid": menuDataTestId, children: shownCustomContentItemId ? (_jsx(ContentWrapper, { keyboardNavigationHandler: handleKeyDownInCustomContent, children: _jsx(Content, { item: getContentItem(items, shownCustomContentItemId) }) })) : (_jsxs(_Fragment, { children: [
                    _jsx(MenuHeader, {}), _jsx("div", { className: e("items-container", {
                            "container-bottom-padding": containerBottomPadding,
                            "container-top-padding": containerTopPadding,
                        }), ref: itemsContainerRef, children: _jsx("menu", { className: e("items"), tabIndex: 0, onKeyDown: handleKeyDown, "aria-activedescendant": focusedItem ? makeItemId(focusedItem) : undefined, ...ariaAttributes, role: "menu", ref: menuComponentRef, children: currentMenuLevelItems.map((item, index) => (_jsx(ItemComponent, { item: item }, "id" in item ? item.id : index))) }) })
                ] })) }) }));
}
