'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { FloatingTree } from '@floating-ui/react';
import { useDirection } from '../../direction-provider/DirectionContext.js';
import { MenuRootContext, useMenuRootContext } from './MenuRootContext.js';
import { useMenuRoot } from './useMenuRoot.js';
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * Groups all parts of the menu.
 * Doesn’t render its own HTML element.
 *
 * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
 */
const MenuRoot = function MenuRoot(props) {
  const {
    children,
    defaultOpen = false,
    disabled = false,
    closeParentOnEsc = true,
    loop = true,
    modal = true,
    onOpenChange,
    open,
    orientation = 'vertical',
    delay = 100,
    openOnHover: openOnHoverProp,
    actionsRef,
    onOpenChangeComplete
  } = props;
  const direction = useDirection();
  const parentContext = useMenuRootContext(true);
  const nested = parentContext != null;
  const openOnHover = openOnHoverProp ?? nested;
  const typingRef = React.useRef(false);
  const onTypingChange = React.useCallback(nextTyping => {
    typingRef.current = nextTyping;
  }, []);
  const menuRoot = useMenuRoot({
    direction,
    disabled,
    closeParentOnEsc,
    onOpenChange,
    loop,
    defaultOpen,
    open,
    orientation,
    nested,
    openOnHover,
    delay,
    onTypingChange,
    modal,
    actionsRef,
    onOpenChangeComplete
  });
  const context = React.useMemo(() => ({
    ...menuRoot,
    nested,
    parentContext,
    disabled,
    allowMouseUpTriggerRef: parentContext?.allowMouseUpTriggerRef ?? menuRoot.allowMouseUpTriggerRef,
    typingRef,
    modal
  }), [menuRoot, nested, parentContext, disabled, modal]);
  if (!nested) {
    // set up a FloatingTree to provide the context to nested menus
    return /*#__PURE__*/_jsx(FloatingTree, {
      children: /*#__PURE__*/_jsx(MenuRootContext.Provider, {
        value: context,
        children: children
      })
    });
  }
  return /*#__PURE__*/_jsx(MenuRootContext.Provider, {
    value: context,
    children: children
  });
};
process.env.NODE_ENV !== "production" ? MenuRoot.propTypes /* remove-proptypes */ = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * A ref to imperative actions.
   */
  actionsRef: PropTypes.shape({
    current: PropTypes.shape({
      unmount: PropTypes.func.isRequired
    }).isRequired
  }),
  /**
   * @ignore
   */
  children: PropTypes.node,
  /**
   * When in a submenu, determines whether pressing the Escape key
   * closes the entire menu, or only the current child menu.
   * @default true
   */
  closeParentOnEsc: PropTypes.bool,
  /**
   * Whether the menu is initially open.
   *
   * To render a controlled menu, use the `open` prop instead.
   * @default false
   */
  defaultOpen: PropTypes.bool,
  /**
   * How long to wait before the menu may be opened on hover. Specified in milliseconds.
   *
   * Requires the `openOnHover` prop.
   * @default 100
   */
  delay: PropTypes.number,
  /**
   * Whether the component should ignore user interaction.
   * @default false
   */
  disabled: PropTypes.bool,
  /**
   * Whether to loop keyboard focus back to the first item
   * when the end of the list is reached while using the arrow keys.
   * @default true
   */
  loop: PropTypes.bool,
  /**
   * Whether the menu should prevent outside clicks and lock page scroll when open.
   * @default true
   */
  modal: PropTypes.bool,
  /**
   * Event handler called when the menu is opened or closed.
   */
  onOpenChange: PropTypes.func,
  /**
   * Event handler called after any animations complete when the menu is closed.
   */
  onOpenChangeComplete: PropTypes.func,
  /**
   * Whether the menu is currently open.
   */
  open: PropTypes.bool,
  /**
   * Whether the menu should also open when the trigger is hovered.
   *
   * Defaults to `true` for nested menus.
   */
  openOnHover: PropTypes.bool,
  /**
   * The visual orientation of the menu.
   * Controls whether roving focus uses up/down or left/right arrow keys.
   * @default 'vertical'
   */
  orientation: PropTypes.oneOf(['horizontal', 'vertical'])
} : void 0;
export { MenuRoot };