import { __read as T, __rest as b, __assign as h } from "../../../../../tslib/tslib.es6.es.js";
import { startAnimation as V } from "../../animation/utils/transitions.es.js";
import { setTarget as B } from "./setters.es.js";
import { resolveVariant as C } from "./variants.es.js";
import { isTransformProp as x } from "../html/utils/transform.es.js";
function q(r, n, t) {
  t === void 0 && (t = {}), r.notifyAnimationStart(n);
  var i;
  if (Array.isArray(n)) {
    var o = n.map(function(e) {
      return P(r, e, t);
    });
    i = Promise.all(o);
  } else if (typeof n == "string")
    i = P(r, n, t);
  else {
    var f = typeof n == "function" ? C(r, n, t.custom) : n;
    i = O(r, f, t);
  }
  return i.then(function() {
    return r.notifyAnimationComplete(n);
  });
}
function P(r, n, t) {
  var i;
  t === void 0 && (t = {});
  var o = C(r, n, t.custom), f = (o || {}).transition, e = f === void 0 ? r.getDefaultTransition() || {} : f;
  t.transitionOverride && (e = t.transitionOverride);
  var d = o ? function() {
    return O(r, o, t);
  } : function() {
    return Promise.resolve();
  }, u = !((i = r.variantChildren) === null || i === void 0) && i.size ? function(s) {
    s === void 0 && (s = 0);
    var c = e.delayChildren, A = c === void 0 ? 0 : c, m = e.staggerChildren, _ = e.staggerDirection;
    return z(r, n, A + s, m, _, t);
  } : function() {
    return Promise.resolve();
  }, a = e.when;
  if (a) {
    var v = T(a === "beforeChildren" ? [d, u] : [u, d], 2), g = v[0], y = v[1];
    return g().then(y);
  } else
    return Promise.all([d(), u(t.delay)]);
}
function O(r, n, t) {
  var i, o = t === void 0 ? {} : t, f = o.delay, e = f === void 0 ? 0 : f, d = o.transitionOverride, u = o.type, a = r.makeTargetAnimatable(n), v = a.transition, g = v === void 0 ? r.getDefaultTransition() : v, y = a.transitionEnd, s = b(a, ["transition", "transitionEnd"]);
  d && (g = d);
  var c = [], A = u && ((i = r.animationState) === null || i === void 0 ? void 0 : i.getState()[u]);
  for (var m in s) {
    var _ = r.getValue(m), l = s[m];
    if (!(!_ || l === void 0 || A && K(A, m))) {
      var p = h({ delay: e }, g);
      r.shouldReduceMotion && x(m) && (p = h(h({}, p), { type: !1, delay: 0 }));
      var S = V(m, _, l, p);
      c.push(S);
    }
  }
  return Promise.all(c).then(function() {
    y && B(r, y);
  });
}
function z(r, n, t, i, o, f) {
  t === void 0 && (t = 0), i === void 0 && (i = 0), o === void 0 && (o = 1);
  var e = [], d = (r.variantChildren.size - 1) * i, u = o === 1 ? function(a) {
    return a === void 0 && (a = 0), a * i;
  } : function(a) {
    return a === void 0 && (a = 0), d - a * i;
  };
  return Array.from(r.variantChildren).sort(D).forEach(function(a, v) {
    e.push(P(a, n, h(h({}, f), { delay: t + u(v) })).then(function() {
      return a.notifyAnimationComplete(n);
    }));
  }), Promise.all(e);
}
function D(r, n) {
  return r.sortNodePosition(n);
}
function K(r, n) {
  var t = r.protectedKeys, i = r.needsAnimating, o = t.hasOwnProperty(n) && i[n] !== !0;
  return i[n] = !1, o;
}
export {
  q as animateVisualElement,
  D as sortByTreeOrder
};
//# sourceMappingURL=animation.es.js.map
