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