"use client";
import * as React from "react";
import { useMergedRefs } from "../../../../utils/esm/useMergedRefs.js";
import { useIsoLayoutEffect } from "../../../../utils/esm/useIsoLayoutEffect.js";
import { visuallyHidden } from "../../../../utils/esm/visuallyHidden.js";
import { createChangeEventDetails } from "../../utils/createBaseUIEventDetails.js";
import { stateAttributesMapping } from "../utils/stateAttributesMapping.js";
import { useBaseUiId } from "../../utils/useBaseUiId.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
import { ACTIVE_COMPOSITE_ITEM } from "../../composite/constants.js";
import { CompositeItem } from "../../composite/item/CompositeItem.js";
import { useFieldRootContext } from "../../field/root/FieldRootContext.js";
import { useFieldItemContext } from "../../field/item/FieldItemContext.js";
import { useLabelableContext } from "../../labelable-provider/LabelableContext.js";
import { useLabelableId } from "../../labelable-provider/useLabelableId.js";
import { useRadioGroupContext } from "../../radio-group/RadioGroupContext.js";
import { RadioRootContext } from "./RadioRootContext.js";
import { jsxs, jsx } from "react/jsx-runtime";
import { useButton } from "../../use-button/useButton.js";
import { EMPTY_OBJECT, NOOP } from "../../../../utils/esm/empty.js";
import { none } from "../../utils/reason-parts.js";
const RadioRoot = /* @__PURE__ */ React.forwardRef(function RadioRoot2(componentProps, forwardedRef) {
  const {
    render,
    className,
    disabled: disabledProp = false,
    readOnly: readOnlyProp = false,
    required: requiredProp = false,
    value,
    inputRef: inputRefProp,
    nativeButton = false,
    id: idProp,
    ...elementProps
  } = componentProps;
  const {
    disabled: disabledGroup,
    readOnly: readOnlyGroup,
    required: requiredGroup,
    checkedValue,
    setCheckedValue,
    touched,
    setTouched,
    validation,
    registerControlRef
  } = useRadioGroupContext();
  const {
    setDirty,
    validityData,
    setTouched: setFieldTouched,
    setFilled,
    state: fieldState,
    disabled: fieldDisabled
  } = useFieldRootContext();
  const fieldItemContext = useFieldItemContext();
  const {
    labelId,
    getDescriptionProps
  } = useLabelableContext();
  const disabled = fieldDisabled || fieldItemContext.disabled || disabledGroup || disabledProp;
  const readOnly = readOnlyGroup || readOnlyProp;
  const required = requiredGroup || requiredProp;
  const checked = checkedValue === value;
  const radioRef = React.useRef(null);
  const inputRef = React.useRef(null);
  const mergedInputRef = useMergedRefs(inputRefProp, inputRef);
  useIsoLayoutEffect(() => {
    if (inputRef.current?.checked) {
      setFilled(true);
    }
  }, [setFilled]);
  const id = useBaseUiId();
  const inputId = useLabelableId({
    id: idProp,
    implicit: false,
    controlRef: radioRef
  });
  const rootProps = {
    role: "radio",
    "aria-checked": checked,
    "aria-required": required || void 0,
    "aria-disabled": disabled || void 0,
    "aria-readonly": readOnly || void 0,
    "aria-labelledby": labelId,
    [ACTIVE_COMPOSITE_ITEM]: checked ? "" : void 0,
    id,
    disabled,
    onKeyDown(event) {
      if (event.key === "Enter") {
        event.preventDefault();
      }
    },
    onClick(event) {
      if (event.defaultPrevented || disabled || readOnly) {
        return;
      }
      event.preventDefault();
      inputRef.current?.click();
    },
    onFocus(event) {
      if (event.defaultPrevented || disabled || readOnly || !touched) {
        return;
      }
      inputRef.current?.click();
      setTouched(false);
    }
  };
  const {
    getButtonProps,
    buttonRef
  } = useButton({
    disabled,
    native: nativeButton
  });
  const inputProps = React.useMemo(() => ({
    type: "radio",
    ref: mergedInputRef,
    id: inputId,
    tabIndex: -1,
    style: visuallyHidden,
    "aria-hidden": true,
    disabled,
    checked,
    required,
    readOnly,
    onChange(event) {
      if (event.nativeEvent.defaultPrevented) {
        return;
      }
      if (disabled || readOnly || value === void 0) {
        return;
      }
      const details = createChangeEventDetails(none, event.nativeEvent);
      if (details.isCanceled) {
        return;
      }
      setFieldTouched(true);
      setDirty(value !== validityData.initialValue);
      setFilled(true);
      setCheckedValue(value, details);
    },
    onFocus() {
      radioRef.current?.focus();
    }
  }), [checked, disabled, inputId, mergedInputRef, readOnly, required, setCheckedValue, setDirty, setFieldTouched, setFilled, validityData.initialValue, value]);
  const state = React.useMemo(() => ({
    ...fieldState,
    required,
    disabled,
    readOnly,
    checked
  }), [fieldState, disabled, readOnly, checked, required]);
  const contextValue = React.useMemo(() => state, [state]);
  const isRadioGroup = setCheckedValue !== NOOP;
  const refs = [forwardedRef, registerControlRef, radioRef, buttonRef];
  const props = [rootProps, getDescriptionProps, validation?.getValidationProps ?? EMPTY_OBJECT, elementProps, getButtonProps];
  const element = useRenderElement("span", componentProps, {
    enabled: !isRadioGroup,
    state,
    ref: refs,
    props,
    stateAttributesMapping
  });
  return /* @__PURE__ */ jsxs(RadioRootContext.Provider, {
    value: contextValue,
    children: [isRadioGroup ? /* @__PURE__ */ jsx(CompositeItem, {
      tag: "span",
      render,
      className,
      state,
      refs,
      props,
      stateAttributesMapping
    }) : element, /* @__PURE__ */ jsx("input", {
      ...inputProps
    })]
  });
});
if (process.env.NODE_ENV !== "production") RadioRoot.displayName = "RadioRoot";
export {
  RadioRoot
};
//# sourceMappingURL=RadioRoot.js.map
