'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { useFloatingTree } from '@floating-ui/react';
import { useMenuRootContext } from '../root/MenuRootContext.js';
import { useBaseUiId } from '../../utils/useBaseUiId.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useMenuSubmenuTrigger } from './useMenuSubmenuTrigger.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { triggerOpenStateMapping } from '../../utils/popupStateMapping.js';
import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js';
import { mergeProps } from '../../merge-props/index.js';

/**
 * A menu item that opens a submenu.
 * Renders a `<div>` element.
 *
 * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
 */
const MenuSubmenuTrigger = /*#__PURE__*/React.forwardRef(function SubmenuTriggerComponent(props, forwardedRef) {
  const {
    render,
    className,
    label,
    id: idProp,
    ...other
  } = props;
  const id = useBaseUiId(idProp);
  const {
    triggerProps: rootTriggerProps,
    parentContext,
    setTriggerElement,
    allowMouseUpTriggerRef,
    open,
    typingRef,
    disabled
  } = useMenuRootContext();
  if (parentContext === undefined) {
    throw new Error('Base UI: ItemTrigger must be placed in a nested Menu.');
  }
  const {
    activeIndex,
    itemProps
  } = parentContext;
  const item = useCompositeListItem();
  const highlighted = activeIndex === item.index;
  const mergedRef = useForkRef(forwardedRef, item.ref);
  const {
    events: menuEvents
  } = useFloatingTree();
  const {
    getTriggerProps
  } = useMenuSubmenuTrigger({
    id,
    highlighted,
    ref: mergedRef,
    disabled,
    menuEvents,
    setTriggerElement,
    allowMouseUpTriggerRef,
    typingRef
  });
  const state = React.useMemo(() => ({
    disabled,
    highlighted,
    open
  }), [disabled, highlighted, open]);
  const {
    renderElement
  } = useComponentRenderer({
    render: render || 'div',
    className,
    state,
    propGetter: externalProps => mergeProps(rootTriggerProps, itemProps, externalProps, getTriggerProps),
    customStyleHookMapping: triggerOpenStateMapping,
    extraProps: other
  });
  return renderElement();
});
process.env.NODE_ENV !== "production" ? MenuSubmenuTrigger.propTypes /* remove-proptypes */ = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * @ignore
   */
  children: PropTypes.node,
  /**
   * CSS class applied to the element, or a function that
   * returns a class based on the component’s state.
   */
  className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  /**
   * @ignore
   */
  id: PropTypes.string,
  /**
   * Overrides the text label to use when the item is matched during keyboard text navigation.
   */
  label: PropTypes.string,
  /**
   * @ignore
   */
  onClick: PropTypes.func,
  /**
   * Allows you to replace the component’s HTML element
   * with a different tag, or compose it with another component.
   *
   * Accepts a `ReactElement` or a function that returns the element to render.
   */
  render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;
export { MenuSubmenuTrigger };