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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var hooks_useVirtualScrollNew = require('../../hooks/useVirtualScrollNew.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/slicedToArray');
require('../../hooks/useResizeObserver.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useTreeScroll(props, context, state) {
  var treeState = state;
  var scope = treeState.scope,
    treeContentRef = treeState.treeContentRef,
    nodes = treeState.nodes,
    isScrolling = treeState.isScrolling;
  var scrollProps = VueCompositionAPI.computed(function () {
    return _objectSpread({
      rowHeight: 34
    }, props.scroll);
  });
  scope.scrollProps = scrollProps;
  var virtualScrollParams = VueCompositionAPI.computed(function () {
    var list = nodes.value.filter(function (node) {
      return node.visible;
    });
    return {
      data: list,
      scroll: scrollProps.value
    };
  });
  var virtualConfig = hooks_useVirtualScrollNew["default"](treeContentRef, virtualScrollParams);
  scope.virtualConfig = virtualConfig;
  treeState.virtualConfig = virtualConfig;
  VueCompositionAPI.onMounted(function () {
    var isVirtual = virtualConfig === null || virtualConfig === void 0 ? void 0 : virtualConfig.isVirtualScroll.value;
    if (isVirtual) virtualConfig.handleScroll();
  });
  var emitScrollEvent = function emitScrollEvent(e) {
    var _props$onScroll;
    (_props$onScroll = props.onScroll) === null || _props$onScroll === void 0 ? void 0 : _props$onScroll.call(props, {
      e: e
    });
    context.emit("scroll", {
      e: e
    });
  };
  var scrollStopTimer = null;
  var setScrolling = function setScrolling() {
    isScrolling.value = true;
    if (scrollStopTimer) {
      clearTimeout(scrollStopTimer);
      scrollStopTimer = null;
    }
    scrollStopTimer = setTimeout(function () {
      scrollStopTimer = null;
      isScrolling.value = false;
    }, 100);
  };
  var lastScrollY = 0;
  var onInnerVirtualScroll = function onInnerVirtualScroll(e) {
    setScrolling();
    var isVirtual = virtualConfig === null || virtualConfig === void 0 ? void 0 : virtualConfig.isVirtualScroll.value;
    var target = e.target || e.srcElement;
    var top = target.scrollTop;
    if (lastScrollY !== top) {
      if (isVirtual) virtualConfig.handleScroll();
    } else {
      lastScrollY = 0;
    }
    lastScrollY = top;
    emitScrollEvent(e);
  };
  return {
    treeContentRef: treeContentRef,
    onInnerVirtualScroll: onInnerVirtualScroll,
    virtualConfig: virtualConfig,
    scrollToElement: virtualConfig.scrollToElement
  };
}

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