/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-127afd4
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { useContext, useRef } from 'react';
import { FocusRing, useMenuItem } from 'react-aria';
import { RightIcon } from '../../../icons/index.js';
import { mergeProps } from '../../../utils/react/index.js';
import { filterCollectionItemProps } from '../../CollectionItem.js';
import { Item } from '../../content/Item/Item.js';
import { useMenuContext } from './context.js';
import { SubmenuTriggerContext } from './SubmenuTriggerContext.js';
/** @private */
export function MenuItem(props) {
    const { item, state, styles, isVirtualized, onAction, size } = props;
    const { onClose, closeOnSelect } = useMenuContext();
    const { rendered, key, props: itemProps } = item;
    // Check if this item is wrapped in a SubmenuTriggerContext
    const submenuContext = useContext(SubmenuTriggerContext);
    // Filter out service props (react-stately and parent-handled props)
    // All remaining props are safe to pass to Item component
    const filteredItemProps = filterCollectionItemProps(itemProps);
    // Extract specific props that need special handling
    const { hotkeys, mods: itemMods, qa: itemQa } = filteredItemProps;
    const isSelectable = state.selectionManager.selectionMode !== 'none';
    const isDisabledKey = state.disabledKeys.has(key) || submenuContext?.isDisabled;
    const ref = useRef(null);
    // Use the triggerRef from submenu context if present
    const elementRef = (submenuContext?.triggerRef ?? ref);
    const { menuItemProps, labelProps, descriptionProps, keyboardShortcutProps, isFocused, isSelected, isPressed, isDisabled, } = useMenuItem({
        isSelected: state.selectionManager.isSelected(key),
        isDisabled: isDisabledKey,
        'aria-label': item['aria-label'],
        key,
        onClose: submenuContext ? undefined : onClose, // Don't close menu for submenu triggers
        closeOnSelect: submenuContext ? false : closeOnSelect, // Don't close on submenu trigger selection
        isVirtualized,
        onAction: submenuContext?.onAction || onAction,
    }, state, elementRef);
    // Selection indicator will be handled by Item component
    const isVirtualFocused = state.selectionManager.focusedKey === key;
    const mods = {
        ...itemMods,
        focused: isFocused || isVirtualFocused,
        pressed: isPressed,
        selected: isSelected,
        disabled: isDisabled,
        submenu: !!submenuContext,
        menuitem: true,
    };
    return (_jsx(FocusRing, { children: _jsx(Item, { ...mergeProps(menuItemProps, filteredItemProps, {
                'data-popover-trigger': true,
                qa: itemQa ? itemQa : `MenuItem-${key}`,
                mods,
                styles,
                'data-size': size,
                as: 'li',
                labelProps,
                descriptionProps,
                keyboardShortcutProps,
                'aria-haspopup': submenuContext ? 'menu' : undefined,
                'aria-expanded': submenuContext?.isOpen,
                'data-has-submenu': submenuContext ? true : undefined,
                onKeyDown: submenuContext?.onKeyDown
                    ? (e) => {
                        // Call submenu handler first, if it prevents default, don't call the original
                        submenuContext.onKeyDown?.(e);
                        if (!e.defaultPrevented && menuItemProps.onKeyDown) {
                            menuItemProps.onKeyDown(e);
                        }
                    }
                    : menuItemProps.onKeyDown,
                onMouseEnter: submenuContext?.onMouseEnter || menuItemProps.onMouseEnter,
                onMouseLeave: submenuContext?.onMouseLeave || menuItemProps.onMouseLeave,
            }), ref: elementRef, disableActionsFocus: true, rightIcon: submenuContext ? _jsx(RightIcon, {}) : filteredItemProps.rightIcon, defaultTooltipPlacement: "right", isSelected: isSelectable ? isSelected : undefined, isDisabled: isDisabled, size: size === 'small' ? 'small' : 'medium', children: rendered }) }));
}

        