import { addPointerEvent as m } from "../../events/add-pointer-event.es.js";
import { extractEventInfo as S } from "../../events/event-info.es.js";
import { distance2D as T } from "../../utils/distance.es.js";
import { frameData as x, frame as D, cancelFrame as W } from "../../../../../../../motion-dom@12.23.23/node_modules/motion-dom/dist/es/frameloop/frame.es.js";
import { isPrimaryPointer as L } from "../../../../../../../motion-dom@12.23.23/node_modules/motion-dom/dist/es/gestures/utils/is-primary-pointer.es.js";
import { pipe as U } from "../../../../../../../motion-utils@12.23.6/node_modules/motion-utils/dist/es/pipe.es.js";
import { secondsToMilliseconds as O, millisecondsToSeconds as V } from "../../../../../../../motion-utils@12.23.6/node_modules/motion-utils/dist/es/time-conversion.es.js";
class C {
  constructor(n, o, { transformPagePoint: e, contextWindow: r = window, dragSnapToOrigin: h = !1, distanceThreshold: s = 3 } = {}) {
    if (this.startEvent = null, this.lastMoveEvent = null, this.lastMoveEventInfo = null, this.handlers = {}, this.contextWindow = window, this.updatePoint = () => {
      if (!(this.lastMoveEvent && this.lastMoveEventInfo))
        return;
      const i = p(this.lastMoveEventInfo, this.history), a = this.startEvent !== null, c = T(i.offset, { x: 0, y: 0 }) >= this.distanceThreshold;
      if (!a && !c)
        return;
      const { point: f } = i, { timestamp: d } = x;
      this.history.push({ ...f, timestamp: d });
      const { onStart: l, onMove: E } = this.handlers;
      a || (l && l(this.lastMoveEvent, i), this.startEvent = this.lastMoveEvent), E && E(this.lastMoveEvent, i);
    }, this.handlePointerMove = (i, a) => {
      this.lastMoveEvent = i, this.lastMoveEventInfo = v(a, this.transformPagePoint), D.update(this.updatePoint, !0);
    }, this.handlePointerUp = (i, a) => {
      this.end();
      const { onEnd: c, onSessionEnd: f, resumeAnimation: d } = this.handlers;
      if (this.dragSnapToOrigin && d && d(), !(this.lastMoveEvent && this.lastMoveEventInfo))
        return;
      const l = p(i.type === "pointercancel" ? this.lastMoveEventInfo : v(a, this.transformPagePoint), this.history);
      this.startEvent && c && c(i, l), f && f(i, l);
    }, !L(n))
      return;
    this.dragSnapToOrigin = h, this.handlers = o, this.transformPagePoint = e, this.distanceThreshold = s, this.contextWindow = r || window;
    const g = S(n), u = v(g, this.transformPagePoint), { point: w } = u, { timestamp: I } = x;
    this.history = [{ ...w, timestamp: I }];
    const { onSessionStart: P } = o;
    P && P(n, p(u, this.history)), this.removeListeners = U(m(this.contextWindow, "pointermove", this.handlePointerMove), m(this.contextWindow, "pointerup", this.handlePointerUp), m(this.contextWindow, "pointercancel", this.handlePointerUp));
  }
  updateHandlers(n) {
    this.handlers = n;
  }
  end() {
    this.removeListeners && this.removeListeners(), W(this.updatePoint);
  }
}
function v(t, n) {
  return n ? { point: n(t.point) } : t;
}
function y(t, n) {
  return { x: t.x - n.x, y: t.y - n.y };
}
function p({ point: t }, n) {
  return {
    point: t,
    delta: y(t, M(n)),
    offset: y(t, b(n)),
    velocity: k(n, 0.1)
  };
}
function b(t) {
  return t[0];
}
function M(t) {
  return t[t.length - 1];
}
function k(t, n) {
  if (t.length < 2)
    return { x: 0, y: 0 };
  let o = t.length - 1, e = null;
  const r = M(t);
  for (; o >= 0 && (e = t[o], !(r.timestamp - e.timestamp > O(n))); )
    o--;
  if (!e)
    return { x: 0, y: 0 };
  const h = V(r.timestamp - e.timestamp);
  if (h === 0)
    return { x: 0, y: 0 };
  const s = {
    x: (r.x - e.x) / h,
    y: (r.y - e.y) / h
  };
  return s.x === 1 / 0 && (s.x = 0), s.y === 1 / 0 && (s.y = 0), s;
}
export {
  C as PanSession
};
//# sourceMappingURL=PanSession.es.js.map
