"use client";
'use strict';

var MDUNURGC = require('../__chunks/MDUNURGC.cjs');
var ZFX5QU3H = require('../__chunks/ZFX5QU3H.cjs');
var VIIRIBF3 = require('../__chunks/VIIRIBF3.cjs');
var DVTPF4J4 = require('../__chunks/DVTPF4J4.cjs');
var YV4JVR4I = require('../__chunks/YV4JVR4I.cjs');
var LMDWO4NN = require('../__chunks/LMDWO4NN.cjs');
var ABQUS43J = require('../__chunks/ABQUS43J.cjs');
var _3YLGPPWQ = require('../__chunks/3YLGPPWQ.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 React = require('react');

var TagName = "div";
var useMenu = LMDWO4NN.createHook(function useMenu2(_a) {
  var _b = _a, {
    store,
    modal: modalProp = false,
    portal = !!modalProp,
    hideOnEscape = true,
    autoFocusOnShow = true,
    hideOnHoverOutside,
    alwaysVisible
  } = _b, props = _3YLGPPWQ.__objRest(_b, [
    "store",
    "modal",
    "portal",
    "hideOnEscape",
    "autoFocusOnShow",
    "hideOnHoverOutside",
    "alwaysVisible"
  ]);
  const context = VIIRIBF3.useMenuProviderContext();
  store = store || context;
  PBFD2E7P.invariant(
    store,
    process.env.NODE_ENV !== "production" && "Menu must receive a `store` prop or be wrapped in a MenuProvider component."
  );
  const ref = React.useRef(null);
  const parentMenu = store.parent;
  const parentMenubar = store.menubar;
  const hasParentMenu = !!parentMenu;
  const parentIsMenubar = !!parentMenubar && !hasParentMenu;
  props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    ref: ABQUS43J.useMergeRefs(ref, props.ref)
  });
  const _a2 = MDUNURGC.useMenuList(_3YLGPPWQ.__spreadValues({
    store,
    alwaysVisible
  }, props)), { "aria-labelledby": ariaLabelledBy } = _a2, menuListProps = _3YLGPPWQ.__objRest(_a2, ["aria-labelledby"]);
  props = menuListProps;
  const [initialFocusRef, setInitialFocusRef] = React.useState();
  const autoFocusOnShowState = store.useState("autoFocusOnShow");
  const initialFocus = store.useState("initialFocus");
  const baseElement = store.useState("baseElement");
  const items = store.useState("renderedItems");
  React.useEffect(() => {
    let cleaning = false;
    setInitialFocusRef((prevInitialFocusRef) => {
      var _a3, _b2, _c;
      if (cleaning) return;
      if (!autoFocusOnShowState) return;
      if ((_a3 = prevInitialFocusRef == null ? void 0 : prevInitialFocusRef.current) == null ? void 0 : _a3.isConnected) return prevInitialFocusRef;
      const ref2 = React.createRef();
      switch (initialFocus) {
        // TODO: Refactor
        case "first":
          ref2.current = ((_b2 = items.find((item) => !item.disabled && item.element)) == null ? void 0 : _b2.element) || null;
          break;
        case "last":
          ref2.current = ((_c = [...items].reverse().find((item) => !item.disabled && item.element)) == null ? void 0 : _c.element) || null;
          break;
        default:
          ref2.current = baseElement;
      }
      return ref2;
    });
    return () => {
      cleaning = true;
    };
  }, [store, autoFocusOnShowState, initialFocus, items, baseElement]);
  const modal = hasParentMenu ? false : modalProp;
  const mayAutoFocusOnShow = !!autoFocusOnShow;
  const canAutoFocusOnShow = !!initialFocusRef || !!props.initialFocus || !!modal;
  const contentElement = YV4JVR4I.useStoreState(
    store.combobox || store,
    "contentElement"
  );
  const parentContentElement = YV4JVR4I.useStoreState(
    (parentMenu == null ? void 0 : parentMenu.combobox) || parentMenu,
    "contentElement"
  );
  const preserveTabOrderAnchor = React.useMemo(() => {
    if (!parentContentElement) return;
    if (!contentElement) return;
    const role = contentElement.getAttribute("role");
    const parentRole = parentContentElement.getAttribute("role");
    const parentIsMenuOrMenubar = parentRole === "menu" || parentRole === "menubar";
    if (parentIsMenuOrMenubar && role === "menu") return;
    return parentContentElement;
  }, [contentElement, parentContentElement]);
  if (preserveTabOrderAnchor !== void 0) {
    props = _3YLGPPWQ.__spreadValues({
      preserveTabOrderAnchor
    }, props);
  }
  props = ZFX5QU3H.useHovercard(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
    store,
    alwaysVisible,
    initialFocus: initialFocusRef,
    autoFocusOnShow: mayAutoFocusOnShow ? canAutoFocusOnShow && autoFocusOnShow : autoFocusOnShowState || !!modal
  }, props), {
    hideOnEscape(event) {
      if (PBFD2E7P.isFalsyBooleanCallback(hideOnEscape, event)) return false;
      store == null ? void 0 : store.hideAll();
      return true;
    },
    hideOnHoverOutside(event) {
      const disclosureElement = store == null ? void 0 : store.getState().disclosureElement;
      const getHideOnHoverOutside = () => {
        if (typeof hideOnHoverOutside === "function") {
          return hideOnHoverOutside(event);
        }
        if (hideOnHoverOutside != null) return hideOnHoverOutside;
        if (hasParentMenu) return true;
        if (!parentIsMenubar) return false;
        if (!disclosureElement) return true;
        if (focus.hasFocusWithin(disclosureElement)) return false;
        return true;
      };
      if (!getHideOnHoverOutside()) return false;
      if (event.defaultPrevented) return true;
      if (!hasParentMenu) return true;
      if (!disclosureElement) return true;
      events.fireEvent(disclosureElement, "mouseout", event);
      if (!focus.hasFocusWithin(disclosureElement)) return true;
      requestAnimationFrame(() => {
        if (focus.hasFocusWithin(disclosureElement)) return;
        store == null ? void 0 : store.hide();
      });
      return false;
    },
    modal,
    portal,
    backdrop: hasParentMenu ? false : props.backdrop
  }));
  props = _3YLGPPWQ.__spreadValues({
    "aria-labelledby": ariaLabelledBy
  }, props);
  return props;
});
var Menu = DVTPF4J4.createDialogComponent(
  LMDWO4NN.forwardRef(function Menu2(props) {
    const htmlProps = useMenu(props);
    return LMDWO4NN.createElement(TagName, htmlProps);
  }),
  VIIRIBF3.useMenuProviderContext
);

exports.Menu = Menu;
exports.useMenu = useMenu;
//# sourceMappingURL=menu.cjs.map
