"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const raf = require("../../../../utils/raf.js");
const index = require("../../../../hooks/use-namespace/index.js");
const util = require("../../../scrollbar/src/util.js");
const defaults = require("../defaults.js");
const props = require("../props.js");
const utils = require("../utils.js");
const Vue = require("vue");
const ScrollBar = Vue.defineComponent({
  name: "ElVirtualScrollBar",
  props: props.virtualizedScrollbarProps,
  emits: [
    "scroll",
    "start-move",
    "stop-move"
  ],
  setup(props2, { emit }) {
    const GAP = Vue.computed(() => props2.startGap + props2.endGap);
    const nsVirtualScrollbar = index.useNamespace("virtual-scrollbar");
    const nsScrollbar = index.useNamespace("scrollbar");
    const trackRef = Vue.ref();
    const thumbRef = Vue.ref();
    let frameHandle = null;
    let onselectstartStore = null;
    const state = Vue.reactive({
      isDragging: false,
      traveled: 0
    });
    const bar = Vue.computed(() => util.BAR_MAP[props2.layout]);
    const trackSize = Vue.computed(() => props2.clientSize - Vue.unref(GAP));
    const trackStyle = Vue.computed(() => ({
      position: "absolute",
      width: `${defaults.HORIZONTAL === props2.layout ? trackSize.value : props2.scrollbarSize}px`,
      height: `${defaults.HORIZONTAL === props2.layout ? props2.scrollbarSize : trackSize.value}px`,
      [defaults.ScrollbarDirKey[props2.layout]]: "2px",
      right: "2px",
      bottom: "2px",
      borderRadius: "4px"
    }));
    const thumbSize = Vue.computed(() => {
      const ratio = props2.ratio;
      if (ratio >= 100) return Number.POSITIVE_INFINITY;
      if (ratio >= 50) return ratio * trackSize.value / 100;
      const SCROLLBAR_MAX_SIZE = trackSize.value / 3;
      return Math.floor(Math.min(Math.max(ratio * trackSize.value / 100, defaults.SCROLLBAR_MIN_SIZE), SCROLLBAR_MAX_SIZE));
    });
    const thumbStyle = Vue.computed(() => {
      if (!Number.isFinite(thumbSize.value)) return { display: "none" };
      const thumb = `${thumbSize.value}px`;
      return utils.renderThumbStyle({
        bar: bar.value,
        size: thumb,
        move: state.traveled
      }, props2.layout);
    });
    const totalSteps = Vue.computed(() => Math.ceil(props2.clientSize - thumbSize.value - Vue.unref(GAP)));
    const attachEvents = () => {
      window.addEventListener("mousemove", onMouseMove);
      window.addEventListener("mouseup", onMouseUp);
      const thumbEl = Vue.unref(thumbRef);
      if (!thumbEl) return;
      onselectstartStore = document.onselectstart;
      document.onselectstart = () => false;
      thumbEl.addEventListener("touchmove", onMouseMove, { passive: true });
      thumbEl.addEventListener("touchend", onMouseUp);
    };
    const detachEvents = () => {
      window.removeEventListener("mousemove", onMouseMove);
      window.removeEventListener("mouseup", onMouseUp);
      document.onselectstart = onselectstartStore;
      onselectstartStore = null;
      const thumbEl = Vue.unref(thumbRef);
      if (!thumbEl) return;
      thumbEl.removeEventListener("touchmove", onMouseMove);
      thumbEl.removeEventListener("touchend", onMouseUp);
    };
    const onThumbMouseDown = (e) => {
      e.stopImmediatePropagation();
      if (e.ctrlKey || [1, 2].includes(e.button)) return;
      state.isDragging = true;
      state[bar.value.axis] = e.currentTarget[bar.value.offset] - (e[bar.value.client] - e.currentTarget.getBoundingClientRect()[bar.value.direction]);
      emit("start-move");
      attachEvents();
    };
    const onMouseUp = () => {
      state.isDragging = false;
      state[bar.value.axis] = 0;
      emit("stop-move");
      detachEvents();
    };
    const onMouseMove = (e) => {
      const { isDragging } = state;
      if (!isDragging) return;
      if (!thumbRef.value || !trackRef.value) return;
      const prevPage = state[bar.value.axis];
      if (!prevPage) return;
      raf.cAF(frameHandle);
      const distance = (trackRef.value.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) * -1 - (thumbRef.value[bar.value.offset] - prevPage);
      frameHandle = raf.rAF(() => {
        state.traveled = Math.max(0, Math.min(distance, totalSteps.value));
        emit("scroll", distance, totalSteps.value);
      });
    };
    const clickTrackHandler = (e) => {
      const distance = Math.abs(e.target.getBoundingClientRect()[bar.value.direction] - e[bar.value.client]) - thumbRef.value[bar.value.offset] / 2;
      state.traveled = Math.max(0, Math.min(distance, totalSteps.value));
      emit("scroll", distance, totalSteps.value);
    };
    Vue.watch(() => props2.scrollFrom, (v) => {
      if (state.isDragging) return;
      state.traveled = Math.ceil(v * totalSteps.value);
    });
    Vue.onBeforeUnmount(() => {
      detachEvents();
    });
    return () => {
      return Vue.h("div", {
        role: "presentation",
        ref: trackRef,
        class: [
          nsVirtualScrollbar.b(),
          props2.class,
          (props2.alwaysOn || state.isDragging) && "always-on"
        ],
        style: trackStyle.value,
        onMousedown: Vue.withModifiers(clickTrackHandler, ["stop", "prevent"]),
        onTouchstartPrevent: onThumbMouseDown
      }, Vue.h("div", {
        ref: thumbRef,
        class: nsScrollbar.e("thumb"),
        style: thumbStyle.value,
        onMousedown: onThumbMouseDown
      }, []));
    };
  }
});
exports.default = ScrollBar;
