import * as d from "react";
import { clamp as Y } from "../../../../../@radix-ui_number@1.1.0/node_modules/@radix-ui/number/dist/index.js";
import { composeEventHandlers as M } from "../../../../../@radix-ui_primitive@1.1.0/node_modules/@radix-ui/primitive/dist/index.js";
import { useComposedRefs as I } from "../../../../../@radix-ui_react-compose-refs@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-compose-refs/dist/index.js";
import { createContextScope as se } from "../../../../../@radix-ui_react-context@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-context/dist/index.js";
import { useControllableState as ae } from "../../../../../@radix-ui_react-use-controllable-state@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-controllable-state/dist/index.js";
import { useDirection as ce } from "../../../../../@radix-ui_react-direction@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-direction/dist/index.js";
import { usePrevious as le } from "../../../../../@radix-ui_react-use-previous@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-previous/dist/index.js";
import { useSize as de } from "../../../../../@radix-ui_react-use-size@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-size/dist/index.js";
import { Primitive as V } from "../../../../../@radix-ui_react-primitive@2.0.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_rea_bdcf182289a3a76356f07e550726e479/node_modules/@radix-ui/react-primitive/dist/index.js";
import { createCollection as ue } from "../../../../../@radix-ui_react-collection@1.1.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_re_068c4d9b06eb1baa6b6ecff66d582472/node_modules/@radix-ui/react-collection/dist/index.js";
import { j as p } from "../../../../../../../_virtual/jsx-runtime.js";
var $ = ["PageUp", "PageDown"], X = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"], G = {
  "from-left": ["Home", "PageDown", "ArrowDown", "ArrowLeft"],
  "from-right": ["Home", "PageDown", "ArrowDown", "ArrowRight"],
  "from-bottom": ["Home", "PageDown", "ArrowDown", "ArrowLeft"],
  "from-top": ["Home", "PageDown", "ArrowUp", "ArrowLeft"]
}, _ = "Slider", [z, fe, me] = ue(_), [W, ze] = se(_, [
  me
]), [pe, j] = W(_), q = d.forwardRef(
  (e, t) => {
    const {
      name: o,
      min: n = 0,
      max: r = 100,
      step: a = 1,
      orientation: s = "horizontal",
      disabled: c = !1,
      minStepsBetweenThumbs: f = 0,
      defaultValue: h = [n],
      value: w,
      onValueChange: i = () => {
      },
      onValueCommit: m = () => {
      },
      inverted: g = !1,
      ...x
    } = e, S = d.useRef(/* @__PURE__ */ new Set()), l = d.useRef(0), P = s === "horizontal" ? Se : he, [u = [], y] = ae({
      prop: w,
      defaultProp: h,
      onChange: (v) => {
        [...S.current][l.current]?.focus(), i(v);
      }
    }), E = d.useRef(u);
    function L(v) {
      const R = be(u, v);
      K(v, R);
    }
    function T(v) {
      K(v, l.current);
    }
    function ie() {
      const v = E.current[l.current];
      u[l.current] !== v && m(u);
    }
    function K(v, R, { commit: B } = { commit: !1 }) {
      const F = De(a), k = Ee(Math.round((v - n) / a) * a + n, F), A = Y(k, [n, r]);
      y((C = []) => {
        const D = we(C, A, R);
        if (Re(D, f * a)) {
          l.current = D.indexOf(A);
          const U = String(D) !== String(C);
          return U && B && m(D), U ? D : C;
        } else
          return C;
      });
    }
    return /* @__PURE__ */ p.jsx(
      pe,
      {
        scope: e.__scopeSlider,
        name: o,
        disabled: c,
        min: n,
        max: r,
        valueIndexToChangeRef: l,
        thumbs: S.current,
        values: u,
        orientation: s,
        children: /* @__PURE__ */ p.jsx(z.Provider, { scope: e.__scopeSlider, children: /* @__PURE__ */ p.jsx(z.Slot, { scope: e.__scopeSlider, children: /* @__PURE__ */ p.jsx(
          P,
          {
            "aria-disabled": c,
            "data-disabled": c ? "" : void 0,
            ...x,
            ref: t,
            onPointerDown: M(x.onPointerDown, () => {
              c || (E.current = u);
            }),
            min: n,
            max: r,
            inverted: g,
            onSlideStart: c ? void 0 : L,
            onSlideMove: c ? void 0 : T,
            onSlideEnd: c ? void 0 : ie,
            onHomeKeyDown: () => !c && K(n, 0, { commit: !0 }),
            onEndKeyDown: () => !c && K(r, u.length - 1, { commit: !0 }),
            onStepKeyDown: ({ event: v, direction: R }) => {
              if (!c) {
                const k = $.includes(v.key) || v.shiftKey && X.includes(v.key) ? 10 : 1, A = l.current, C = u[A], D = a * k * R;
                K(C + D, A, { commit: !0 });
              }
            }
          }
        ) }) })
      }
    );
  }
);
q.displayName = _;
var [J, Q] = W(_, {
  startEdge: "left",
  endEdge: "right",
  size: "width",
  direction: 1
}), Se = d.forwardRef(
  (e, t) => {
    const {
      min: o,
      max: n,
      dir: r,
      inverted: a,
      onSlideStart: s,
      onSlideMove: c,
      onSlideEnd: f,
      onStepKeyDown: h,
      ...w
    } = e, [i, m] = d.useState(null), g = I(t, (u) => m(u)), x = d.useRef(), S = ce(r), l = S === "ltr", b = l && !a || !l && a;
    function P(u) {
      const y = x.current || i.getBoundingClientRect(), E = [0, y.width], T = O(E, b ? [o, n] : [n, o]);
      return x.current = y, T(u - y.left);
    }
    return /* @__PURE__ */ p.jsx(
      J,
      {
        scope: e.__scopeSlider,
        startEdge: b ? "left" : "right",
        endEdge: b ? "right" : "left",
        direction: b ? 1 : -1,
        size: "width",
        children: /* @__PURE__ */ p.jsx(
          Z,
          {
            dir: S,
            "data-orientation": "horizontal",
            ...w,
            ref: g,
            style: {
              ...w.style,
              "--radix-slider-thumb-transform": "translateX(-50%)"
            },
            onSlideStart: (u) => {
              const y = P(u.clientX);
              s?.(y);
            },
            onSlideMove: (u) => {
              const y = P(u.clientX);
              c?.(y);
            },
            onSlideEnd: () => {
              x.current = void 0, f?.();
            },
            onStepKeyDown: (u) => {
              const E = G[b ? "from-left" : "from-right"].includes(u.key);
              h?.({ event: u, direction: E ? -1 : 1 });
            }
          }
        )
      }
    );
  }
), he = d.forwardRef(
  (e, t) => {
    const {
      min: o,
      max: n,
      inverted: r,
      onSlideStart: a,
      onSlideMove: s,
      onSlideEnd: c,
      onStepKeyDown: f,
      ...h
    } = e, w = d.useRef(null), i = I(t, w), m = d.useRef(), g = !r;
    function x(S) {
      const l = m.current || w.current.getBoundingClientRect(), b = [0, l.height], u = O(b, g ? [n, o] : [o, n]);
      return m.current = l, u(S - l.top);
    }
    return /* @__PURE__ */ p.jsx(
      J,
      {
        scope: e.__scopeSlider,
        startEdge: g ? "bottom" : "top",
        endEdge: g ? "top" : "bottom",
        size: "height",
        direction: g ? 1 : -1,
        children: /* @__PURE__ */ p.jsx(
          Z,
          {
            "data-orientation": "vertical",
            ...h,
            ref: i,
            style: {
              ...h.style,
              "--radix-slider-thumb-transform": "translateY(50%)"
            },
            onSlideStart: (S) => {
              const l = x(S.clientY);
              a?.(l);
            },
            onSlideMove: (S) => {
              const l = x(S.clientY);
              s?.(l);
            },
            onSlideEnd: () => {
              m.current = void 0, c?.();
            },
            onStepKeyDown: (S) => {
              const b = G[g ? "from-bottom" : "from-top"].includes(S.key);
              f?.({ event: S, direction: b ? -1 : 1 });
            }
          }
        )
      }
    );
  }
), Z = d.forwardRef(
  (e, t) => {
    const {
      __scopeSlider: o,
      onSlideStart: n,
      onSlideMove: r,
      onSlideEnd: a,
      onHomeKeyDown: s,
      onEndKeyDown: c,
      onStepKeyDown: f,
      ...h
    } = e, w = j(_, o);
    return /* @__PURE__ */ p.jsx(
      V.span,
      {
        ...h,
        ref: t,
        onKeyDown: M(e.onKeyDown, (i) => {
          i.key === "Home" ? (s(i), i.preventDefault()) : i.key === "End" ? (c(i), i.preventDefault()) : $.concat(X).includes(i.key) && (f(i), i.preventDefault());
        }),
        onPointerDown: M(e.onPointerDown, (i) => {
          const m = i.target;
          m.setPointerCapture(i.pointerId), i.preventDefault(), w.thumbs.has(m) ? m.focus() : n(i);
        }),
        onPointerMove: M(e.onPointerMove, (i) => {
          i.target.hasPointerCapture(i.pointerId) && r(i);
        }),
        onPointerUp: M(e.onPointerUp, (i) => {
          const m = i.target;
          m.hasPointerCapture(i.pointerId) && (m.releasePointerCapture(i.pointerId), a(i));
        })
      }
    );
  }
), ee = "SliderTrack", te = d.forwardRef(
  (e, t) => {
    const { __scopeSlider: o, ...n } = e, r = j(ee, o);
    return /* @__PURE__ */ p.jsx(
      V.span,
      {
        "data-disabled": r.disabled ? "" : void 0,
        "data-orientation": r.orientation,
        ...n,
        ref: t
      }
    );
  }
);
te.displayName = ee;
var H = "SliderRange", ne = d.forwardRef(
  (e, t) => {
    const { __scopeSlider: o, ...n } = e, r = j(H, o), a = Q(H, o), s = d.useRef(null), c = I(t, s), f = r.values.length, h = r.values.map(
      (m) => re(m, r.min, r.max)
    ), w = f > 1 ? Math.min(...h) : 0, i = 100 - Math.max(...h);
    return /* @__PURE__ */ p.jsx(
      V.span,
      {
        "data-orientation": r.orientation,
        "data-disabled": r.disabled ? "" : void 0,
        ...n,
        ref: c,
        style: {
          ...e.style,
          [a.startEdge]: w + "%",
          [a.endEdge]: i + "%"
        }
      }
    );
  }
);
ne.displayName = H;
var N = "SliderThumb", oe = d.forwardRef(
  (e, t) => {
    const o = fe(e.__scopeSlider), [n, r] = d.useState(null), a = I(t, (c) => r(c)), s = d.useMemo(
      () => n ? o().findIndex((c) => c.ref.current === n) : -1,
      [o, n]
    );
    return /* @__PURE__ */ p.jsx(ge, { ...e, ref: a, index: s });
  }
), ge = d.forwardRef(
  (e, t) => {
    const { __scopeSlider: o, index: n, name: r, ...a } = e, s = j(N, o), c = Q(N, o), [f, h] = d.useState(null), w = I(t, (P) => h(P)), i = f ? !!f.closest("form") : !0, m = de(f), g = s.values[n], x = g === void 0 ? 0 : re(g, s.min, s.max), S = xe(n, s.values.length), l = m?.[c.size], b = l ? ye(l, x, c.direction) : 0;
    return d.useEffect(() => {
      if (f)
        return s.thumbs.add(f), () => {
          s.thumbs.delete(f);
        };
    }, [f, s.thumbs]), /* @__PURE__ */ p.jsxs(
      "span",
      {
        style: {
          transform: "var(--radix-slider-thumb-transform)",
          position: "absolute",
          [c.startEdge]: `calc(${x}% + ${b}px)`
        },
        children: [
          /* @__PURE__ */ p.jsx(z.ItemSlot, { scope: e.__scopeSlider, children: /* @__PURE__ */ p.jsx(
            V.span,
            {
              role: "slider",
              "aria-label": e["aria-label"] || S,
              "aria-valuemin": s.min,
              "aria-valuenow": g,
              "aria-valuemax": s.max,
              "aria-orientation": s.orientation,
              "data-orientation": s.orientation,
              "data-disabled": s.disabled ? "" : void 0,
              tabIndex: s.disabled ? void 0 : 0,
              ...a,
              ref: w,
              style: g === void 0 ? { display: "none" } : e.style,
              onFocus: M(e.onFocus, () => {
                s.valueIndexToChangeRef.current = n;
              })
            }
          ) }),
          i && /* @__PURE__ */ p.jsx(
            ve,
            {
              name: r ?? (s.name ? s.name + (s.values.length > 1 ? "[]" : "") : void 0),
              value: g
            },
            n
          )
        ]
      }
    );
  }
);
oe.displayName = N;
var ve = (e) => {
  const { value: t, ...o } = e, n = d.useRef(null), r = le(t);
  return d.useEffect(() => {
    const a = n.current, s = window.HTMLInputElement.prototype, f = Object.getOwnPropertyDescriptor(s, "value").set;
    if (r !== t && f) {
      const h = new Event("input", { bubbles: !0 });
      f.call(a, t), a.dispatchEvent(h);
    }
  }, [r, t]), /* @__PURE__ */ p.jsx("input", { style: { display: "none" }, ...o, ref: n, defaultValue: t });
};
function we(e = [], t, o) {
  const n = [...e];
  return n[o] = t, n.sort((r, a) => r - a);
}
function re(e, t, o) {
  const a = 100 / (o - t) * (e - t);
  return Y(a, [0, 100]);
}
function xe(e, t) {
  return t > 2 ? `Value ${e + 1} of ${t}` : t === 2 ? ["Minimum", "Maximum"][e] : void 0;
}
function be(e, t) {
  if (e.length === 1) return 0;
  const o = e.map((r) => Math.abs(r - t)), n = Math.min(...o);
  return o.indexOf(n);
}
function ye(e, t, o) {
  const n = e / 2, a = O([0, 50], [0, n]);
  return (n - a(t) * o) * o;
}
function Pe(e) {
  return e.slice(0, -1).map((t, o) => e[o + 1] - t);
}
function Re(e, t) {
  if (t > 0) {
    const o = Pe(e);
    return Math.min(...o) >= t;
  }
  return !0;
}
function O(e, t) {
  return (o) => {
    if (e[0] === e[1] || t[0] === t[1]) return t[0];
    const n = (t[1] - t[0]) / (e[1] - e[0]);
    return t[0] + n * (o - e[0]);
  };
}
function De(e) {
  return (String(e).split(".")[1] || "").length;
}
function Ee(e, t) {
  const o = Math.pow(10, t);
  return Math.round(e * o) / o;
}
var He = q, Ne = te, Oe = ne, Le = oe;
export {
  Oe as Range,
  He as Root,
  q as Slider,
  ne as SliderRange,
  oe as SliderThumb,
  te as SliderTrack,
  Le as Thumb,
  Ne as Track,
  ze as createSliderScope
};
//# sourceMappingURL=index.js.map
