import { j as o } from "../../_virtual/jsx-runtime.js";
import { useRef as B, useState as S, useEffect as W } from "react";
import { Wrapper as X, Col as k, TextWrapper as V, Title as w, SupportText as I, SliderWrapper as U, Input as q, Track as z, ThumbWrapper as A, Thumb as F, ThumbValue as G, MinMaxValuesWrapper as H } from "./styles.js";
const J = (s, t) => Math.ceil(s / t) * t, K = (s, t, n) => s < t ? t : s > t + n ? t + n : s;
function Z({
  onChange: s,
  value: t,
  min: n = 0,
  max: l = 100,
  thumbText: a,
  minText: g,
  maxText: h,
  minMaxTextColor: N,
  variant: C = "primary",
  stepValue: m,
  title: D,
  supportText: P,
  ...R
}) {
  const v = B(null), [b, y] = S(t), x = (e) => 100 * (e - n) / (l - n), [f, j] = S(x(t)), E = (e) => (m && (e = J(e, m)), e < n ? n : e > l ? l : e), M = (e, r) => {
    const c = e.getBoundingClientRect().left, i = K(r, c, e.offsetWidth), u = Math.round((i - c) / e.offsetWidth * 100);
    j(u);
    const p = Math.round(n + (l - n) * u / 100);
    s(E(p));
  }, L = (e) => (r) => {
    if (!r.touches[0]) return;
    const c = r.touches[0].pageX;
    M(e, c);
  }, T = (e) => (r) => {
    M(e, r.pageX);
  };
  return W(() => {
    t !== b && (j(x(t)), y(E(t)));
  }, [t]), W(() => {
    const e = v.current;
    if (e) {
      const r = T(e), c = L(e), i = () => {
        document.removeEventListener("mousemove", r), document.removeEventListener("mouseup", i);
      }, u = (d) => {
        d.preventDefault(), document.addEventListener("mousemove", r), document.addEventListener("mouseup", i), T(e)(d);
      }, p = (d) => {
        d.preventDefault(), L(e)(d);
      };
      return e.addEventListener("mousedown", u), e.addEventListener("touchmove", c), e.addEventListener("touchstart", p), e.addEventListener("touchend", i), () => {
        e.removeEventListener("mousedown", u), e.removeEventListener("touchmove", c), e.removeEventListener("touchstart", p), e.removeEventListener("touchend", i);
      };
    }
  }, []), /* @__PURE__ */ o.jsxs(X, { ...R, children: [
    /* @__PURE__ */ o.jsxs(k, { children: [
      /* @__PURE__ */ o.jsxs(V, { children: [
        /* @__PURE__ */ o.jsx(w, { children: D }),
        /* @__PURE__ */ o.jsx(I, { children: P })
      ] }),
      /* @__PURE__ */ o.jsxs(U, { ref: v, isThumbText: !!a, children: [
        /* @__PURE__ */ o.jsx(q, { type: "hidden", value: t, "data-testid": "slider-input" }),
        /* @__PURE__ */ o.jsx(z, { position: f, trackColor: C }),
        /* @__PURE__ */ o.jsxs(A, { position: f, children: [
          /* @__PURE__ */ o.jsx(F, {}),
          /* @__PURE__ */ o.jsx(G, { children: a })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ o.jsxs(H, { onlyMax: !!h, children: [
      /* @__PURE__ */ o.jsx("span", { children: g }),
      /* @__PURE__ */ o.jsx("span", { children: h })
    ] })
  ] });
}
export {
  Z as Slider
};
