"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FastScrollManager = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
/**
 * 快速滚动管理器
 * 抽取 BaseTable 中的快速滚动相关逻辑
 */
var FastScrollManager = exports.FastScrollManager = /*#__PURE__*/function () {
  function FastScrollManager(callbacks) {
    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    (0, _classCallCheck2.default)(this, FastScrollManager);
    // 快速滚动状态
    this.state = {
      lastScrollTime: 0,
      scrollVelocity: 0,
      lastOffsetY: 0
    };
    // 快速滚动标志 - 作为私有属性立即更新
    this.isFastScrolling = false;
    this.callbacks = callbacks;
    // 合并默认配置
    this.config = (0, _extends2.default)({
      overscanSize: 100,
      velocityThreshold: 3,
      distanceMultiplier: 3,
      fastScrollEndDelayHigh: 200,
      fastScrollEndDelayNormal: 150,
      highVelocityThreshold: 5
    }, config);
  }
  /**
   * 检测是否接近底部（剩余滚动距离少于两屏）
   */
  return (0, _createClass2.default)(FastScrollManager, [{
    key: "isNearBottom",
    value: function isNearBottom(currentScrollTop, maxRenderHeight, totalScrollHeight) {
      var remainingScrollDistance = totalScrollHeight - currentScrollTop - maxRenderHeight;
      return remainingScrollDistance < maxRenderHeight;
    }
    /**
     * 处理滚动事件 - 检测和处理快速滚动
     */
  }, {
    key: "handleScrollEvent",
    value: function handleScrollEvent(sizeAndOffset, currentState, dataLength, totalScrollHeight) {
      var currentTime = performance.now();
      var deltaY = Math.abs(sizeAndOffset.offsetY - this.state.lastOffsetY);
      var deltaTime = currentTime - this.state.lastScrollTime;
      // 计算滚动速度 (像素/毫秒)
      this.state.scrollVelocity = deltaTime > 0 ? deltaY / deltaTime : 0;
      // 检测是否接近底部
      var isNearBottom = this.isNearBottom(sizeAndOffset.offsetY, sizeAndOffset.maxRenderHeight, totalScrollHeight);
      // 如果正在快速滚动但接近底部，提前结束快速滚动
      if (this.isFastScrolling && isNearBottom) {
        this.endFastScrolling(sizeAndOffset);
        return; // 让正常渲染逻辑接管
      }
      // 快速滚动判断条件：
      // 1. 滚动距离超过阈值 && 滚动速度超过阈值
      // 2. 当前未在快速滚动状态
      // 3. 未接近底部边界
      var isSignificantChange = deltaY > this.config.overscanSize * this.config.distanceMultiplier;
      var isHighVelocity = this.state.scrollVelocity > this.config.velocityThreshold;
      var shouldStartFastScroll = (isSignificantChange || isHighVelocity) && !this.isFastScrolling && !isNearBottom; // 接近底部时不启动快速滚动
      if (shouldStartFastScroll) {
        this.startFastScrolling(currentState, dataLength);
      }
      // 重置快速滚动结束定时器
      if (this.isFastScrolling) {
        this.resetFastScrollEndTimer(sizeAndOffset);
      }
      // 更新记录
      this.state.lastOffsetY = sizeAndOffset.offsetY;
      this.state.lastScrollTime = currentTime;
    }
    /**
     * 开始快速滚动
     */
  }, {
    key: "startFastScrolling",
    value: function startFastScrolling(currentState, dataLength) {
      this.isFastScrolling = true;
      // 获取当前渲染范围作为缓存
      var currentVerticalRange = this.callbacks.getCurrentRenderRange(currentState.offsetY, currentState.maxRenderHeight, dataLength);
      // 通知外部开始快速滚动
      this.callbacks.onFastScrollStart({
        offsetY: currentState.offsetY,
        maxRenderHeight: currentState.maxRenderHeight,
        maxRenderWidth: currentState.maxRenderWidth,
        verticalRenderRange: currentVerticalRange
      });
    }
    /**
     * 重置快速滚动结束定时器
     */
  }, {
    key: "resetFastScrollEndTimer",
    value: function resetFastScrollEndTimer(sizeAndOffset) {
      var _this = this;
      if (this.fastScrollEndTimer) {
        clearTimeout(this.fastScrollEndTimer);
      }
      var waitTime = this.state.scrollVelocity > this.config.highVelocityThreshold ? this.config.fastScrollEndDelayHigh : this.config.fastScrollEndDelayNormal;
      this.fastScrollEndTimer = window.setTimeout(function () {
        _this.endFastScrolling(sizeAndOffset);
      }, waitTime);
    }
    /**
     * 结束快速滚动
     */
  }, {
    key: "endFastScrolling",
    value: function endFastScrolling(sizeAndOffset) {
      this.isFastScrolling = false;
      this.fastScrollEndTimer = undefined;
      // 通知外部结束快速滚动
      this.callbacks.onFastScrollEnd(sizeAndOffset);
    }
    /**
     * 清理资源
     */
  }, {
    key: "cleanup",
    value: function cleanup() {
      if (this.fastScrollEndTimer) {
        clearTimeout(this.fastScrollEndTimer);
      }
    }
    /**
     * 获取当前是否处于快速滚动状态
     */
  }, {
    key: "getIsFastScrolling",
    value: function getIsFastScrolling() {
      return this.isFastScrolling;
    }
    /**
     * 获取当前滚动速度
     */
  }, {
    key: "getScrollVelocity",
    value: function getScrollVelocity() {
      return this.state.scrollVelocity;
    }
    /**
     * 清理资源
     */
  }, {
    key: "destroy",
    value: function destroy() {
      if (this.fastScrollEndTimer) {
        clearTimeout(this.fastScrollEndTimer);
        this.fastScrollEndTimer = undefined;
      }
      this.isFastScrolling = false;
    }
    /**
     * 获取当前配置
     */
  }, {
    key: "getConfig",
    value: function getConfig() {
      return (0, _extends2.default)({}, this.config);
    }
  }]);
}();