import { useRef, useCallback } from 'react';
import { debounce } from 'lodash';
export var useScrollEvents = function useScrollEvents(_ref) {
  var containerRef = _ref.containerRef,
    onScroll = _ref.onScroll,
    onScrollStart = _ref.onScrollStart,
    onScrollEnd = _ref.onScrollEnd,
    onReachTop = _ref.onReachTop,
    onReachBottom = _ref.onReachBottom,
    onReachLeft = _ref.onReachLeft,
    onReachRight = _ref.onReachRight,
    _ref$reachThreshold = _ref.reachThreshold,
    reachThreshold = _ref$reachThreshold === void 0 ? 0 : _ref$reachThreshold,
    _ref$scrollEndDelay = _ref.scrollEndDelay,
    scrollEndDelay = _ref$scrollEndDelay === void 0 ? 150 : _ref$scrollEndDelay,
    updateScrollState = _ref.updateScrollState,
    scrollState = _ref.scrollState;
  var isScrolling = useRef(false);

  // 滚动结束处理（防抖）
  var handleScrollEnd = useCallback(debounce(function (finalState) {
    isScrolling.current = false;
    onScrollEnd === null || onScrollEnd === void 0 || onScrollEnd(finalState);
  }, scrollEndDelay), [onScrollEnd, scrollEndDelay]);
  var handleScroll = useCallback(function () {
    var container = containerRef.current;
    if (!container) return;
    updateScrollState();

    // 获取最新的状态需要直接从 DOM 拿，因为 state 更新是异步的
    // 为了简化，这里我们在下一帧或通过 ref 获取实时数据可能会更准，
    // 但复用 updateScrollState 逻辑保持一致性
    // 这里我们直接构造临时数据用于触发回调
    var scrollTop = container.scrollTop,
      scrollLeft = container.scrollLeft,
      scrollHeight = container.scrollHeight,
      scrollWidth = container.scrollWidth,
      clientHeight = container.clientHeight,
      clientWidth = container.clientWidth;

    // 简单的方向计算需要依赖上一次状态，这里暂简化
    // 实际使用中 scrollState 的更新会有延迟，所以 callback 中的 data 最好实时计算

    var maxScrollTop = Math.max(0, scrollHeight - clientHeight);
    var maxScrollLeft = Math.max(0, scrollWidth - clientWidth);
    var percentY = maxScrollTop > 0 ? scrollTop / maxScrollTop * 100 : 0;
    var percentX = maxScrollLeft > 0 ? scrollLeft / maxScrollLeft * 100 : 0;

    // 计算边界状态
    var isScrollToTop = scrollTop <= reachThreshold;
    var isScrollToBottom = scrollTop + clientHeight >= scrollHeight - reachThreshold;
    var isScrollToLeft = scrollLeft <= reachThreshold;
    var isScrollToRight = scrollLeft + clientWidth >= scrollWidth - reachThreshold;

    // 计算是否溢出
    var isOverflows = scrollHeight > clientHeight || scrollWidth > clientWidth;
    var currentData = {
      scrollTop: scrollTop,
      scrollLeft: scrollLeft,
      scrollHeight: scrollHeight,
      scrollWidth: scrollWidth,
      clientHeight: clientHeight,
      clientWidth: clientWidth,
      percentY: percentY,
      percentX: percentX,
      direction: null,
      // 在这里计算方向比较麻烦，可以使用 scrollState 中的（虽然有一点延迟）
      isScrollToTop: isScrollToTop,
      isScrollToBottom: isScrollToBottom,
      isScrollToLeft: isScrollToLeft,
      isScrollToRight: isScrollToRight,
      isOverflows: isOverflows
    };

    // 触发 ScrollStart
    if (!isScrolling.current) {
      isScrolling.current = true;
      onScrollStart === null || onScrollStart === void 0 || onScrollStart(currentData);
    }

    // 触发 Scroll
    onScroll === null || onScroll === void 0 || onScroll(currentData);

    // 触发边界事件
    if (scrollTop <= reachThreshold) onReachTop === null || onReachTop === void 0 || onReachTop(currentData);
    if (scrollTop + clientHeight >= scrollHeight - reachThreshold) onReachBottom === null || onReachBottom === void 0 || onReachBottom(currentData);
    if (scrollLeft <= reachThreshold) onReachLeft === null || onReachLeft === void 0 || onReachLeft(currentData);
    if (scrollLeft + clientWidth >= scrollWidth - reachThreshold) onReachRight === null || onReachRight === void 0 || onReachRight(currentData);

    // 触发 ScrollEnd
    handleScrollEnd(currentData);
  }, [containerRef, updateScrollState, onScroll, onScrollStart, handleScrollEnd, onReachTop, onReachBottom, onReachLeft, onReachRight, reachThreshold]);
  return {
    handleScroll: handleScroll
  };
};