/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var VueCompositionAPI = require('@vue/composition-api');

function useTagScroll(props) {
  const tagInputRef = VueCompositionAPI.ref();
  const {
    excessTagsDisplayType,
    readonly,
    disabled
  } = VueCompositionAPI.toRefs(props);
  const scrollDistance = VueCompositionAPI.ref(0);
  const scrollElement = VueCompositionAPI.ref();
  const mouseEnterTimer = VueCompositionAPI.ref();
  const updateScrollElement = element => {
    const inputElement = element.children[0];
    scrollElement.value = inputElement;
  };
  const updateScrollDistance = () => {
    scrollDistance.value = scrollElement.value.scrollWidth - scrollElement.value.clientWidth;
  };
  const scrollTo = distance => {
    scrollElement.value?.scroll({
      left: distance,
      behavior: "smooth"
    });
  };
  const scrollToRight = () => {
    updateScrollDistance();
    scrollTo(scrollDistance.value);
  };
  const scrollToLeft = () => {
    scrollTo(0);
  };
  const onWheel = _ref => {
    let {
      e
    } = _ref;
    if (readonly.value || disabled.value) return;
    if (!scrollElement.value) return;
    if (e.deltaX > 0) {
      const distance = Math.min(scrollElement.value.scrollLeft + 120, scrollDistance.value);
      scrollTo(distance);
    } else {
      const distance = Math.max(scrollElement.value.scrollLeft - 120, 0);
      scrollTo(distance);
    }
  };
  const scrollToRightOnEnter = () => {
    if (excessTagsDisplayType.value !== "scroll") return;
    mouseEnterTimer.value = setTimeout(() => {
      scrollToRight();
      clearTimeout(mouseEnterTimer.value);
    }, 100);
  };
  const scrollToLeftOnLeave = () => {
    if (excessTagsDisplayType.value !== "scroll") return;
    scrollTo(0);
    clearTimeout(mouseEnterTimer.value);
  };
  const init = () => {
    const element = tagInputRef.value?.$el;
    if (!element) return;
    updateScrollElement(element);
  };
  const clear = () => {
    clearTimeout(mouseEnterTimer.value);
  };
  VueCompositionAPI.onMounted(init);
  VueCompositionAPI.onUnmounted(clear);
  return {
    tagInputRef,
    scrollElement,
    scrollDistance,
    scrollTo,
    scrollToRight,
    scrollToLeft,
    updateScrollElement,
    updateScrollDistance,
    onWheel,
    scrollToRightOnEnter,
    scrollToLeftOnLeave
  };
}

exports["default"] = useTagScroll;
//# sourceMappingURL=use-tag-scroll.js.map
