import { __assign as u } from "../../../../tslib/tslib.es6.es.js";
import { isMouseEvent as D, isTouchEvent as U } from "./utils/event-type.es.js";
import { extractEventInfo as b } from "../events/event-info.es.js";
import L, { getFrameData as x, cancelSync as T } from "../../../../framesync/dist/es/index.es.js";
import { secondsToMilliseconds as V } from "../utils/time-conversion.es.js";
import { addPointerEvent as h } from "../events/use-pointer-event.es.js";
import { distance as k } from "../../../../popmotion/dist/es/utils/distance.es.js";
import { pipe as F } from "../../../../popmotion/dist/es/utils/pipe.es.js";
var N = function() {
  function n(i, o, r) {
    var t = this, v = r === void 0 ? {} : r, a = v.transformPagePoint;
    if (this.startEvent = null, this.lastMoveEvent = null, this.lastMoveEventInfo = null, this.handlers = {}, this.updatePoint = function() {
      if (!!(t.lastMoveEvent && t.lastMoveEventInfo)) {
        var e = c(t.lastMoveEventInfo, t.history), s = t.startEvent !== null, p = k(e.offset, { x: 0, y: 0 }) >= 3;
        if (!(!s && !p)) {
          var m = e.point, l = x().timestamp;
          t.history.push(u(u({}, m), { timestamp: l }));
          var f = t.handlers, y = f.onStart, M = f.onMove;
          s || (y && y(t.lastMoveEvent, e), t.startEvent = t.lastMoveEvent), M && M(t.lastMoveEvent, e);
        }
      }
    }, this.handlePointerMove = function(e, s) {
      if (t.lastMoveEvent = e, t.lastMoveEventInfo = d(s, t.transformPagePoint), D(e) && e.buttons === 0) {
        t.handlePointerUp(e, s);
        return;
      }
      L.update(t.updatePoint, !0);
    }, this.handlePointerUp = function(e, s) {
      t.end();
      var p = t.handlers, m = p.onEnd, l = p.onSessionEnd, f = c(d(s, t.transformPagePoint), t.history);
      t.startEvent && m && m(e, f), l && l(e, f);
    }, !(U(i) && i.touches.length > 1)) {
      this.handlers = o, this.transformPagePoint = a;
      var I = b(i), P = d(I, this.transformPagePoint), w = P.point, _ = x().timestamp;
      this.history = [u(u({}, w), { timestamp: _ })];
      var E = o.onSessionStart;
      E && E(i, c(P, this.history)), this.removeListeners = F(h(window, "pointermove", this.handlePointerMove), h(window, "pointerup", this.handlePointerUp), h(window, "pointercancel", this.handlePointerUp));
    }
  }
  return n.prototype.updateHandlers = function(i) {
    this.handlers = i;
  }, n.prototype.end = function() {
    this.removeListeners && this.removeListeners(), T.update(this.updatePoint);
  }, n;
}();
function d(n, i) {
  return i ? { point: i(n.point) } : n;
}
function g(n, i) {
  return { x: n.x - i.x, y: n.y - i.y };
}
function c(n, i) {
  var o = n.point;
  return {
    point: o,
    delta: g(o, S(i)),
    offset: g(o, H(i)),
    velocity: j(i, 0.1)
  };
}
function H(n) {
  return n[0];
}
function S(n) {
  return n[n.length - 1];
}
function j(n, i) {
  if (n.length < 2)
    return { x: 0, y: 0 };
  for (var o = n.length - 1, r = null, t = S(n); o >= 0 && (r = n[o], !(t.timestamp - r.timestamp > V(i))); )
    o--;
  if (!r)
    return { x: 0, y: 0 };
  var v = (t.timestamp - r.timestamp) / 1e3;
  if (v === 0)
    return { x: 0, y: 0 };
  var a = {
    x: (t.x - r.x) / v,
    y: (t.y - r.y) / v
  };
  return a.x === 1 / 0 && (a.x = 0), a.y === 1 / 0 && (a.y = 0), a;
}
export {
  N as PanSession
};
//# sourceMappingURL=PanSession.es.js.map
