"use client";
import { jsxs as Ve, jsx as R } from "react/jsx-runtime";
import { cn as fe } from "../../../lib/utils.js";
import * as r from "react";
import { u as Pe } from "../../../useControlled-Sx-_x7Sz.js";
import { u as G } from "../../../useStableCallback-CudqoENs.js";
import { u as pe } from "../../../useBaseUiId-C8Dq0BQq.js";
import { S as Oe } from "../../../composite-BSpUD7nI.js";
import { A as Fe, C as we } from "../../../CompositeRoot-Dy-5A-Je.js";
import { f as me, a as be, u as ye, c as Me, b as Te } from "../../../LabelableContext-Dzw1DWuj.js";
import { f as ge, N as z, c as _e, E as le, u as he } from "../../../useRenderElement-DNB2vtF1.js";
import { u as Ge } from "../../../useValueChanged-Bissqjjn.js";
import { c as Ke } from "../../../element-DSo91iOi.js";
import { u as ue } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { v as qe, a as Be } from "../../../visuallyHidden-PuyyGHVF.js";
import { c as Se, n as De } from "../../../createBaseUIEventDetails-CqidROtw.js";
import { t as Le, u as ze, a as He } from "../../../useOpenChangeComplete-DkeH9FGJ.js";
import { C as Ue } from "../../../CompositeItem-CniPsjzP.js";
import { u as je } from "../../../FieldItemContext-CF2fRwBC.js";
import { u as Ye } from "../../../useAriaLabelledBy-VklrCmKp.js";
import { u as Je } from "../../../useLabelableId-DF_d91GP.js";
import { s as We } from "../../../serializeValue-BebmSzW3.js";
import { a as $e } from "../../../useButton-JYPKQKj4.js";
const Ce = /* @__PURE__ */ r.createContext({
  legendId: void 0,
  setLegendId: () => {
  },
  disabled: void 0
});
process.env.NODE_ENV !== "production" && (Ce.displayName = "FieldsetRootContext");
function Qe(t = !1) {
  const a = r.useContext(Ce);
  if (!a && !t)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: FieldsetRootContext is missing. Fieldset parts must be placed within <Fieldset.Root>." : ge(86));
  return a;
}
let ce = /* @__PURE__ */ (function(t) {
  return t.checked = "data-checked", t.unchecked = "data-unchecked", t.disabled = "data-disabled", t.readonly = "data-readonly", t.required = "data-required", t.valid = "data-valid", t.invalid = "data-invalid", t.touched = "data-touched", t.dirty = "data-dirty", t.filled = "data-filled", t.focused = "data-focused", t;
})({});
const re = {
  checked(t) {
    return t ? {
      [ce.checked]: ""
    } : {
      [ce.unchecked]: ""
    };
  },
  ...Le,
  ...me
}, ne = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (ne.displayName = "RadioGroupContext");
function Xe() {
  return r.useContext(ne);
}
const ae = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (ae.displayName = "RadioRootContext");
function Ze() {
  const t = r.useContext(ae);
  if (t === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: RadioRootContext is missing. Radio parts must be placed within <Radio.Root>." : ge(52));
  return t;
}
const Re = /* @__PURE__ */ r.forwardRef(function(a, P) {
  const {
    render: K,
    className: q,
    disabled: O = !1,
    readOnly: b = !1,
    required: m = !1,
    "aria-labelledby": v,
    value: d,
    inputRef: F,
    nativeButton: y = !1,
    id: g,
    ...I
  } = a, l = Xe(), {
    disabled: N,
    readOnly: w,
    required: H,
    checkedValue: U,
    touched: j = !1,
    validation: Y,
    name: k
  } = l ?? {}, f = l?.setCheckedValue ?? z, J = l?.setTouched ?? z, M = l?.registerControlRef ?? z, V = l?.registerInputRef ?? z, {
    setDirty: W,
    validityData: $,
    setTouched: Q,
    setFilled: h,
    state: x,
    disabled: X
  } = be(), i = je(), {
    labelId: Z,
    getDescriptionProps: T
  } = ye(), o = X || i.disabled || N || O, s = w || b, E = H || m, c = l ? U === d : d === "", B = r.useMemo(() => We(d), [d]), C = r.useRef(null), p = r.useRef(null), S = G((n) => {
    n && M(n, o);
  }), _ = _e(F, p, V);
  ue(() => {
    p.current?.checked && h(!0);
  }, [h]), ue(() => {
    if (p.current) {
      if (o && c) {
        V(null);
        return;
      }
      C.current && M(C.current, o), V(p.current);
    }
  }, [c, o, M, V]);
  const A = pe(), D = Je({
    id: g,
    implicit: !1,
    controlRef: C
  }), L = y ? void 0 : D, ee = Ye(v, Z, p, !y, L), e = {
    role: "radio",
    "aria-checked": c,
    "aria-required": E || void 0,
    "aria-readonly": s || void 0,
    "aria-labelledby": ee,
    [Fe]: c ? "" : void 0,
    id: y ? D : A,
    onKeyDown(n) {
      n.key === "Enter" && n.preventDefault();
    },
    onClick(n) {
      n.defaultPrevented || o || s || (n.preventDefault(), p.current?.dispatchEvent(new PointerEvent("click", {
        bubbles: !0,
        shiftKey: n.shiftKey,
        ctrlKey: n.ctrlKey,
        altKey: n.altKey,
        metaKey: n.metaKey
      })));
    },
    onFocus(n) {
      n.defaultPrevented || o || s || !j || (p.current?.click(), J(!1));
    }
  }, {
    getButtonProps: u,
    buttonRef: Ee
  } = $e({
    disabled: o,
    native: y
  }), Ie = {
    type: "radio",
    ref: _,
    id: L,
    name: k,
    tabIndex: -1,
    style: k ? qe : Be,
    "aria-hidden": !0,
    ...d !== void 0 ? {
      value: B
    } : le,
    disabled: o,
    checked: c,
    required: E,
    readOnly: s,
    onChange(n) {
      if (n.nativeEvent.defaultPrevented || o || s || d === void 0)
        return;
      const de = Se(De, n.nativeEvent);
      de.isCanceled || (Q(!0), W(d !== $.initialValue), h(!0), f(d, de));
    },
    onFocus() {
      C.current?.focus();
    }
  }, te = r.useMemo(() => ({
    ...x,
    required: E,
    disabled: o,
    readOnly: s,
    checked: c
  }), [x, o, s, c, E]), Ne = te, oe = l !== void 0, ie = [P, C, Ee, S], se = [e, T, Y?.getValidationProps ?? le, I, u], ke = he("span", a, {
    enabled: !oe,
    state: te,
    ref: ie,
    props: se,
    stateAttributesMapping: re
  });
  return /* @__PURE__ */ Ve(ae.Provider, {
    value: Ne,
    children: [oe ? /* @__PURE__ */ R(Ue, {
      tag: "span",
      render: K,
      className: q,
      state: te,
      refs: ie,
      props: se,
      stateAttributesMapping: re
    }) : ke, /* @__PURE__ */ R("input", {
      ...Ie
    })]
  });
});
process.env.NODE_ENV !== "production" && (Re.displayName = "RadioRoot");
const ve = /* @__PURE__ */ r.forwardRef(function(a, P) {
  const {
    render: K,
    className: q,
    keepMounted: O = !1,
    ...b
  } = a, m = Ze(), v = m.checked, {
    mounted: d,
    transitionStatus: F,
    setMounted: y
  } = ze(v), g = {
    ...m,
    transitionStatus: F
  }, I = r.useRef(null), l = O || d, N = he("span", a, {
    ref: [P, I],
    state: g,
    props: b,
    stateAttributesMapping: re
  });
  return He({
    open: v,
    ref: I,
    onComplete() {
      v || y(!1);
    }
  }), l ? N : null;
});
process.env.NODE_ENV !== "production" && (ve.displayName = "RadioIndicator");
const Ae = [Oe], xe = /* @__PURE__ */ r.forwardRef(function(a, P) {
  const {
    render: K,
    className: q,
    disabled: O,
    readOnly: b,
    required: m,
    onValueChange: v,
    value: d,
    defaultValue: F,
    name: y,
    inputRef: g,
    id: I,
    ...l
  } = a, {
    setTouched: N,
    setFocused: w,
    shouldValidateOnChange: H,
    validationMode: U,
    name: j,
    disabled: Y,
    state: k,
    validation: f,
    setDirty: J,
    setFilled: M,
    validityData: V
  } = be(), {
    labelId: W
  } = ye(), {
    clearErrors: $
  } = Me(), Q = Qe(!0), h = Y || O, x = j ?? y, X = pe(I), [i, Z] = Pe({
    controlled: d,
    default: F,
    name: "RadioGroup",
    state: "value"
  }), T = G(v), o = G((e, u) => {
    T(e, u), !u.isCanceled && Z(e);
  }), s = r.useRef(null), E = r.useRef(null), c = r.useRef(null);
  function B(e) {
    let u;
    return g && (typeof g == "function" ? u = g(e) : g.current = e), E.current = e, f.inputRef.current = e, u;
  }
  const C = G((e, u = !1) => {
    if (e) {
      if (u) {
        s.current === e && (s.current = null);
        return;
      }
      s.current == null && (s.current = e);
    }
  }), p = G((e) => {
    if (!e || e.disabled)
      return;
    c.current || (c.current = e);
    const u = E.current;
    if (e.checked || u == null || u.disabled)
      return B(e);
  });
  Te({
    id: X,
    commit: f.commit,
    value: i,
    controlRef: s,
    name: x,
    getValue: () => i ?? null
  }), Ge(i, () => {
    $(x), J(i !== V.initialValue), M(i != null), H() ? f.commit(i) : f.commit(i, !0);
    const e = c.current;
    i == null && e && !e.disabled && B(e);
  });
  const [S, _] = r.useState(!1), A = l["aria-labelledby"] ?? W ?? Q?.legendId, D = {
    ...k,
    disabled: h ?? !1,
    required: m ?? !1,
    readOnly: b ?? !1
  }, L = r.useMemo(() => ({
    ...k,
    checkedValue: i,
    disabled: h,
    validation: f,
    name: x,
    onValueChange: T,
    readOnly: b,
    registerControlRef: C,
    registerInputRef: p,
    required: m,
    setCheckedValue: o,
    setTouched: _,
    touched: S
  }), [i, h, f, k, x, T, b, C, p, m, o, _, S]), ee = {
    role: "radiogroup",
    "aria-required": m || void 0,
    "aria-disabled": h || void 0,
    "aria-readonly": b || void 0,
    "aria-labelledby": A,
    onFocus() {
      w(!0);
    },
    onBlur(e) {
      Ke(e.currentTarget, e.relatedTarget) || (N(!0), w(!1), U === "onBlur" && f.commit(i));
    },
    onKeyDownCapture(e) {
      e.key.startsWith("Arrow") && (N(!0), _(!0), w(!0));
    }
  };
  return /* @__PURE__ */ R(ne.Provider, {
    value: L,
    children: /* @__PURE__ */ R(we, {
      render: K,
      className: q,
      state: D,
      props: [ee, f.getValidationProps, l],
      refs: [P],
      stateAttributesMapping: me,
      enableHomeAndEndKeys: !1,
      modifierKeys: Ae
    })
  });
});
process.env.NODE_ENV !== "production" && (xe.displayName = "RadioGroup");
function vt({ className: t, ...a }) {
  return /* @__PURE__ */ R(
    xe,
    {
      className: fe("grid w-full gap-3", t),
      "data-slot": "radio-group",
      ...a
    }
  );
}
function xt({ className: t, ...a }) {
  return /* @__PURE__ */ R(
    Re,
    {
      className: fe(
        "group/radio-group-item peer relative flex aspect-square size-4 shrink-0 rounded-full border border-input outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary data-checked:border-primary dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
        t
      ),
      "data-slot": "radio-group-item",
      ...a,
      children: /* @__PURE__ */ R(
        ve,
        {
          className: "flex size-4 items-center justify-center",
          "data-slot": "radio-group-indicator",
          children: /* @__PURE__ */ R("span", { className: "absolute top-1/2 left-1/2 size-2.5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-primary shadow-[inset_0_1px_2px_rgba(0,0,0,0.2)]" })
        }
      )
    }
  );
}
export {
  vt as RadioGroup,
  xt as RadioGroupItem
};
