import { __assign as y } from "../../../../../tslib/tslib.es6.es.js";
import { invariant as k } from "../../../../../hey-listen/dist/hey-listen.es.es.js";
import { PanSession as j } from "../PanSession.es.js";
import { getGlobalLock as x } from "./utils/lock.es.js";
import { isRefObject as C } from "../../utils/is-ref-object.es.js";
import { addPointerEvent as T } from "../../events/use-pointer-event.es.js";
import { applyConstraints as B, calcRelativeConstraints as G, resolveDragElastic as R, calcViewportConstraints as w, rebaseAxisConstraints as I, calcOrigin as O, defaultElastic as W } from "./utils/constraints.es.js";
import { AnimationType as M } from "../../render/utils/types.es.js";
import { createBox as z } from "../../projection/geometry/models.es.js";
import { eachAxis as m } from "../../projection/utils/each-axis.es.js";
import { measurePageBox as U } from "../../projection/utils/measure.es.js";
import { extractEventInfo as F } from "../../events/event-info.es.js";
import { startAnimation as K } from "../../animation/utils/transitions.es.js";
import { convertBoxToBoundingBox as q, convertBoundingBoxToBox as H } from "../../projection/geometry/conversion.es.js";
import { addDomEvent as J } from "../../events/use-dom-event.es.js";
import { calcLength as N } from "../../projection/geometry/delta-calc.es.js";
import { percent as Q } from "../../../../../style-value-types/dist/es/numbers/units.es.js";
import { mix as S } from "../../../../../popmotion/dist/es/utils/mix.es.js";
var X = /* @__PURE__ */ new WeakMap(), mt = function() {
  function c(t) {
    this.openGlobalLock = null, this.isDragging = !1, this.currentDirection = null, this.originPoint = { x: 0, y: 0 }, this.constraints = !1, this.hasMutatedConstraints = !1, this.elastic = z(), this.visualElement = t;
  }
  return c.prototype.start = function(t, i) {
    var n = this, e = i === void 0 ? {} : i, r = e.snapToCursor, o = r === void 0 ? !1 : r;
    if (this.visualElement.isPresent !== !1) {
      var l = function(v) {
        n.stopAnimation(), o && n.snapToCursor(F(v, "page").point);
      }, p = function(v, a) {
        var u, f = n.getProps(), P = f.drag, h = f.dragPropagation, d = f.onDragStart;
        P && !h && (n.openGlobalLock && n.openGlobalLock(), n.openGlobalLock = x(P), !n.openGlobalLock) || (n.isDragging = !0, n.currentDirection = null, n.resolveConstraints(), n.visualElement.projection && (n.visualElement.projection.isAnimationBlocked = !0, n.visualElement.projection.target = void 0), m(function(E) {
          var L, A, D = n.getAxisMotionValue(E).get() || 0;
          if (Q.test(D)) {
            var b = (A = (L = n.visualElement.projection) === null || L === void 0 ? void 0 : L.layout) === null || A === void 0 ? void 0 : A.actual[E];
            if (b) {
              var V = N(b);
              D = V * (parseFloat(D) / 100);
            }
          }
          n.originPoint[E] = D;
        }), d == null || d(v, a), (u = n.visualElement.animationState) === null || u === void 0 || u.setActive(M.Drag, !0));
      }, s = function(v, a) {
        var u = n.getProps(), f = u.dragPropagation, P = u.dragDirectionLock, h = u.onDirectionLock, d = u.onDrag;
        if (!(!f && !n.openGlobalLock)) {
          var E = a.offset;
          if (P && n.currentDirection === null) {
            n.currentDirection = Y(E), n.currentDirection !== null && (h == null || h(n.currentDirection));
            return;
          }
          n.updateAxis("x", a.point, E), n.updateAxis("y", a.point, E), n.visualElement.syncRender(), d == null || d(v, a);
        }
      }, g = function(v, a) {
        return n.stop(v, a);
      };
      this.panSession = new j(t, {
        onSessionStart: l,
        onStart: p,
        onMove: s,
        onSessionEnd: g
      }, { transformPagePoint: this.visualElement.getTransformPagePoint() });
    }
  }, c.prototype.stop = function(t, i) {
    var n = this.isDragging;
    if (this.cancel(), !!n) {
      var e = i.velocity;
      this.startAnimation(e);
      var r = this.getProps().onDragEnd;
      r == null || r(t, i);
    }
  }, c.prototype.cancel = function() {
    var t, i;
    this.isDragging = !1, this.visualElement.projection && (this.visualElement.projection.isAnimationBlocked = !1), (t = this.panSession) === null || t === void 0 || t.end(), this.panSession = void 0;
    var n = this.getProps().dragPropagation;
    !n && this.openGlobalLock && (this.openGlobalLock(), this.openGlobalLock = null), (i = this.visualElement.animationState) === null || i === void 0 || i.setActive(M.Drag, !1);
  }, c.prototype.updateAxis = function(t, i, n) {
    var e = this.getProps().drag;
    if (!(!n || !_(t, e, this.currentDirection))) {
      var r = this.getAxisMotionValue(t), o = this.originPoint[t] + n[t];
      this.constraints && this.constraints[t] && (o = B(o, this.constraints[t], this.elastic[t])), r.set(o);
    }
  }, c.prototype.resolveConstraints = function() {
    var t = this, i = this.getProps(), n = i.dragConstraints, e = i.dragElastic, r = (this.visualElement.projection || {}).layout, o = this.constraints;
    n && C(n) ? this.constraints || (this.constraints = this.resolveRefConstraints()) : n && r ? this.constraints = G(r.actual, n) : this.constraints = !1, this.elastic = R(e), o !== this.constraints && r && this.constraints && !this.hasMutatedConstraints && m(function(l) {
      t.getAxisMotionValue(l) && (t.constraints[l] = I(r.actual[l], t.constraints[l]));
    });
  }, c.prototype.resolveRefConstraints = function() {
    var t = this.getProps(), i = t.dragConstraints, n = t.onMeasureDragConstraints;
    if (!i || !C(i))
      return !1;
    var e = i.current;
    k(e !== null, "If `dragConstraints` is set as a React ref, that ref must be passed to another component's `ref` prop.");
    var r = this.visualElement.projection;
    if (!r || !r.layout)
      return !1;
    var o = U(e, r.root, this.visualElement.getTransformPagePoint()), l = w(r.layout.actual, o);
    if (n) {
      var p = n(q(l));
      this.hasMutatedConstraints = !!p, p && (l = H(p));
    }
    return l;
  }, c.prototype.startAnimation = function(t) {
    var i = this, n = this.getProps(), e = n.drag, r = n.dragMomentum, o = n.dragElastic, l = n.dragTransition, p = n.dragSnapToOrigin, s = n.onDragTransitionEnd, g = this.constraints || {}, v = m(function(a) {
      var u;
      if (!!_(a, e, i.currentDirection)) {
        var f = (u = g == null ? void 0 : g[a]) !== null && u !== void 0 ? u : {};
        p && (f = { min: 0, max: 0 });
        var P = o ? 200 : 1e6, h = o ? 40 : 1e7, d = y(y({ type: "inertia", velocity: r ? t[a] : 0, bounceStiffness: P, bounceDamping: h, timeConstant: 750, restDelta: 1, restSpeed: 10 }, l), f);
        return i.startAxisValueAnimation(a, d);
      }
    });
    return Promise.all(v).then(s);
  }, c.prototype.startAxisValueAnimation = function(t, i) {
    var n = this.getAxisMotionValue(t);
    return K(t, n, 0, i);
  }, c.prototype.stopAnimation = function() {
    var t = this;
    m(function(i) {
      return t.getAxisMotionValue(i).stop();
    });
  }, c.prototype.getAxisMotionValue = function(t) {
    var i, n, e = "_drag" + t.toUpperCase(), r = this.visualElement.getProps()[e];
    return r || this.visualElement.getValue(t, (n = (i = this.visualElement.getProps().initial) === null || i === void 0 ? void 0 : i[t]) !== null && n !== void 0 ? n : 0);
  }, c.prototype.snapToCursor = function(t) {
    var i = this;
    m(function(n) {
      var e = i.getProps().drag;
      if (!!_(n, e, i.currentDirection)) {
        var r = i.visualElement.projection, o = i.getAxisMotionValue(n);
        if (r && r.layout) {
          var l = r.layout.actual[n], p = l.min, s = l.max;
          o.set(t[n] - S(p, s, 0.5));
        }
      }
    });
  }, c.prototype.scalePositionWithinConstraints = function() {
    var t = this, i, n = this.getProps(), e = n.drag, r = n.dragConstraints, o = this.visualElement.projection;
    if (!(!C(r) || !o || !this.constraints)) {
      this.stopAnimation();
      var l = { x: 0, y: 0 };
      m(function(s) {
        var g = t.getAxisMotionValue(s);
        if (g) {
          var v = g.get();
          l[s] = O({ min: v, max: v }, t.constraints[s]);
        }
      });
      var p = this.visualElement.getProps().transformTemplate;
      this.visualElement.getInstance().style.transform = p ? p({}, "") : "none", (i = o.root) === null || i === void 0 || i.updateScroll(), o.updateLayout(), this.resolveConstraints(), m(function(s) {
        if (!!_(s, e, null)) {
          var g = t.getAxisMotionValue(s), v = t.constraints[s], a = v.min, u = v.max;
          g.set(S(a, u, l[s]));
        }
      });
    }
  }, c.prototype.addListeners = function() {
    var t = this, i;
    X.set(this.visualElement, this);
    var n = this.visualElement.getInstance(), e = T(n, "pointerdown", function(s) {
      var g = t.getProps(), v = g.drag, a = g.dragListener, u = a === void 0 ? !0 : a;
      v && u && t.start(s);
    }), r = function() {
      var s = t.getProps().dragConstraints;
      C(s) && (t.constraints = t.resolveRefConstraints());
    }, o = this.visualElement.projection, l = o.addEventListener("measure", r);
    o && !o.layout && ((i = o.root) === null || i === void 0 || i.updateScroll(), o.updateLayout()), r();
    var p = J(window, "resize", function() {
      return t.scalePositionWithinConstraints();
    });
    return o.addEventListener("didUpdate", function(s) {
      var g = s.delta, v = s.hasLayoutChanged;
      t.isDragging && v && (m(function(a) {
        var u = t.getAxisMotionValue(a);
        !u || (t.originPoint[a] += g[a].translate, u.set(u.get() + g[a].translate));
      }), t.visualElement.syncRender());
    }), function() {
      p(), e(), l();
    };
  }, c.prototype.getProps = function() {
    var t = this.visualElement.getProps(), i = t.drag, n = i === void 0 ? !1 : i, e = t.dragDirectionLock, r = e === void 0 ? !1 : e, o = t.dragPropagation, l = o === void 0 ? !1 : o, p = t.dragConstraints, s = p === void 0 ? !1 : p, g = t.dragElastic, v = g === void 0 ? W : g, a = t.dragMomentum, u = a === void 0 ? !0 : a;
    return y(y({}, t), { drag: n, dragDirectionLock: r, dragPropagation: l, dragConstraints: s, dragElastic: v, dragMomentum: u });
  }, c;
}();
function _(c, t, i) {
  return (t === !0 || t === c) && (i === null || i === c);
}
function Y(c, t) {
  t === void 0 && (t = 10);
  var i = null;
  return Math.abs(c.y) > t ? i = "y" : Math.abs(c.x) > t && (i = "x"), i;
}
export {
  mt as VisualElementDragControls,
  X as elementDragControls
};
//# sourceMappingURL=VisualElementDragControls.es.js.map
