import { defineComponent as x, shallowRef as m, ref as V, toRef as h, watch as W, watchEffect as z, unref as o, openBlock as A, createElementBlock as N, normalizeStyle as U, normalizeClass as k, createElementVNode as L } from "vue";
import "../config-provider/index.mjs";
import { useTrack as X } from "./hooks/use-track.mjs";
import Y from "./style/index.mjs";
import { ensureRangeValue as C } from "./utils/index.mjs";
import { useNamespace as I } from "../config-provider/hooks/use-namespace.mjs";
const K = /* @__PURE__ */ x({
  __name: "internal",
  props: {
    direction: { default: "vertical" },
    scroll: { default: 0 },
    barLength: { default: 35 },
    width: {},
    barColor: {},
    trackColor: {},
    trackWidth: {},
    disabled: { type: Boolean, default: !1 },
    showTrack: { type: Boolean, default: !0 },
    trackSpeed: { default: 2 },
    onScrollStart: {},
    onScroll: {},
    onScrollEnd: {}
  },
  setup(r, { expose: D }) {
    const t = r, v = m(), i = m(), a = m(), { b: M, e: p, m: R, em: s, cssVar: B } = I("scroll-bar"), P = Y("scroll-bar"), l = V(t.scroll), { handleMouseDown: g } = X({
      currentScroll: l,
      track: i,
      bar: a,
      type: h(t, "direction"),
      trackSpeed: h(t, "trackSpeed"),
      barLength: h(t, "barLength"),
      disabled: h(t, "disabled"),
      onDown: (e) => {
        t.onScrollStart?.(e);
      },
      onUp: (e) => {
        t.onScrollEnd?.(e);
      },
      onScroll: (e) => {
        t.onScroll?.(e);
      }
    });
    W(
      () => t.scroll,
      (e) => {
        l.value = e;
      }
    ), z(() => {
      if (!o(a)) return;
      const e = `${(100 - t.barLength) * l.value / t.barLength}%`, n = `${t.barLength}%`;
      t.direction === "vertical" ? (a.value.style.height = n, a.value.style.transform = `translate3d(0, ${e}, 0)`) : (a.value.style.width = n, a.value.style.transform = `translate3d(${e}, 0, 0)`);
    });
    let b, d, u;
    function T(e) {
      if (t.disabled || e.button !== 0) return;
      e.preventDefault(), e.stopPropagation();
      const n = o(i), c = o(a);
      if (!n || !c)
        return;
      document.addEventListener("pointermove", S), document.addEventListener("pointerup", w);
      const f = n.getBoundingClientRect(), E = c.getBoundingClientRect();
      t.direction === "vertical" ? (b = f.height, d = E.top - f.top, u = e.clientY) : (b = f.width, d = E.left - f.left, u = e.clientX), t.onScrollStart?.(o(l));
    }
    function S(e) {
      e.stopPropagation(), e.preventDefault();
      let n;
      t.direction === "vertical" ? n = d + e.clientY - u : n = d + e.clientX - u;
      const c = n / b / (100 - t.barLength) * 1e4;
      l.value = C(c), t.onScroll?.(o(l));
    }
    function w(e) {
      e.preventDefault(), document.removeEventListener("pointermove", S), document.removeEventListener("pointerup", w), t.onScrollEnd?.(o(l));
    }
    function y(e) {
      e.cancelable && (e.stopPropagation(), e.preventDefault());
    }
    function $(e) {
      Math.abs(o(l) - e) <= 1e-3 || (l.value = C(e));
    }
    return D({
      currentScroll: l,
      container: v,
      bar: a,
      track: i,
      scroll: $
    }), (e, n) => (A(), N("div", {
      ref_key: "container",
      ref: v,
      class: k([o(M)(), o(R)(r.direction), o(P)]),
      style: U(
        o(B)(
          {
            "bar-color": r.barColor,
            "track-color": r.trackColor,
            "bar-width": r.width ? `${r.width}px` : void 0,
            "track-width": r.trackWidth ? `${r.trackWidth}px` : void 0
          },
          !1
        )
      )
    }, [
      L("div", {
        ref_key: "track",
        ref: i,
        class: k([
          o(p)("track"),
          r.disabled ? o(s)("track", "disabled") : null,
          r.showTrack ? null : o(s)("track", "hidden")
        ]),
        onPointerdown: n[0] || (n[0] = //@ts-ignore
        (...c) => o(g) && o(g)(...c)),
        onTouchstart: y
      }, null, 34),
      L("div", {
        ref_key: "bar",
        ref: a,
        class: k([
          o(p)("bar"),
          o(s)("bar", r.direction),
          r.disabled ? o(s)("bar", "disabled") : null
        ]),
        onPointerdown: T,
        onTouchstart: y
      }, null, 34)
    ], 6));
  }
});
export {
  K as default
};
