"use client";
'use strict';

var VIIRIBF3 = require('./VIIRIBF3.cjs');
var _62UHHO2X = require('./62UHHO2X.cjs');
var UQQRIHDV = require('./UQQRIHDV.cjs');
var _6VRAQV3D = require('./6VRAQV3D.cjs');
var YV4JVR4I = require('./YV4JVR4I.cjs');
var LMDWO4NN = require('./LMDWO4NN.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var events = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/events.cjs');
var focus = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/focus.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');

var TagName = "div";
function menuHasFocus(baseElement, items, currentTarget) {
  var _a;
  if (!baseElement) return false;
  if (focus.hasFocusWithin(baseElement)) return true;
  const expandedItem = items == null ? void 0 : items.find((item) => {
    var _a2;
    if (item.element === currentTarget) return false;
    return ((_a2 = item.element) == null ? void 0 : _a2.getAttribute("aria-expanded")) === "true";
  });
  const expandedMenuId = (_a = expandedItem == null ? void 0 : expandedItem.element) == null ? void 0 : _a.getAttribute("aria-controls");
  if (!expandedMenuId) return false;
  const doc = DTR5TSDJ.getDocument(baseElement);
  const expandedMenu = doc.getElementById(expandedMenuId);
  if (!expandedMenu) return false;
  if (focus.hasFocusWithin(expandedMenu)) return true;
  return !!expandedMenu.querySelector("[role=menuitem][aria-expanded=true]");
}
var useMenuItem = LMDWO4NN.createHook(
  function useMenuItem2(_a) {
    var _b = _a, {
      store,
      hideOnClick = true,
      preventScrollOnKeyDown = true,
      focusOnHover,
      blurOnHoverEnd
    } = _b, props = _3YLGPPWQ.__objRest(_b, [
      "store",
      "hideOnClick",
      "preventScrollOnKeyDown",
      "focusOnHover",
      "blurOnHoverEnd"
    ]);
    const menuContext = VIIRIBF3.useMenuScopedContext(true);
    const menubarContext = _62UHHO2X.useMenubarScopedContext();
    store = store || menuContext || menubarContext;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "MenuItem must be wrapped in a MenuList, Menu or Menubar component"
    );
    const onClickProp = props.onClick;
    const hideOnClickProp = ABQUS43J.useBooleanEvent(hideOnClick);
    const hideMenu = "hideAll" in store ? store.hideAll : void 0;
    const isWithinMenu = !!hideMenu;
    const onClick = ABQUS43J.useEvent((event) => {
      onClickProp == null ? void 0 : onClickProp(event);
      if (event.defaultPrevented) return;
      if (events.isDownloading(event)) return;
      if (events.isOpeningInNewTab(event)) return;
      if (!hideMenu) return;
      const popupType = event.currentTarget.getAttribute("aria-haspopup");
      if (popupType === "menu") return;
      if (!hideOnClickProp(event)) return;
      hideMenu();
    });
    const contentElement = YV4JVR4I.useStoreState(
      store,
      (state) => "contentElement" in state ? state.contentElement : null
    );
    const role = DTR5TSDJ.getPopupItemRole(contentElement, "menuitem");
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      role
    }, props), {
      onClick
    });
    props = _6VRAQV3D.useCompositeItem(_3YLGPPWQ.__spreadValues({
      store,
      preventScrollOnKeyDown
    }, props));
    props = UQQRIHDV.useCompositeHover(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      store
    }, props), {
      focusOnHover(event) {
        const getFocusOnHover = () => {
          if (typeof focusOnHover === "function") return focusOnHover(event);
          if (focusOnHover != null) return focusOnHover;
          return true;
        };
        if (!store) return false;
        if (!getFocusOnHover()) return false;
        const { baseElement, items } = store.getState();
        if (isWithinMenu) {
          if (event.currentTarget.hasAttribute("aria-expanded")) {
            event.currentTarget.focus();
          }
          return true;
        }
        if (menuHasFocus(baseElement, items, event.currentTarget)) {
          event.currentTarget.focus();
          return true;
        }
        return false;
      },
      blurOnHoverEnd(event) {
        if (typeof blurOnHoverEnd === "function") return blurOnHoverEnd(event);
        if (blurOnHoverEnd != null) return blurOnHoverEnd;
        return isWithinMenu;
      }
    }));
    return props;
  }
);
var MenuItem = LMDWO4NN.memo(
  LMDWO4NN.forwardRef(function MenuItem2(props) {
    const htmlProps = useMenuItem(props);
    return LMDWO4NN.createElement(TagName, htmlProps);
  })
);

exports.MenuItem = MenuItem;
exports.useMenuItem = useMenuItem;
//# sourceMappingURL=2NBKRL7C.cjs.map
