/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { ref, toRefs, onMounted, onUnmounted } from '@vue/composition-api';

function useTagScroll(props) {
  const tagInputRef = ref();
  const {
    excessTagsDisplayType,
    readonly,
    disabled
  } = toRefs(props);
  const scrollDistance = ref(0);
  const scrollElement = ref();
  const mouseEnterTimer = 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 => {
    var _scrollElement$value;
    (_scrollElement$value = scrollElement.value) === null || _scrollElement$value === void 0 ? void 0 : _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 = () => {
    var _tagInputRef$value;
    const element = (_tagInputRef$value = tagInputRef.value) === null || _tagInputRef$value === void 0 ? void 0 : _tagInputRef$value.$el;
    if (!element) return;
    updateScrollElement(element);
  };
  const clear = () => {
    clearTimeout(mouseEnterTimer.value);
  };
  onMounted(init);
  onUnmounted(clear);
  return {
    tagInputRef,
    scrollElement,
    scrollDistance,
    scrollTo,
    scrollToRight,
    scrollToLeft,
    updateScrollElement,
    updateScrollDistance,
    onWheel,
    scrollToRightOnEnter,
    scrollToLeftOnLeave
  };
}

export { useTagScroll as default };
//# sourceMappingURL=use-tag-scroll.js.map
