import { ref as n } from "vue";
import { ensureRangeValue as w } from "../utils/index.mjs";
const h = () => {
};
function U({
  currentScroll: e = n(0),
  trackSpeed: f = n(2),
  track: p = n(null),
  bar: o = n(null),
  tracking: m = n(!1),
  type: r = n("vertical"),
  barLength: i = n(35),
  disabled: D = n(!1),
  onDown: A = h,
  onMove: B = h,
  onUp: C = h,
  onScroll: P = h
}) {
  let d, s, v, u, M = !0, c = !1;
  function x() {
    if (!m.value) return;
    c = !0;
    const t = q(u, e.value);
    let a = !1;
    if (M) {
      if (e.value < u) {
        const l = e.value + t * f.value;
        e.value = Math.min(l, u), a = !0;
      }
    } else if (e.value > u) {
      const l = e.value - t * f.value;
      e.value = Math.max(l, u), a = !0;
    }
    a && P(e.value), (m.value && M ? e.value < u : e.value > u) ? requestAnimationFrame(x) : c = !1;
  }
  function X(t) {
    if (D.value || t.button !== 0 || (t.stopPropagation(), t.preventDefault(), !p.value || !o.value)) return !1;
    document.addEventListener("pointermove", E), document.addEventListener("pointerup", g);
    const a = p.value.getBoundingClientRect(), l = o.value.getBoundingClientRect();
    let R;
    r.value === "vertical" ? (d = a.height, s = l.top - a.top, v = l.top, R = s + t.clientY - v) : (d = a.width, s = l.left - a.left, v = l.left, R = s + t.clientX - v), M = R / d * 100 > i.value;
    const Y = (R / d * 100 - (M ? i.value : 0)) / (100 - i.value);
    u = w(Y * 100), m.value = !0, A(e.value), x();
  }
  function E(t) {
    t.stopPropagation(), t.preventDefault(), B(e.value);
    let a;
    r.value === "vertical" ? a = s + t.clientY - v : a = s + t.clientX - v;
    const l = (a / d * 100 - (M ? i.value : 0)) / (100 - i.value);
    u = w(l * 100), !c && x();
  }
  function g(t) {
    t.preventDefault(), document.removeEventListener("pointermove", E), document.removeEventListener("pointerup", g), m.value = !1, C(e.value);
  }
  return {
    currentScroll: e,
    trackSpeed: f,
    track: p,
    bar: o,
    tracking: m,
    type: r,
    barLength: i,
    disabled: D,
    handleMouseDown: X
  };
}
function q(e, f) {
  const p = Math.abs(e - f);
  for (let o = 0; o < 5; ++o)
    if (p < (o + 1) * 20)
      return 1 + 0.25 * o;
  return 2;
}
export {
  U as useTrack
};
