"use client";
import { jsxs as Le, jsx as C } from "react/jsx-runtime";
import { MinusIcon as Te, CheckIcon as qe } from "lucide-react";
import { cn as je } from "../../../lib/utils.js";
import * as r from "react";
import { f as re, a as He, N as A, c as Ue, m as ze, E as We, u as ae } from "../../../useRenderElement-DNB2vtF1.js";
import { u as $e } from "../../../useControlled-Sx-_x7Sz.js";
import { u as De } from "../../../useStableCallback-CudqoENs.js";
import { u as ee } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { v as Je, a as Xe } from "../../../visuallyHidden-PuyyGHVF.js";
import { f as oe, c as Ye, a as Qe, u as Ze, b as Ae } from "../../../LabelableContext-Dzw1DWuj.js";
import { u as te } from "../../../useBaseUiId-C8Dq0BQq.js";
import { a as et } from "../../../useButton-JYPKQKj4.js";
import { u as tt } from "../../../FieldItemContext-CF2fRwBC.js";
import { u as nt } from "../../../useAriaLabelledBy-VklrCmKp.js";
import { c as rt, n as at } from "../../../createBaseUIEventDetails-CqidROtw.js";
import { u as ot } from "../../../useValueChanged-Bissqjjn.js";
import { u as dt, a as it, t as st } from "../../../useOpenChangeComplete-DkeH9FGJ.js";
let 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 de(e) {
  return r.useMemo(() => ({
    checked(d) {
      return e.indeterminate ? {} : d ? {
        [ne.checked]: ""
      } : {
        [ne.unchecked]: ""
      };
    },
    ...oe
  }), [e.indeterminate]);
}
const ie = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (ie.displayName = "CheckboxGroupContext");
function ct(e = !0) {
  const d = r.useContext(ie);
  if (d === void 0 && !e)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: CheckboxGroupContext is missing. CheckboxGroup parts must be placed within <CheckboxGroup>." : re(3));
  return d;
}
const j = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (j.displayName = "CheckboxRootContext");
function ut() {
  const e = r.useContext(j);
  if (e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: CheckboxRootContext is missing. Checkbox parts must be placed within <Checkbox.Root>." : re(14));
  return e;
}
const lt = "data-parent", se = /* @__PURE__ */ r.forwardRef(function(d, x) {
  const {
    checked: H,
    className: ue,
    defaultChecked: S = !1,
    "aria-labelledby": w,
    disabled: p = !1,
    id: m,
    indeterminate: v = !1,
    inputRef: G,
    name: K,
    onCheckedChange: E,
    parent: i = !1,
    readOnly: c = !1,
    render: U,
    required: h = !1,
    uncheckedValue: P,
    value: N,
    nativeButton: I = !1,
    ...le
  } = d, {
    clearErrors: fe
  } = Ye(), {
    disabled: pe,
    name: me,
    setDirty: he,
    setFilled: F,
    setFocused: z,
    setTouched: be,
    state: W,
    validationMode: ge,
    validityData: ke,
    shouldValidateOnChange: Ce,
    validation: xe
  } = Qe(), ve = tt(), {
    labelId: ye,
    controlId: Ee,
    registerControlId: b,
    getDescriptionProps: $
  } = Ze(), t = ct(), y = t?.parent, R = y && t.allValues, u = pe || ve.disabled || t?.disabled || p, l = me ?? K, a = N ?? l, D = te(), Pe = te();
  let f = Ee;
  R ? f = i ? Pe : `${y.id}-${a}` : m && (f = m);
  let _ = {};
  R && (i ? _ = t.parent.getParentProps() : a && (_ = t.parent.getChildProps(a)));
  const Ne = De(E), {
    checked: J = H,
    indeterminate: V = v,
    onCheckedChange: Ie,
    ...Re
  } = _, g = t?.value, X = t?.setValue, Y = t?.defaultValue, L = r.useRef(null), M = He(() => /* @__PURE__ */ Symbol("checkbox-control")), T = r.useRef(!1), {
    getButtonProps: Ve,
    buttonRef: Me
  } = et({
    disabled: u,
    native: I
  }), s = t?.validation ?? xe, [o, Be] = $e({
    controlled: a && g && !i ? g.includes(a) : J,
    default: a && Y && !i ? Y.includes(a) : S,
    name: "Checkbox",
    state: "checked"
  });
  ee(() => {
    b !== A && (T.current = !0, b(M.current, f));
  }, [f, t, b, i, M]), r.useEffect(() => {
    const n = M.current;
    return () => {
      !T.current || b === A || (T.current = !1, b(n, void 0));
    };
  }, [b, M]), Ae({
    enabled: !t,
    id: D,
    commit: s.commit,
    value: o,
    controlRef: L,
    name: l,
    getValue: () => o
  });
  const k = r.useRef(null), Oe = Ue(G, k, s.inputRef), Se = nt(w, ye, k, !I, f ?? void 0);
  ee(() => {
    k.current && (k.current.indeterminate = V, o && F(!0));
  }, [o, V, F]), ot(o, () => {
    t && !i || (fe(l), F(o), he(o !== ke.initialValue), Ce() ? s.commit(o) : s.commit(o, !0));
  });
  const we = ze(
    {
      checked: o,
      disabled: u,
      // parent checkboxes unset `name` to be excluded from form submission
      name: i ? void 0 : l,
      // Set `id` to stop Chrome warning about an unassociated input.
      // When using a native button, the `id` is applied to the button instead.
      id: I ? void 0 : f ?? void 0,
      required: h,
      ref: Oe,
      style: l ? Je : Xe,
      tabIndex: -1,
      type: "checkbox",
      "aria-hidden": !0,
      onChange(n) {
        if (n.nativeEvent.defaultPrevented)
          return;
        const B = n.target.checked, O = rt(at, n.nativeEvent);
        if (Ie?.(B, O), Ne(B, O), !O.isCanceled && (Be(B), a && g && X && !i)) {
          const Fe = B ? [...g, a] : g.filter((_e) => _e !== a);
          X(Fe, O);
        }
      },
      onFocus() {
        L.current?.focus();
      }
    },
    // React <19 sets an empty value if `undefined` is passed explicitly
    // To avoid this, we only set the value if it's defined
    N !== void 0 ? {
      value: (t ? o && N : N) || ""
    } : We,
    $,
    t ? s.getValidationProps : s.getInputValidationProps
  ), Q = R ? !!J : o, Z = R && V || v;
  r.useEffect(() => {
    if (!y || !a)
      return;
    const n = y.disabledStatesRef.current;
    return n.set(a, u), () => {
      n.delete(a);
    };
  }, [y, u, a]);
  const q = r.useMemo(() => ({
    ...W,
    checked: Q,
    disabled: u,
    readOnly: c,
    required: h,
    indeterminate: Z
  }), [W, Q, u, c, h, Z]), Ge = de(q), Ke = ae("span", d, {
    state: q,
    ref: [Me, L, x, t?.registerControlRef],
    props: [{
      id: I ? f ?? void 0 : D,
      role: "checkbox",
      "aria-checked": V ? "mixed" : o,
      "aria-readonly": c || void 0,
      "aria-required": h || void 0,
      "aria-labelledby": Se,
      [lt]: i ? "" : void 0,
      onFocus() {
        z(!0);
      },
      onBlur() {
        const n = k.current;
        n && (be(!0), z(!1), ge === "onBlur" && s.commit(t ? g : n.checked));
      },
      onClick(n) {
        c || u || (n.preventDefault(), k.current?.dispatchEvent(new PointerEvent("click", {
          bubbles: !0,
          shiftKey: n.shiftKey,
          ctrlKey: n.ctrlKey,
          altKey: n.altKey,
          metaKey: n.metaKey
        })));
      }
    }, $, s.getValidationProps, le, Re, Ve],
    stateAttributesMapping: Ge
  });
  return /* @__PURE__ */ Le(j.Provider, {
    value: q,
    children: [Ke, !o && !t && l && !i && P !== void 0 && /* @__PURE__ */ C("input", {
      type: "hidden",
      name: l,
      value: P
    }), /* @__PURE__ */ C("input", {
      ...we
    })]
  });
});
process.env.NODE_ENV !== "production" && (se.displayName = "CheckboxRoot");
const ce = /* @__PURE__ */ r.forwardRef(function(d, x) {
  const {
    render: H,
    className: ue,
    keepMounted: S = !1,
    ...w
  } = d, p = ut(), m = p.checked || p.indeterminate, {
    mounted: v,
    transitionStatus: G,
    setMounted: K
  } = dt(m), E = r.useRef(null), i = {
    ...p,
    transitionStatus: G
  };
  it({
    open: m,
    ref: E,
    onComplete() {
      m || K(!1);
    }
  });
  const c = de(p), U = r.useMemo(() => ({
    ...c,
    ...st,
    ...oe
  }), [c]), h = S || v, P = ae("span", d, {
    ref: [x, E],
    state: i,
    stateAttributesMapping: U,
    props: w
  });
  return h ? P : null;
});
process.env.NODE_ENV !== "production" && (ce.displayName = "CheckboxIndicator");
function Vt({
  className: e,
  indeterminate: d,
  ...x
}) {
  return /* @__PURE__ */ C(
    se,
    {
      className: je(
        "peer relative flex size-4 shrink-0 items-center justify-center rounded-[4px] border border-input outline-none transition-colors 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 group-has-disabled/field:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary data-disabled:data-checked:border-muted-foreground data-disabled:data-indeterminate:border-muted-foreground data-disabled:data-checked:bg-muted-foreground data-disabled:data-indeterminate:bg-muted-foreground data-disabled:cursor-not-allowed data-checked:border-primary data-indeterminate:border-primary data-checked:bg-primary data-indeterminate:bg-primary data-checked:text-primary-foreground data-indeterminate:text-primary-foreground data-disabled:opacity-50 dark:bg-input/30 dark:data-disabled:data-checked:bg-muted-foreground dark:data-disabled:data-indeterminate:bg-muted-foreground dark:data-checked:bg-primary dark:data-indeterminate:bg-primary dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
        e
      ),
      "data-slot": "checkbox",
      indeterminate: d,
      ...x,
      children: /* @__PURE__ */ C(
        ce,
        {
          className: "grid place-content-center text-current transition-none [&>svg]:size-3.5",
          "data-slot": "checkbox-indicator",
          children: d ? /* @__PURE__ */ C(Te, {}) : /* @__PURE__ */ C(qe, {})
        }
      )
    }
  );
}
export {
  Vt as Checkbox
};
