/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var vue = require('vue');
var isFunction = require('lodash/isFunction');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

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

exports["default"] = useTagScroll;
//# sourceMappingURL=useTagScroll.js.map
