'use client';

import * as React from 'react';
import PropTypes from 'prop-types';
import { SHIFT } from '../composite/composite.js';
import { CompositeRoot } from '../composite/root/CompositeRoot.js';
import { useComponentRenderer } from '../utils/useComponentRenderer.js';
import { useEventCallback } from '../utils/useEventCallback.js';
import { useDirection } from '../direction-provider/DirectionContext.js';
import { useRadioGroup } from './useRadioGroup.js';
import { RadioGroupContext } from './RadioGroupContext.js';
import { useFieldRootContext } from '../field/root/FieldRootContext.js';
import { fieldValidityMapping } from '../field/utils/constants.js';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const MODIFIER_KEYS = [SHIFT];

/**
 * Provides a shared state to a series of radio buttons.
 * Renders a `<div>` element.
 *
 * Documentation: [Base UI Radio Group](https://base-ui.com/react/components/radio)
 */
const RadioGroup = /*#__PURE__*/React.forwardRef(function RadioGroup(props, forwardedRef) {
  const {
    render,
    className,
    disabled: disabledProp,
    readOnly,
    required,
    onValueChange: onValueChangeProp,
    name,
    ...otherProps
  } = props;
  const direction = useDirection();
  const radioGroup = useRadioGroup(props);
  const {
    state: fieldState,
    disabled: fieldDisabled
  } = useFieldRootContext();
  const disabled = fieldDisabled || disabledProp;
  const onValueChange = useEventCallback(onValueChangeProp ?? (() => {}));
  const state = React.useMemo(() => ({
    ...fieldState,
    disabled: disabled ?? false,
    required: required ?? false,
    readOnly: readOnly ?? false
  }), [fieldState, disabled, readOnly, required]);
  const contextValue = React.useMemo(() => ({
    ...fieldState,
    ...radioGroup,
    onValueChange,
    disabled,
    readOnly,
    required
  }), [fieldState, disabled, onValueChange, radioGroup, readOnly, required]);
  const {
    renderElement
  } = useComponentRenderer({
    propGetter: radioGroup.getRootProps,
    render: render ?? 'div',
    ref: forwardedRef,
    className,
    state,
    extraProps: otherProps,
    customStyleHookMapping: fieldValidityMapping
  });
  return /*#__PURE__*/_jsxs(RadioGroupContext.Provider, {
    value: contextValue,
    children: [/*#__PURE__*/_jsx(CompositeRoot, {
      direction: direction,
      enableHomeAndEndKeys: false,
      modifierKeys: MODIFIER_KEYS,
      render: renderElement()
    }), /*#__PURE__*/_jsx("input", {
      ...radioGroup.getInputProps()
    })]
  });
});
process.env.NODE_ENV !== "production" ? RadioGroup.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]),
  /**
   * The uncontrolled value of the radio button that should be initially selected.
   *
   * To render a controlled radio group, use the `value` prop instead.
   */
  defaultValue: PropTypes.any,
  /**
   * Whether the component should ignore user interaction.
   * @default false
   */
  disabled: PropTypes.bool,
  /**
   * Identifies the field when a form is submitted.
   */
  name: PropTypes.string,
  /**
   * Callback fired when the value changes.
   */
  onValueChange: PropTypes.func,
  /**
   * Whether the user should be unable to select a different radio button in the group.
   * @default false
   */
  readOnly: 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]),
  /**
   * Whether the user must choose a value before submitting a form.
   * @default false
   */
  required: PropTypes.bool,
  /**
   * The controlled value of the radio item that should be currently selected.
   *
   * To render an uncontrolled radio group, use the `defaultValue` prop instead.
   */
  value: PropTypes.any
} : void 0;
export { RadioGroup };