import v from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as R, createElement as d, useRef as h, useEffect as _, useState as D } from "react";
import { composeEventHandlers as x } from "../../primitive/dist/index.js";
import { useComposedRefs as C } from "../../react-compose-refs/dist/index.js";
import { createContextScope as G } from "../../react-context/dist/index.js";
import { Primitive as k } from "../../react-primitive/dist/index.js";
import { createRovingFocusGroupScope as P, Root as q, Item as M } from "../../react-roving-focus/dist/index.js";
import { useControllableState as F } from "../../react-use-controllable-state/dist/index.js";
import { useDirection as O } from "../../react-direction/dist/index.js";
import { useSize as V } from "../../react-use-size/dist/index.js";
import { usePrevious as L } from "../../react-use-previous/dist/index.js";
import { Presence as K } from "../../react-presence/dist/index.js";
const g = "Radio", [N, y] = G(g), [B, T] = N(g), U = /* @__PURE__ */ R((r, s) => {
  const { __scopeRadio: e, name: n, checked: o = !1, required: t, disabled: c, value: f = "on", onCheck: i, ...$ } = r, [b, u] = D(null), p = C(
    s,
    (m) => u(m)
  ), a = h(!1), l = b ? !!b.closest("form") : !0;
  return /* @__PURE__ */ d(B, {
    scope: e,
    checked: o,
    disabled: c
  }, /* @__PURE__ */ d(k.button, v({
    type: "button",
    role: "radio",
    "aria-checked": o,
    "data-state": E(o),
    "data-disabled": c ? "" : void 0,
    disabled: c,
    value: f
  }, $, {
    ref: p,
    onClick: x(r.onClick, (m) => {
      o || i == null || i(), l && (a.current = m.isPropagationStopped(), a.current || m.stopPropagation());
    })
  })), l && /* @__PURE__ */ d(j, {
    control: b,
    bubbles: !a.current,
    name: n,
    value: f,
    checked: o,
    required: t,
    disabled: c,
    style: {
      transform: "translateX(-100%)"
    }
  }));
}), z = "RadioIndicator", H = /* @__PURE__ */ R((r, s) => {
  const { __scopeRadio: e, forceMount: n, ...o } = r, t = T(z, e);
  return /* @__PURE__ */ d(K, {
    present: n || t.checked
  }, /* @__PURE__ */ d(k.span, v({
    "data-state": E(t.checked),
    "data-disabled": t.disabled ? "" : void 0
  }, o, {
    ref: s
  })));
}), j = (r) => {
  const { control: s, checked: e, bubbles: n = !0, ...o } = r, t = h(null), c = L(e), f = V(s);
  return _(() => {
    const i = t.current, $ = window.HTMLInputElement.prototype, u = Object.getOwnPropertyDescriptor($, "checked").set;
    if (c !== e && u) {
      const p = new Event("click", {
        bubbles: n
      });
      u.call(i, e), i.dispatchEvent(p);
    }
  }, [
    c,
    e,
    n
  ]), /* @__PURE__ */ d("input", v({
    type: "radio",
    "aria-hidden": !0,
    defaultChecked: e
  }, o, {
    tabIndex: -1,
    ref: t,
    style: {
      ...r.style,
      ...f,
      position: "absolute",
      pointerEvents: "none",
      opacity: 0,
      margin: 0
    }
  }));
};
function E(r) {
  return r ? "checked" : "unchecked";
}
const W = [
  "ArrowUp",
  "ArrowDown",
  "ArrowLeft",
  "ArrowRight"
], S = "RadioGroup", [X, be] = G(S, [
  P,
  y
]), w = P(), I = y(), [Y, J] = X(S), Q = /* @__PURE__ */ R((r, s) => {
  const { __scopeRadioGroup: e, name: n, defaultValue: o, value: t, required: c = !1, disabled: f = !1, orientation: i, dir: $, loop: b = !0, onValueChange: u, ...p } = r, a = w(e), l = O($), [m, A] = F({
    prop: t,
    defaultProp: o,
    onChange: u
  });
  return /* @__PURE__ */ d(Y, {
    scope: e,
    name: n,
    required: c,
    disabled: f,
    value: m,
    onValueChange: A
  }, /* @__PURE__ */ d(q, v({
    asChild: !0
  }, a, {
    orientation: i,
    dir: l,
    loop: b
  }), /* @__PURE__ */ d(k.div, v({
    role: "radiogroup",
    "aria-required": c,
    "aria-orientation": i,
    "data-disabled": f ? "" : void 0,
    dir: l
  }, p, {
    ref: s
  }))));
}), Z = "RadioGroupItem", ee = /* @__PURE__ */ R((r, s) => {
  const { __scopeRadioGroup: e, disabled: n, ...o } = r, t = J(Z, e), c = t.disabled || n, f = w(e), i = I(e), $ = h(null), b = C(s, $), u = t.value === o.value, p = h(!1);
  return _(() => {
    const a = (m) => {
      W.includes(m.key) && (p.current = !0);
    }, l = () => p.current = !1;
    return document.addEventListener("keydown", a), document.addEventListener("keyup", l), () => {
      document.removeEventListener("keydown", a), document.removeEventListener("keyup", l);
    };
  }, []), /* @__PURE__ */ d(M, v({
    asChild: !0
  }, f, {
    focusable: !c,
    active: u
  }), /* @__PURE__ */ d(U, v({
    disabled: c,
    required: t.required,
    checked: u
  }, i, o, {
    name: t.name,
    ref: b,
    onCheck: () => t.onValueChange(o.value),
    onKeyDown: x((a) => {
      a.key === "Enter" && a.preventDefault();
    }),
    onFocus: x(o.onFocus, () => {
      var a;
      p.current && ((a = $.current) === null || a === void 0 || a.click());
    })
  })));
}), oe = /* @__PURE__ */ R((r, s) => {
  const { __scopeRadioGroup: e, ...n } = r, o = I(e);
  return /* @__PURE__ */ d(H, v({}, o, n, {
    ref: s
  }));
}), le = Q, me = ee, ve = oe;
export {
  ve as Indicator,
  me as Item,
  Q as RadioGroup,
  oe as RadioGroupIndicator,
  ee as RadioGroupItem,
  le as Root,
  be as createRadioGroupScope
};
