'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useSelectItemContext } from '../item/SelectItemContext.js';
import { mergeProps } from '../../merge-props/index.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useTransitionStatus } from '../../utils/useTransitionStatus.js';
import { useOpenChangeComplete } from '../../utils/useOpenChangeComplete.js';

/**
 * Indicates whether the select item is selected.
 * Renders a `<span>` element.
 *
 * Documentation: [Base UI Select](https://base-ui.com/react/components/select)
 */
const SelectItemIndicator = /*#__PURE__*/React.forwardRef(function SelectItemIndicator(props, forwardedRef) {
  const {
    render,
    className,
    keepMounted = false,
    ...otherProps
  } = props;
  const {
    selected
  } = useSelectItemContext();
  const indicatorRef = React.useRef(null);
  const mergedRef = useForkRef(forwardedRef, indicatorRef);
  const {
    mounted,
    transitionStatus,
    setMounted
  } = useTransitionStatus(selected);
  const getItemProps = React.useCallback((externalProps = {}) => mergeProps({
    'aria-hidden': true,
    children: '✔️'
  }, externalProps), []);
  const state = React.useMemo(() => ({
    selected,
    transitionStatus
  }), [selected, transitionStatus]);
  const {
    renderElement
  } = useComponentRenderer({
    propGetter: getItemProps,
    render: render ?? 'span',
    ref: mergedRef,
    className,
    state,
    extraProps: {
      hidden: !mounted,
      ...otherProps
    }
  });
  useOpenChangeComplete({
    open: selected,
    ref: indicatorRef,
    onComplete() {
      if (!selected) {
        setMounted(false);
      }
    }
  });
  const shouldRender = keepMounted || selected;
  if (!shouldRender) {
    return null;
  }
  return renderElement();
});
process.env.NODE_ENV !== "production" ? SelectItemIndicator.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 item is not selected.
   * @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 { SelectItemIndicator };