import { useCallback, useRef } from 'react';
var DECAY = 0.95;
var MIN_VELOCITY = 0.5;
var SAMPLE_MS = 50;
/**
 * 惯性滚动：根据拖动末段速度在松手后继续滚动并衰减
 */
export function useDragInertia(options) {
  var enabled = options.enabled,
    orientation = options.orientation,
    getScrollContainer = options.getScrollContainer;
  var rafId = useRef(null);
  var lastPos = useRef(0);
  var lastTime = useRef(0);
  var velocity = useRef(0);
  var samples = useRef([]);
  var applyScroll = useCallback(function (delta) {
    var el = getScrollContainer();
    if (!el) return;
    if (orientation === 'horizontal') {
      el.scrollLeft = Math.max(0, el.scrollLeft + delta);
    } else {
      el.scrollTop = Math.max(0, el.scrollTop + delta);
    }
  }, [getScrollContainer, orientation]);
  var lastTickTime = useRef(0);
  var tick = useCallback(function (now) {
    var t = now !== null && now !== void 0 ? now : performance.now();
    var dt = lastTickTime.current ? (t - lastTickTime.current) / 1000 : 0.016;
    lastTickTime.current = t;
    var v = velocity.current;
    if (Math.abs(v) < MIN_VELOCITY) {
      velocity.current = 0;
      lastTickTime.current = 0;
      return;
    }
    applyScroll(v * dt);
    velocity.current = v * DECAY;
    rafId.current = requestAnimationFrame(tick);
  }, [applyScroll]);
  var stopInertia = useCallback(function () {
    if (rafId.current != null) {
      cancelAnimationFrame(rafId.current);
      rafId.current = null;
    }
    velocity.current = 0;
    samples.current = [];
  }, []);
  var onPointerDown = useCallback(function (e) {
    if (!enabled) return;
    stopInertia();
    var pos = orientation === 'horizontal' ? e.clientX : e.clientY;
    lastPos.current = pos;
    lastTime.current = Date.now();
    samples.current = [{
      pos: pos,
      t: lastTime.current
    }];
  }, [enabled, orientation, stopInertia]);
  var onPointerMove = useCallback(function (e) {
    if (!enabled) return;
    var pos = orientation === 'horizontal' ? e.clientX : e.clientY;
    var t = Date.now();
    var el = getScrollContainer();
    if (el) {
      var scroll = orientation === 'horizontal' ? el.scrollLeft : el.scrollTop;
      var delta = pos - lastPos.current;
      var maxScroll = orientation === 'horizontal' ? el.scrollWidth - el.clientWidth : el.scrollHeight - el.clientHeight;
      var next = Math.max(0, Math.min(maxScroll, scroll + delta));
      if (orientation === 'horizontal') {
        el.scrollLeft = next;
      } else {
        el.scrollTop = next;
      }
    }
    lastPos.current = pos;
    lastTime.current = t;
    samples.current.push({
      pos: pos,
      t: t
    });
    var cutoff = t - SAMPLE_MS;
    while (samples.current.length > 1 && samples.current[0].t < cutoff) {
      samples.current.shift();
    }
  }, [enabled, orientation, getScrollContainer]);
  var onPointerUp = useCallback(function (e) {
    if (!enabled) return;
    var arr = samples.current;
    if (arr.length < 2) return;
    var first = arr[0];
    var last = arr[arr.length - 1];
    var dt = (last.t - first.t) / 1000;
    if (dt <= 0) return;
    var scrollEl = getScrollContainer();
    if (!scrollEl) return;
    var scroll = orientation === 'horizontal' ? scrollEl.scrollLeft : scrollEl.scrollTop;
    var totalDelta = last.pos - first.pos;
    velocity.current = totalDelta / dt;
    stopInertia();
    rafId.current = requestAnimationFrame(tick);
  }, [enabled, orientation, getScrollContainer, stopInertia, tick]);
  return {
    onPointerDown: onPointerDown,
    onPointerMove: onPointerMove,
    onPointerUp: onPointerUp,
    stopInertia: stopInertia
  };
}