"use strict";
const vue = require("vue");
const onResize = require("../directives/on-resize.js");
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  __name: "ScrollbarRail",
  props: {
    direction: { default: "y" },
    thumbRate: {},
    offsetRate: { default: 0 },
    notStepJump: { type: Boolean },
    size: { default: "medium" }
  },
  emits: ["scroll"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const isY = vue.computed(() => props.direction === "y");
    const isDarggingBar = vue.ref(false);
    const barRef = vue.ref(null);
    const thumbRef = vue.ref(null);
    const trackLength = vue.ref(0);
    const thumbSize = vue.computed(() => {
      if (trackLength.value && props.thumbRate) {
        return Math.round(trackLength.value * props.thumbRate);
      }
      return 0;
    });
    const thumbSizeStyle = vue.computed(() => {
      return `${thumbSize.value}px`;
    });
    const maxOffset = vue.computed(() => trackLength.value - thumbSize.value);
    const sizeProp = vue.computed(() => {
      return isY.value ? "height" : "width";
    });
    const offsetProp = vue.computed(() => {
      return isY.value ? "translateY" : "translateX";
    });
    const offset = vue.ref(0);
    vue.watch(
      () => [props.offsetRate, trackLength.value],
      (val) => {
        if (!isDarggingBar.value) {
          offset.value = Math.round(val[1] * val[0]);
        }
      },
      {
        immediate: true
      }
    );
    const offsetStyle = vue.computed(() => {
      return `${offsetProp.value}(${offset.value}px)`;
    });
    const adjustOffset = (pos) => {
      if (pos < 0) {
        return 0;
      }
      if (pos > maxOffset.value) {
        return maxOffset.value;
      }
      return pos;
    };
    vue.onMounted(() => {
      if (!barRef.value) {
        return;
      }
      const { offsetHeight, offsetWidth } = barRef.value;
      trackLength.value = props.direction === "x" ? offsetWidth : offsetHeight;
    });
    let s = 0;
    let oldOffset = 0;
    const onMouseMove = (e) => {
      const pos = isY.value ? e.clientY : e.clientX;
      const v = oldOffset + pos - s;
      const of = adjustOffset(v);
      if (of !== offset.value) {
        offset.value = of;
        emits("scroll", offset.value / trackLength.value);
      }
    };
    const onMouseUp = () => {
      isDarggingBar.value = false;
      window.removeEventListener("mousemove", onMouseMove);
      window.removeEventListener("mouseup", onMouseUp);
      window.removeEventListener("contextmenu", onMouseUp);
    };
    const onThumbMouseDown = (e) => {
      e.preventDefault();
      e.stopPropagation();
      isDarggingBar.value = true;
      s = isY.value ? e.clientY : e.clientX;
      oldOffset = offset.value;
      window.addEventListener("mousemove", onMouseMove);
      window.addEventListener("mouseup", onMouseUp);
      window.addEventListener("contextmenu", onMouseUp);
    };
    const onTrackClick = (e) => {
      e.preventDefault();
      if (!thumbRef.value || !barRef.value) {
        return;
      }
      const pos = isY.value ? e.clientY : e.clientX;
      let v = 0;
      if (props.notStepJump) {
        const bc = barRef.value.getBoundingClientRect();
        v = pos - (isY.value ? bc.top : bc.left) - thumbSize.value / 2;
      } else {
        const bc = thumbRef.value.getBoundingClientRect();
        const isPlus = pos > (isY.value ? bc.top : bc.left);
        v = offset.value + thumbSize.value * (isPlus ? 1 : -1);
      }
      const of = adjustOffset(v);
      if (of !== offset.value) {
        offset.value = of;
        emits("scroll", offset.value / trackLength.value);
      }
    };
    const onResize$1 = () => {
      if (!barRef.value) {
        return;
      }
      const { offsetHeight, offsetWidth } = barRef.value;
      trackLength.value = props.direction === "x" ? offsetWidth : offsetHeight;
    };
    return (_ctx, _cache) => {
      return vue.withDirectives((vue.openBlock(), vue.createElementBlock(
        "div",
        {
          ref_key: "barRef",
          ref: barRef,
          class: vue.normalizeClass(["o-scrollbar-rail", [
            `o-scrollbar-${props.direction}`,
            `o-scrollbar-${props.size}`,
            {
              "o-scrollbar-dragging": isDarggingBar.value
            }
          ]]),
          onClick: onTrackClick
        },
        [
          vue.createElementVNode(
            "div",
            {
              ref_key: "thumbRef",
              ref: thumbRef,
              class: vue.normalizeClass(["o-scrollbar-thumb", [
                `o-scrollbar-${props.direction}-thumb`,
                {
                  [`o-scrollbar-${props.direction}-thumb-dragging`]: isDarggingBar.value
                }
              ]]),
              style: vue.normalizeStyle({
                [sizeProp.value]: thumbSizeStyle.value,
                transform: offsetStyle.value
              }),
              onClick: _cache[0] || (_cache[0] = vue.withModifiers(() => {
              }, ["stop"])),
              onMousedown: onThumbMouseDown
            },
            [
              vue.renderSlot(_ctx.$slots, "thumb", {
                direction: props.direction,
                dragging: isDarggingBar.value
              }, () => [
                vue.createElementVNode(
                  "div",
                  {
                    class: vue.normalizeClass([
                      `o-scrollbar-${props.direction}-thumb-bar`,
                      {
                        "is-dragging": isDarggingBar.value
                      }
                    ])
                  },
                  null,
                  2
                  /* CLASS */
                )
              ])
            ],
            38
            /* CLASS, STYLE, NEED_HYDRATION */
          ),
          vue.renderSlot(_ctx.$slots, "track", {
            direction: props.direction
          }, () => [
            vue.createElementVNode(
              "div",
              {
                class: vue.normalizeClass(["o-scrollbar-track", [`o-scrollbar-${props.direction}-track`]])
              },
              null,
              2
              /* CLASS */
            )
          ])
        ],
        2
        /* CLASS */
      )), [
        [vue.unref(onResize.vOnResize), onResize$1]
      ]);
    };
  }
});
module.exports = _sfc_main;
