var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellTimeNavigator/hooks/useDragInertia.ts
var useDragInertia_exports = {};
__export(useDragInertia_exports, {
  useDragInertia: () => useDragInertia
});
module.exports = __toCommonJS(useDragInertia_exports);
var import_react = require("react");
var DECAY = 0.95;
var MIN_VELOCITY = 0.5;
var SAMPLE_MS = 50;
function useDragInertia(options) {
  const { enabled, orientation, getScrollContainer } = options;
  const rafId = (0, import_react.useRef)(null);
  const lastPos = (0, import_react.useRef)(0);
  const lastTime = (0, import_react.useRef)(0);
  const velocity = (0, import_react.useRef)(0);
  const samples = (0, import_react.useRef)([]);
  const applyScroll = (0, import_react.useCallback)(
    (delta) => {
      const 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]
  );
  const lastTickTime = (0, import_react.useRef)(0);
  const tick = (0, import_react.useCallback)(
    (now) => {
      const t = now ?? performance.now();
      const dt = lastTickTime.current ? (t - lastTickTime.current) / 1e3 : 0.016;
      lastTickTime.current = t;
      const 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]
  );
  const stopInertia = (0, import_react.useCallback)(() => {
    if (rafId.current != null) {
      cancelAnimationFrame(rafId.current);
      rafId.current = null;
    }
    velocity.current = 0;
    samples.current = [];
  }, []);
  const onPointerDown = (0, import_react.useCallback)(
    (e) => {
      if (!enabled) return;
      stopInertia();
      const pos = orientation === "horizontal" ? e.clientX : e.clientY;
      lastPos.current = pos;
      lastTime.current = Date.now();
      samples.current = [{ pos, t: lastTime.current }];
    },
    [enabled, orientation, stopInertia]
  );
  const onPointerMove = (0, import_react.useCallback)(
    (e) => {
      if (!enabled) return;
      const pos = orientation === "horizontal" ? e.clientX : e.clientY;
      const t = Date.now();
      const el = getScrollContainer();
      if (el) {
        const scroll = orientation === "horizontal" ? el.scrollLeft : el.scrollTop;
        const delta = pos - lastPos.current;
        const maxScroll = orientation === "horizontal" ? el.scrollWidth - el.clientWidth : el.scrollHeight - el.clientHeight;
        const 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, t });
      const cutoff = t - SAMPLE_MS;
      while (samples.current.length > 1 && samples.current[0].t < cutoff) {
        samples.current.shift();
      }
    },
    [enabled, orientation, getScrollContainer]
  );
  const onPointerUp = (0, import_react.useCallback)(
    (e) => {
      if (!enabled) return;
      const arr = samples.current;
      if (arr.length < 2) return;
      const first = arr[0];
      const last = arr[arr.length - 1];
      const dt = (last.t - first.t) / 1e3;
      if (dt <= 0) return;
      const scrollEl = getScrollContainer();
      if (!scrollEl) return;
      const scroll = orientation === "horizontal" ? scrollEl.scrollLeft : scrollEl.scrollTop;
      const totalDelta = last.pos - first.pos;
      velocity.current = totalDelta / dt;
      stopInertia();
      rafId.current = requestAnimationFrame(tick);
    },
    [enabled, orientation, getScrollContainer, stopInertia, tick]
  );
  return { onPointerDown, onPointerMove, onPointerUp, stopInertia };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDragInertia
});
