'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { useMenuRadioItemContext } from '../radio-item/MenuRadioItemContext.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { itemMapping } from '../utils/styleHookMapping.js';
import { useTransitionStatus } from '../../utils/useTransitionStatus.js';
import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete.js';
import { useForkRef } from '../../utils/useForkRef.js';

/**
 * Indicates whether the radio item is selected.
 * Renders a `<div>` element.
 *
 * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
 */
const MenuRadioItemIndicator = /*#__PURE__*/React.forwardRef(function MenuRadioItemIndicator(props, forwardedRef) {
  const {
    render,
    className,
    keepMounted = false,
    ...other
  } = props;
  const item = useMenuRadioItemContext();
  const indicatorRef = React.useRef(null);
  const mergedRef = useForkRef(forwardedRef, indicatorRef);
  const {
    transitionStatus,
    setMounted
  } = useTransitionStatus(item.checked);
  useOpenChangeComplete({
    open: item.checked,
    ref: indicatorRef,
    onComplete() {
      if (!item.checked) {
        setMounted(false);
      }
    }
  });
  const state = React.useMemo(() => ({
    checked: item.checked,
    disabled: item.disabled,
    highlighted: item.highlighted,
    transitionStatus
  }), [item.checked, item.disabled, item.highlighted, transitionStatus]);
  const {
    renderElement
  } = useComponentRenderer({
    render: render || 'span',
    className,
    state,
    customStyleHookMapping: itemMapping,
    extraProps: {
      'aria-hidden': true,
      ...other
    },
    ref: mergedRef
  });
  const shouldRender = keepMounted || item.checked;
  if (!shouldRender) {
    return null;
  }
  return renderElement();
});
process.env.NODE_ENV !== "production" ? MenuRadioItemIndicator.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]),
  /**
   * Whether to keep the HTML element in the DOM when the radio item is inactive.
   * @default false
   */
  keepMounted: PropTypes.bool,
  /**
   * 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 { MenuRadioItemIndicator };