import { o as we } from "./rolldown-runtime.js";
import { i as qe, t as Ge } from "./vendor/react.js";
import { i as A, l as Te, o as je, r as Le, s as re, t as ae, u as Ne } from "./useRenderElement.js";
import { n as He, t as ee } from "./useIsoLayoutEffect.js";
import { t as We } from "./useButton.js";
import { a as $e, n as Je, t as Ue } from "./useTransitionStatus.js";
import { t as te } from "./useBaseUiId.js";
import { g as Xe, t as Ye } from "./createBaseUIEventDetails.js";
import { n as ze, t as Qe } from "./visuallyHidden.js";
import { t as Ze } from "./useControlled.js";
import { a as De, c as oe, r as Ae, t as et } from "./useRegisterFieldControl.js";
import { t as tt } from "./FormContext.js";
import { t as nt } from "./useAriaLabelledBy.js";
import { t as rt } from "./useValueChanged.js";
var r = /* @__PURE__ */ we(qe()), at = /* @__PURE__ */ r.createContext({ disabled: !1 });
function ot() {
  return r.useContext(at);
}
var it = /* @__PURE__ */ r.createContext(void 0);
function st(e = !0) {
  const s = r.useContext(it);
  if (s === void 0 && !e) throw new Error(re(3));
  return s;
}
var ne = /* @__PURE__ */ (function(e) {
  return e.checked = "data-checked", e.unchecked = "data-unchecked", e.indeterminate = "data-indeterminate", e.disabled = "data-disabled", e.readonly = "data-readonly", e.required = "data-required", e.valid = "data-valid", e.invalid = "data-invalid", e.touched = "data-touched", e.dirty = "data-dirty", e.filled = "data-filled", e.focused = "data-focused", e;
})({});
function ie(e) {
  return r.useMemo(() => ({
    checked(s) {
      return e.indeterminate ? {} : s ? { [ne.checked]: "" } : { [ne.unchecked]: "" };
    },
    ...oe
  }), [e.indeterminate]);
}
var se = /* @__PURE__ */ r.createContext(void 0);
function dt() {
  const e = r.useContext(se);
  if (e === void 0) throw new Error(re(14));
  return e;
}
var L = Ge(), lt = "data-parent", It = /* @__PURE__ */ r.forwardRef(function(s, B) {
  const { checked: N, className: de, defaultChecked: H = !1, "aria-labelledby": _, disabled: F = !1, form: l, id: p, indeterminate: k = !1, inputRef: K, name: O, onCheckedChange: P, parent: i = !1, readOnly: d = !1, render: W, required: m = !1, uncheckedValue: b, value: E, nativeButton: I = !1, style: ut, ...le } = s, { clearErrors: ue } = tt(), { disabled: ce, name: fe, setDirty: pe, setFilled: w, setFocused: $, setTouched: me, state: J, validationMode: he, validityData: ve, shouldValidateOnChange: Ce, validation: ge } = De(), ke = ot(), { labelId: ye, controlId: xe, registerControlId: h, getDescriptionProps: U } = Ae(), n = st(), y = n?.parent, x = y && n.allValues, u = ce || ke.disabled || n?.disabled || F, v = fe ?? O, a = E ?? v, X = te(), Pe = te();
  let c = xe;
  x ? c = i ? Pe : `${y.id}-${a}` : p && (c = p);
  let q = {};
  x && (i ? q = n.parent.getParentProps() : a && (q = n.parent.getChildProps(a)));
  const be = He(P), { checked: Y = N, indeterminate: R = k, onCheckedChange: Ee, ...Ie } = q, C = n?.value, z = n?.setValue, Q = n?.defaultValue, G = r.useRef(null), M = Ne(() => /* @__PURE__ */ Symbol("checkbox-control")), T = r.useRef(!1), { getButtonProps: Re, buttonRef: Me } = We({
    disabled: u,
    native: I
  }), f = n?.validation ?? ge, [o, Se] = Ze({
    controlled: a && C && !i ? C.includes(a) : Y,
    default: a && Q && !i ? Q.includes(a) : H,
    name: "Checkbox",
    state: "checked"
  });
  ee(() => {
    h !== A && (T.current = !0, h(M.current, c));
  }, [
    c,
    n,
    h,
    i,
    M
  ]), r.useEffect(() => {
    const t = M.current;
    return () => {
      !T.current || h === A || (T.current = !1, h(t, void 0));
    };
  }, [h, M]), et(G, {
    enabled: !n,
    id: X,
    value: o
  });
  const g = r.useRef(null), Ve = je(K, g, f.inputRef), Be = nt(_, ye, g, !I, c ?? void 0);
  ee(() => {
    g.current && (g.current.indeterminate = R, o && w(!0));
  }, [
    o,
    R,
    w
  ]), rt(o, () => {
    n && !i || (ue(v), w(o), pe(o !== ve.initialValue), Ce() ? f.commit(o) : f.commit(o, !0));
  });
  const _e = Te({
    checked: o,
    disabled: u,
    form: l,
    name: i ? void 0 : v,
    id: I ? void 0 : c ?? void 0,
    required: m,
    ref: Ve,
    style: v ? ze : Qe,
    tabIndex: -1,
    type: "checkbox",
    "aria-hidden": !0,
    onChange(t) {
      if (t.nativeEvent.defaultPrevented) return;
      if (d) {
        t.preventDefault();
        return;
      }
      const S = t.currentTarget.checked, V = Ye(Xe, t.nativeEvent);
      Ee?.(S, V), be(S, V), !V.isCanceled && (Se(S), a && C && z && !i && !x && z(S ? [...C, a] : C.filter((Oe) => Oe !== a), V));
    },
    onFocus() {
      G.current?.focus();
    }
  }, E !== void 0 ? { value: (n ? o && E : E) || "" } : Le, U, n ? f.getValidationProps : f.getInputValidationProps), Z = x ? !!Y : o, D = x && R || k;
  r.useEffect(() => {
    if (!y || !a) return;
    const t = y.disabledStatesRef.current;
    return t.set(a, u), () => {
      t.delete(a);
    };
  }, [
    y,
    u,
    a
  ]);
  const j = r.useMemo(() => ({
    ...J,
    checked: Z,
    disabled: u,
    readOnly: d,
    required: m,
    indeterminate: D
  }), [
    J,
    Z,
    u,
    d,
    m,
    D
  ]), Fe = ie(j), Ke = ae("span", s, {
    state: j,
    ref: [
      Me,
      G,
      B,
      n?.registerControlRef
    ],
    props: [
      {
        id: I ? c ?? void 0 : X,
        role: "checkbox",
        "aria-checked": R ? "mixed" : o,
        "aria-readonly": d || void 0,
        "aria-required": m || void 0,
        "aria-labelledby": Be,
        [lt]: i ? "" : void 0,
        onFocus() {
          $(!0);
        },
        onBlur() {
          const t = g.current;
          t && (me(!0), $(!1), he === "onBlur" && f.commit(n ? C : t.checked));
        },
        onClick(t) {
          d || u || (t.preventDefault(), g.current?.dispatchEvent(new PointerEvent("click", {
            bubbles: !0,
            shiftKey: t.shiftKey,
            ctrlKey: t.ctrlKey,
            altKey: t.altKey,
            metaKey: t.metaKey
          })));
        }
      },
      U,
      f.getValidationProps,
      le,
      Ie,
      Re
    ],
    stateAttributesMapping: Fe
  });
  return /* @__PURE__ */ (0, L.jsxs)(se.Provider, {
    value: j,
    children: [
      Ke,
      !o && !n && v && !i && b !== void 0 && /* @__PURE__ */ (0, L.jsx)("input", {
        type: "hidden",
        form: l,
        name: v,
        value: b
      }),
      /* @__PURE__ */ (0, L.jsx)("input", {
        ..._e,
        suppressHydrationWarning: !0
      })
    ]
  });
}), Rt = /* @__PURE__ */ r.forwardRef(function(s, B) {
  const { render: N, className: de, style: H, keepMounted: _ = !1, ...F } = s, l = dt(), p = l.checked || l.indeterminate, { mounted: k, transitionStatus: K, setMounted: O } = Ue(p), P = r.useRef(null), i = {
    ...l,
    transitionStatus: K
  };
  Je({
    open: p,
    ref: P,
    onComplete() {
      p || O(!1);
    }
  });
  const d = ie(l), W = r.useMemo(() => ({
    ...d,
    ...$e,
    ...oe
  }), [d]), m = _ || k, b = ae("span", s, {
    ref: [B, P],
    state: i,
    stateAttributesMapping: W,
    props: F
  });
  return m ? b : null;
});
export {
  It as n,
  Rt as t
};

//# sourceMappingURL=CheckboxIndicator.js.map