'use strict';

var VIIRIBF3 = require('../__chunks/VIIRIBF3.cjs');
var MR26YJYP = require('../__chunks/MR26YJYP.cjs');
var T7VMP3TM = require('../__chunks/T7VMP3TM.cjs');
var WMYDSCNQ = require('../__chunks/WMYDSCNQ.cjs');
var XL7CSKGW = require('../__chunks/XL7CSKGW.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 DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var React = require('react');
var jsxRuntime = require('../../../../../../react@18.2.0/node_modules/react/jsx-runtime.cjs');

var TagName = "button";
function getInitialFocus(event, dir) {
  const keyMap = {
    ArrowDown: dir === "bottom" || dir === "top" ? "first" : false,
    ArrowUp: dir === "bottom" || dir === "top" ? "last" : false,
    ArrowRight: dir === "right" ? "first" : false,
    ArrowLeft: dir === "left" ? "first" : false
  };
  return keyMap[event.key];
}
function hasActiveItem(items, excludeElement) {
  return !!(items == null ? void 0 : items.some((item) => {
    if (!item.element) return false;
    if (item.element === excludeElement) return false;
    return item.element.getAttribute("aria-expanded") === "true";
  }));
}
var useMenuButton = LMDWO4NN.createHook(
  function useMenuButton2(_a) {
    var _b = _a, {
      store,
      focusable,
      accessibleWhenDisabled,
      showOnHover
    } = _b, props = _3YLGPPWQ.__objRest(_b, [
      "store",
      "focusable",
      "accessibleWhenDisabled",
      "showOnHover"
    ]);
    const context = VIIRIBF3.useMenuProviderContext();
    store = store || context;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "MenuButton 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;
    const disabled = PBFD2E7P.disabledFromProps(props);
    const showMenu = () => {
      const trigger = ref.current;
      if (!trigger) return;
      store == null ? void 0 : store.setDisclosureElement(trigger);
      store == null ? void 0 : store.setAnchorElement(trigger);
      store == null ? void 0 : store.show();
    };
    const onFocusProp = props.onFocus;
    const onFocus = ABQUS43J.useEvent((event) => {
      onFocusProp == null ? void 0 : onFocusProp(event);
      if (disabled) return;
      if (event.defaultPrevented) return;
      store == null ? void 0 : store.setAutoFocusOnShow(false);
      store == null ? void 0 : store.setActiveId(null);
      if (!parentMenubar) return;
      if (!parentIsMenubar) return;
      const { items } = parentMenubar.getState();
      if (hasActiveItem(items, event.currentTarget)) {
        showMenu();
      }
    });
    const dir = YV4JVR4I.useStoreState(
      store,
      (state) => state.placement.split("-")[0]
    );
    const onKeyDownProp = props.onKeyDown;
    const onKeyDown = ABQUS43J.useEvent((event) => {
      onKeyDownProp == null ? void 0 : onKeyDownProp(event);
      if (disabled) return;
      if (event.defaultPrevented) return;
      const initialFocus = getInitialFocus(event, dir);
      if (initialFocus) {
        event.preventDefault();
        showMenu();
        store == null ? void 0 : store.setAutoFocusOnShow(true);
        store == null ? void 0 : store.setInitialFocus(initialFocus);
      }
    });
    const onClickProp = props.onClick;
    const onClick = ABQUS43J.useEvent((event) => {
      onClickProp == null ? void 0 : onClickProp(event);
      if (event.defaultPrevented) return;
      if (!store) return;
      const isKeyboardClick = !event.detail;
      const { open } = store.getState();
      if (!open || isKeyboardClick) {
        if (!hasParentMenu || isKeyboardClick) {
          store.setAutoFocusOnShow(true);
        }
        store.setInitialFocus(isKeyboardClick ? "first" : "container");
      }
      if (hasParentMenu) {
        showMenu();
      }
    });
    props = ABQUS43J.useWrapElement(
      props,
      (element) => /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(VIIRIBF3.MenuContextProvider, { value: store, children: element }),
      [store]
    );
    if (hasParentMenu) {
      props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
        render: /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(XL7CSKGW.Role.div, { render: props.render })
      });
    }
    const id = ABQUS43J.useId(props.id);
    const parentContentElement = YV4JVR4I.useStoreState(
      (parentMenu == null ? void 0 : parentMenu.combobox) || parentMenu,
      "contentElement"
    );
    const role = hasParentMenu || parentIsMenubar ? DTR5TSDJ.getPopupItemRole(parentContentElement, "menuitem") : void 0;
    const contentElement = store.useState("contentElement");
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      id,
      role,
      "aria-haspopup": DTR5TSDJ.getPopupRole(contentElement, "menu")
    }, props), {
      ref: ABQUS43J.useMergeRefs(ref, props.ref),
      onFocus,
      onKeyDown,
      onClick
    });
    props = MR26YJYP.useHovercardAnchor(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({
      store,
      focusable,
      accessibleWhenDisabled
    }, props), {
      showOnHover: (event) => {
        const getShowOnHover = () => {
          if (typeof showOnHover === "function") return showOnHover(event);
          if (showOnHover != null) return showOnHover;
          if (hasParentMenu) return true;
          if (!parentMenubar) return false;
          const { items } = parentMenubar.getState();
          return parentIsMenubar && hasActiveItem(items);
        };
        const canShowOnHover = getShowOnHover();
        if (!canShowOnHover) return false;
        const parent = parentIsMenubar ? parentMenubar : parentMenu;
        if (!parent) return true;
        parent.setActiveId(event.currentTarget.id);
        return true;
      }
    }));
    props = WMYDSCNQ.usePopoverDisclosure(_3YLGPPWQ.__spreadValues({
      store,
      toggleOnClick: !hasParentMenu,
      focusable,
      accessibleWhenDisabled
    }, props));
    props = T7VMP3TM.useCompositeTypeahead(_3YLGPPWQ.__spreadValues({
      store,
      typeahead: parentIsMenubar
    }, props));
    return props;
  }
);
var MenuButton = LMDWO4NN.forwardRef(function MenuButton2(props) {
  const htmlProps = useMenuButton(props);
  return LMDWO4NN.createElement(TagName, htmlProps);
});

exports.MenuButton = MenuButton;
exports.useMenuButton = useMenuButton;
//# sourceMappingURL=menu-button.cjs.map
