"use client";
import { jsxs as T, jsx as f } from "react/jsx-runtime";
import { cn as E } from "../../../lib/utils.js";
import * as i from "react";
import { u as ce } from "../../../useControlled-Sx-_x7Sz.js";
import { u as le } from "../../../useStableCallback-CudqoENs.js";
import { f as ue, c as fe, m as pe, E as me, u as _ } from "../../../useRenderElement-DNB2vtF1.js";
import { u as he } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { v as be, a as ke } from "../../../visuallyHidden-PuyyGHVF.js";
import { u as ge } from "../../../useBaseUiId-C8Dq0BQq.js";
import { f as ve, c as ye, a as q, u as xe, b as Ce } from "../../../LabelableContext-Dzw1DWuj.js";
import { u as we } from "../../../useAriaLabelledBy-VklrCmKp.js";
import { u as Ee } from "../../../useLabelableId-DF_d91GP.js";
import { c as Ne, n as ze } from "../../../createBaseUIEventDetails-CqidROtw.js";
import { u as Pe } from "../../../useValueChanged-Bissqjjn.js";
import { a as Re } from "../../../useButton-JYPKQKj4.js";
const N = /* @__PURE__ */ i.createContext(void 0);
process.env.NODE_ENV !== "production" && (N.displayName = "SwitchRootContext");
function Ie() {
  const e = i.useContext(N);
  if (e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: SwitchRootContext is missing. Switch parts must be placed within <Switch.Root>." : ue(63));
  return e;
}
let L = /* @__PURE__ */ (function(e) {
  return e.checked = "data-checked", e.unchecked = "data-unchecked", 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;
})({});
const j = {
  ...ve,
  checked(e) {
    return e ? {
      [L.checked]: ""
    } : {
      [L.unchecked]: ""
    };
  }
}, H = /* @__PURE__ */ i.forwardRef(function(s, n) {
  const {
    checked: p,
    className: k,
    defaultChecked: l,
    "aria-labelledby": g,
    id: v,
    inputRef: y,
    name: J,
    nativeButton: m = !1,
    onCheckedChange: Y,
    readOnly: h = !1,
    required: u = !1,
    disabled: D = !1,
    render: Be,
    uncheckedValue: z,
    value: x,
    ...G
  } = s, {
    clearErrors: Q
  } = ye(), {
    state: P,
    setTouched: W,
    setDirty: X,
    validityData: Z,
    setFilled: C,
    setFocused: R,
    shouldValidateOnChange: $,
    validationMode: A,
    disabled: ee,
    name: ae,
    validation: r
  } = q(), {
    labelId: te
  } = xe(), d = ee || D, o = ae ?? J, I = le(Y), c = i.useRef(null), B = fe(c, y, r.inputRef), b = i.useRef(null), V = ge(), S = Ee({
    id: v,
    implicit: !1,
    controlRef: b
  }), w = m ? void 0 : S, [a, K] = ce({
    controlled: p,
    default: !!l,
    name: "Switch",
    state: "checked"
  });
  Ce({
    id: V,
    commit: r.commit,
    value: a,
    controlRef: b,
    name: o,
    getValue: () => a
  }), he(() => {
    c.current && C(c.current.checked);
  }, [c, C]), Pe(a, () => {
    Q(o), X(a !== Z.initialValue), C(a), $() ? r.commit(a) : r.commit(a, !0);
  });
  const {
    getButtonProps: ne,
    buttonRef: re
  } = Re({
    disabled: d,
    native: m
  }), de = we(g, te, c, !m, w), ie = {
    id: m ? S : V,
    role: "switch",
    "aria-checked": a,
    "aria-readonly": h || void 0,
    "aria-required": u || void 0,
    "aria-labelledby": de,
    onFocus() {
      d || R(!0);
    },
    onBlur() {
      const t = c.current;
      !t || d || (W(!0), R(!1), A === "onBlur" && r.commit(t.checked));
    },
    onClick(t) {
      h || d || (t.preventDefault(), c.current?.dispatchEvent(new PointerEvent("click", {
        bubbles: !0,
        shiftKey: t.shiftKey,
        ctrlKey: t.ctrlKey,
        altKey: t.altKey,
        metaKey: t.metaKey
      })));
    }
  }, se = i.useMemo(() => pe(
    {
      checked: a,
      disabled: d,
      id: w,
      name: o,
      required: u,
      style: o ? be : ke,
      tabIndex: -1,
      type: "checkbox",
      "aria-hidden": !0,
      ref: B,
      onChange(t) {
        if (t.nativeEvent.defaultPrevented)
          return;
        const F = t.target.checked, O = Ne(ze, t.nativeEvent);
        I?.(F, O), !O.isCanceled && K(F);
      },
      onFocus() {
        b.current?.focus();
      }
    },
    r.getInputValidationProps,
    // React <19 sets an empty value if `undefined` is passed explicitly
    // To avoid this, we only set the value if it's defined
    x !== void 0 ? {
      value: x
    } : me
  ), [a, d, B, w, o, I, u, K, r, x]), M = i.useMemo(() => ({
    ...P,
    checked: a,
    disabled: d,
    readOnly: h,
    required: u
  }), [P, a, d, h, u]), oe = _("span", s, {
    state: M,
    ref: [n, b, re],
    props: [ie, r.getValidationProps, G, ne],
    stateAttributesMapping: j
  });
  return /* @__PURE__ */ T(N.Provider, {
    value: M,
    children: [oe, !a && o && z !== void 0 && /* @__PURE__ */ f("input", {
      type: "hidden",
      name: o,
      value: z
    }), /* @__PURE__ */ f("input", {
      ...se
    })]
  });
});
process.env.NODE_ENV !== "production" && (H.displayName = "SwitchRoot");
const U = /* @__PURE__ */ i.forwardRef(function(s, n) {
  const {
    render: p,
    className: k,
    ...l
  } = s, {
    state: g
  } = q(), v = Ie(), y = {
    ...g,
    ...v
  };
  return _("span", s, {
    state: y,
    ref: n,
    stateAttributesMapping: j,
    props: l
  });
});
process.env.NODE_ENV !== "production" && (U.displayName = "SwitchThumb");
function Ye({
  className: e,
  size: s = "default",
  loading: n = !1,
  loadingIndicatorClassName: p,
  disabled: k,
  ...l
}) {
  return /* @__PURE__ */ T(
    H,
    {
      "aria-busy": n || void 0,
      className: E(
        "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-foreground/10 outline-none transition-all after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-[size=default]:h-[18.4px] data-[size=sm]:h-[14px] data-[size=default]:w-[32px] data-[size=sm]:w-[24px] data-disabled:cursor-not-allowed data-checked:bg-primary data-unchecked:bg-input data-disabled:opacity-50 dark:border-transparent dark:data-unchecked:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
        e
      ),
      "data-loading": n || void 0,
      "data-size": s,
      "data-slot": "switch",
      disabled: k || n,
      ...l,
      children: [
        /* @__PURE__ */ f(
          U,
          {
            className: E(
              "pointer-events-none block rounded-full bg-background ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-unchecked:translate-x-0 dark:data-checked:bg-primary-foreground dark:data-unchecked:bg-foreground",
              n && "opacity-0"
            ),
            "data-slot": "switch-thumb"
          }
        ),
        n && /* @__PURE__ */ f(
          "span",
          {
            "aria-hidden": "true",
            className: "pointer-events-none absolute inset-0 flex items-center justify-center",
            children: /* @__PURE__ */ f(
              "span",
              {
                className: E(
                  "animate-spin rounded-full border-2 border-muted-foreground border-t-transparent group-data-[size=default]/switch:size-3 group-data-[size=sm]/switch:size-2.5",
                  p
                )
              }
            )
          }
        )
      ]
    }
  );
}
export {
  Ye as Switch
};
