import { defineComponent, inject, ref, computed, onBeforeUnmount, createBlock, toRef, openBlock, Transition, unref, withCtx, withDirectives, createElementVNode, normalizeClass, normalizeStyle, vShow } from "vue";
import { useEventListener as v } from "../../../../../@vueuse/core/index.mjs";
import { scrollbarContextKey as o$1 } from "./constants.mjs";
import { BAR_MAP as o$2, renderThumbStyle as s } from "./util.mjs";
import { thumbProps as o } from "./thumb.mjs";
import c from "../../../_virtual/plugin-vue_export-helper.mjs";
import { useNamespace as q } from "../../../hooks/use-namespace/index.mjs";
import { throwError as i } from "../../../utils/error.mjs";
import { isClient as f } from "../../../../../@vueuse/shared/index.mjs";
const Z = "Thumb", $ = /* @__PURE__ */ defineComponent({
  __name: "thumb",
  props: o,
  setup(k) {
    const u = k, o2 = inject(o$1), c2 = q("scrollbar");
    o2 || i(Z, "can not inject scrollbar context");
    const l = ref(), a = ref(), f$1 = ref({}), i$1 = ref(false);
    let m = false, d = false, p = f ? document.onselectstart : null;
    const e = computed(() => o$2[u.vertical ? "vertical" : "horizontal"]), z = computed(() => s({
      size: u.size,
      move: u.move,
      bar: e.value
    })), g = computed(() => l.value[e.value.offset] ** 2 / o2.wrapElement[e.value.scrollSize] / u.ratio / a.value[e.value.offset]), x = (t) => {
      var n;
      if (t.stopPropagation(), t.ctrlKey || [1, 2].includes(t.button))
        return;
      (n = window.getSelection()) == null || n.removeAllRanges(), M(t);
      const r = t.currentTarget;
      r && (f$1.value[e.value.axis] = r[e.value.offset] - (t[e.value.client] - r.getBoundingClientRect()[e.value.direction]));
    }, L = (t) => {
      if (!a.value || !l.value || !o2.wrapElement)
        return;
      const n = Math.abs(t.target.getBoundingClientRect()[e.value.direction] - t[e.value.client]), r = a.value[e.value.offset] / 2, h = (n - r) * 100 * g.value / l.value[e.value.offset];
      o2.wrapElement[e.value.scroll] = h * o2.wrapElement[e.value.scrollSize] / 100;
    }, M = (t) => {
      t.stopImmediatePropagation(), m = true, document.addEventListener("mousemove", w), document.addEventListener("mouseup", b), p = document.onselectstart, document.onselectstart = () => false;
    }, w = (t) => {
      if (!l.value || !a.value || m === false)
        return;
      const n = f$1.value[e.value.axis];
      if (!n)
        return;
      const r = (l.value.getBoundingClientRect()[e.value.direction] - t[e.value.client]) * -1, h = a.value[e.value.offset] - n, H = (r - h) * 100 * g.value / l.value[e.value.offset];
      o2.wrapElement[e.value.scroll] = H * o2.wrapElement[e.value.scrollSize] / 100;
    }, b = () => {
      m = false, f$1.value[e.value.axis] = 0, document.removeEventListener("mousemove", w), document.removeEventListener("mouseup", b), _(), d && (i$1.value = false);
    }, T = () => {
      d = false, i$1.value = !!u.size;
    }, B = () => {
      d = true, i$1.value = m;
    };
    onBeforeUnmount(() => {
      _(), document.removeEventListener("mouseup", b);
    });
    const _ = () => {
      document.onselectstart !== p && (document.onselectstart = p);
    };
    return v(toRef(o2, "scrollbarElement"), "mousemove", T), v(toRef(o2, "scrollbarElement"), "mouseleave", B), (t, n) => (openBlock(), createBlock(Transition, {
      name: unref(c2).b("fade"),
      persisted: ""
    }, {
      default: withCtx(() => [
        withDirectives(createElementVNode("div", {
          ref_key: "instance",
          ref: l,
          class: normalizeClass([unref(c2).e("bar"), unref(c2).is(unref(e).key)]),
          onMousedown: L
        }, [
          createElementVNode("div", {
            ref_key: "thumb",
            ref: a,
            class: normalizeClass(unref(c2).e("thumb")),
            style: normalizeStyle(unref(z)),
            onMousedown: x
          }, null, 38)
        ], 34), [
          [vShow, t.always || i$1.value]
        ])
      ]),
      _: 1
    }, 8, ["name"]));
  }
});
var ce = /* @__PURE__ */ c($, [["__file", "thumb.vue"]]);
export {
  ce as default
};
