/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-e634e55 | Cube Dev Team */
import { mergeProps as mergeProps$1 } from "../../../utils/react/mergeProps.js";
import { filterCollectionItemProps } from "../../CollectionItem.js";
import { _Item } from "../../content/Item/Item.js";
import { RightIcon } from "../../../icons/RightIcon.js";
import { useMenuContext } from "./context.js";
import { SubmenuTriggerContext } from "./SubmenuTriggerContext.js";
import { useContext, useRef } from "react";
import { jsx } from "react/jsx-runtime";
import { FocusRing, useMenuItem } from "react-aria";

//#region src/components/actions/Menu/MenuItem.tsx
/** @private */
function MenuItem(props) {
	const { item, state, styles, isVirtualized, onAction, size } = props;
	const { onClose, closeOnSelect } = useMenuContext();
	const { rendered, key, props: itemProps } = item;
	const submenuContext = useContext(SubmenuTriggerContext);
	const filteredItemProps = filterCollectionItemProps(itemProps);
	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);
	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 ? void 0 : onClose,
		closeOnSelect: submenuContext ? false : closeOnSelect,
		isVirtualized,
		onAction: submenuContext?.onAction || onAction
	}, state, elementRef);
	const isVirtualFocused = state.selectionManager.focusedKey === key;
	const mods = {
		...itemMods,
		focused: isFocused || isVirtualFocused,
		pressed: isPressed,
		selected: isSelected,
		disabled: isDisabled,
		submenu: !!submenuContext,
		menuitem: true
	};
	return /* @__PURE__ */ jsx(FocusRing, { children: /* @__PURE__ */ jsx(_Item, {
		...mergeProps$1(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" : void 0,
			"aria-expanded": submenuContext?.isOpen,
			"data-has-submenu": submenuContext ? true : void 0,
			onKeyDown: submenuContext?.onKeyDown ? (e) => {
				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 ? /* @__PURE__ */ jsx(RightIcon, {}) : filteredItemProps.rightIcon,
		defaultTooltipPlacement: "right",
		isSelected: isSelectable ? isSelected : void 0,
		isDisabled,
		size: size === "small" ? "small" : "medium",
		children: rendered
	}) });
}

//#endregion
export { MenuItem };
//# sourceMappingURL=MenuItem.js.map