import { jsxs as X, jsx as v } from "react/jsx-runtime";
import * as z from "react";
import { useState as C, useCallback as M, useEffect as R, cloneElement as W, isValidElement as A, useRef as x, forwardRef as I } from "react";
import { b as j } from "./icon.es-CZcKxTaM.mjs";
import { l as F } from "./checkmark-bold.es-CV3e_t3E.mjs";
import { L as U } from "./label-7sJe5-pX.mjs";
import { a as V } from "./paragraph-DNtj7OLr.mjs";
import { u as G, c as J, a as K } from "./index-tyLV0iiH.mjs";
import { u as Q } from "./index-DLcqcWxM.mjs";
import { c as Y } from "./index-DOfn3ixy.mjs";
import { u as Z } from "./index-DPxjTK4w.mjs";
import { P as q } from "./index-CCNZ9uWB.mjs";
import { c as ee } from "./bind-CU-R61T-.mjs";
var P = "Switch", [te] = Y(P), [re, ne] = te(P), B = z.forwardRef(
  (e, r) => {
    const {
      __scopeSwitch: t,
      name: i,
      checked: o,
      defaultChecked: s,
      required: g,
      disabled: u,
      value: n = "on",
      onCheckedChange: h,
      form: p,
      ...f
    } = e, [a, c] = z.useState(null), l = Q(r, (b) => c(b)), m = z.useRef(!1), _ = a ? p || !!a.closest("form") : !0, [d = !1, S] = G({
      prop: o,
      defaultProp: s,
      onChange: h
    });
    return /* @__PURE__ */ X(re, { scope: t, checked: d, disabled: u, children: [
      /* @__PURE__ */ v(
        q.button,
        {
          type: "button",
          role: "switch",
          "aria-checked": d,
          "aria-required": g,
          "data-state": O(d),
          "data-disabled": u ? "" : void 0,
          disabled: u,
          value: n,
          ...f,
          ref: l,
          onClick: J(e.onClick, (b) => {
            S((w) => !w), _ && (m.current = b.isPropagationStopped(), m.current || b.stopPropagation());
          })
        }
      ),
      _ && /* @__PURE__ */ v(
        oe,
        {
          control: a,
          bubbles: !m.current,
          name: i,
          value: n,
          checked: d,
          required: g,
          disabled: u,
          form: p,
          style: { transform: "translateX(-100%)" }
        }
      )
    ] });
  }
);
B.displayName = P;
var H = "SwitchThumb", T = z.forwardRef(
  (e, r) => {
    const { __scopeSwitch: t, ...i } = e, o = ne(H, t);
    return /* @__PURE__ */ v(
      q.span,
      {
        "data-state": O(o.checked),
        "data-disabled": o.disabled ? "" : void 0,
        ...i,
        ref: r
      }
    );
  }
);
T.displayName = H;
var oe = (e) => {
  const { control: r, checked: t, bubbles: i = !0, ...o } = e, s = z.useRef(null), g = K(t), u = Z(r);
  return z.useEffect(() => {
    const n = s.current, h = window.HTMLInputElement.prototype, f = Object.getOwnPropertyDescriptor(h, "checked").set;
    if (g !== t && f) {
      const a = new Event("click", { bubbles: i });
      f.call(n, t), n.dispatchEvent(a);
    }
  }, [g, t, i]), /* @__PURE__ */ v(
    "input",
    {
      type: "checkbox",
      "aria-hidden": !0,
      defaultChecked: t,
      ...o,
      tabIndex: -1,
      ref: s,
      style: {
        ...e.style,
        ...u,
        position: "absolute",
        pointerEvents: "none",
        opacity: 0,
        margin: 0
      }
    }
  );
};
function O(e) {
  return e ? "checked" : "unchecked";
}
var se = B, ie = T;
const y = (e) => e && "touches" in e, ae = ({
  children: e,
  disabled: r,
  onStart: t,
  onDrag: i,
  onStop: o,
  bounds: s,
  position: g,
  style: u
}) => {
  const [n, h] = C(void 0), [p, f] = C(void 0), [a, c] = C(!1), l = (d) => {
    if (!r) {
      d.preventDefault();
      const S = y(d.nativeEvent) ? d.nativeEvent.touches[0].clientX : d.nativeEvent.clientX;
      f(S), t?.();
    }
  }, m = M(
    (d) => {
      if (typeof p != "number")
        return;
      const b = (y(d) ? d.touches[0].clientX : d.clientX) - p;
      if (!b)
        return;
      const w = g + b;
      !a && Math.abs(w) && c(!0);
      const E = w > s.right ? s.right : w < s.left ? s.left : w;
      h(E), i?.({ x: E });
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [i, h, a, p]
  ), _ = M(() => {
    f(void 0), h(void 0), c(!1), o?.();
  }, [o, f]);
  return R(() => (window.addEventListener("mousemove", m), window.addEventListener("touchmove", m), () => {
    window.removeEventListener("mousemove", m), window.removeEventListener("touchmove", m);
  }), [m]), R(() => (window.addEventListener("mouseup", _), window.addEventListener("touchend", _), () => {
    window.removeEventListener("mouseup", _), window.removeEventListener("touchend", _);
  }), [_]), A(e) ? W(e, {
    onMouseDown: l,
    onTouchStart: l,
    style: { ...u, transform: `translateX(${a ? n : g}px)` }
  }) : null;
}, N = {
  width: void 0,
  height: void 0
};
function L(e) {
  const { ref: r, box: t = "content-box", round: i } = e, [{ width: o, height: s }, g] = C(N), u = ce(), n = x({ ...N }), h = x(void 0);
  return h.current = e.onResize, R(() => {
    if (!r.current || typeof window > "u" || !("ResizeObserver" in window))
      return;
    const p = new ResizeObserver(([f]) => {
      const a = t === "border-box" ? "borderBoxSize" : t === "device-pixel-content-box" ? "devicePixelContentBoxSize" : "contentBoxSize", c = $(f, a, "inlineSize"), l = $(f, a, "blockSize");
      if (n.current.width !== c || n.current.height !== l) {
        const _ = {
          width: c && i ? Math.round(c) : c,
          height: l && i ? Math.round(l) : l
        };
        n.current.width = c, n.current.height = l, h.current ? h.current(_) : u() && g(_);
      }
    });
    return p.observe(r.current, { box: t }), () => {
      p.disconnect();
    };
  }, [t, r, u, i]), { width: o, height: s };
}
function $(e, r, t) {
  return e[r] ? Array.isArray(e[r]) ? e[r][0][t] : (
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore Support Firefox's non-standard behavior
    e[r][t]
  ) : r === "contentBoxSize" ? e.contentRect[t === "inlineSize" ? "width" : "height"] : void 0;
}
function ce() {
  const e = x(!1);
  return R(() => (e.current = !0, () => {
    e.current = !1;
  }), []), M(() => e.current, []);
}
const ue = ({ checked: e, onChange: r }) => {
  const t = x(null), i = x(null), [o, s] = C(void 0), [g, u] = C(!1), [n, h] = C(0), { height: p = 0 } = L({ ref: i, round: !0 }), { height: f = 0 } = L({ ref: t, round: !0 }), a = (f || 0) - p, c = p + a, l = a / 2, m = c / 2, _ = typeof o == "number" && (e ? o < m : o > m);
  return {
    trackRef: t,
    thumbRef: i,
    isDragging: g,
    bounds: { left: l, right: c },
    position: e ? c : l,
    onDrag: ({ x: w }) => {
      s(w), Math.abs((e ? c : l) - w) > 2 && u(!0);
    },
    onStop: () => {
      u(!1), g && h(Date.now()), _ && r?.(!e);
    },
    onChangeWithDrag: () => {
      Date.now() - n > 50 && r?.(!e), s(void 0);
    }
  };
}, le = {
  "purpur-toggle": "_purpur-toggle_zqs8u_1",
  "purpur-toggle__track": "_purpur-toggle__track_zqs8u_7",
  "purpur-toggle__checkmark-container": "_purpur-toggle__checkmark-container_zqs8u_45",
  "purpur-toggle__thumb": "_purpur-toggle__thumb_zqs8u_60",
  "purpur-toggle__thumb--dragging": "_purpur-toggle__thumb--dragging_zqs8u_85",
  "purpur-toggle__checkmark": "_purpur-toggle__checkmark_zqs8u_45",
  "purpur-toggle__label--right": "_purpur-toggle__label--right_zqs8u_110",
  "purpur-toggle__label--left": "_purpur-toggle__label--left_zqs8u_113",
  "purpur-toggle__container": "_purpur-toggle__container_zqs8u_116"
}, D = ee.bind(le), k = "purpur-toggle", de = I(
  ({
    ["data-testid"]: e,
    className: r,
    label: t,
    onChange: i,
    labelPosition: o = "right",
    checked: s,
    disableDrag: g,
    defaultChecked: u,
    ...n
  }, h) => {
    const [p, f] = C(
      typeof s == "boolean" ? s : !!u
    ), a = !!(typeof s == "boolean" ? s : p), { thumbRef: c, trackRef: l, isDragging: m, onChangeWithDrag: _, ...d } = ue({
      checked: a,
      onChange: (b) => {
        n.disabled || (i?.(b), f(b));
      }
    }), S = () => /* @__PURE__ */ v(
      U,
      {
        htmlFor: n.id,
        "data-testid": e && `${e}-label`,
        disabled: n.disabled,
        className: D(`${k}__label`, `${k}__label--${o}`),
        children: /* @__PURE__ */ v(V, { disabled: n.disabled, children: t })
      }
    );
    return /* @__PURE__ */ X("div", { className: D(r, `${k}__container`), children: [
      t && o === "left" && S(),
      /* @__PURE__ */ v(
        se,
        {
          ...n,
          ref: h,
          id: n.id,
          "data-testid": e,
          className: D(k),
          onCheckedChange: _,
          checked: a,
          children: /* @__PURE__ */ X("span", { ref: l, className: D(`${k}__track`), children: [
            /* @__PURE__ */ v("span", { className: D(`${k}__checkmark-container`), children: /* @__PURE__ */ v(j, { className: D(`${k}__checkmark`), svg: F, size: "xxs" }) }),
            /* @__PURE__ */ v(ae, { disabled: g, ...d, children: /* @__PURE__ */ v(
              ie,
              {
                ref: c,
                "data-testid": e && `${e}-thumb`,
                className: D(`${k}__thumb`, {
                  [`${k}__thumb--dragging`]: m
                })
              }
            ) })
          ] })
        }
      ),
      t && o === "right" && S()
    ] });
  }
);
de.displayName = "Toggle";
export {
  de as T
};
//# sourceMappingURL=toggle-Cezg5MUc.mjs.map
