import { __spreadArray as q, __read as G, __assign as f, __rest as $ } from "../../../../../tslib/tslib.es6.es.js";
import { isAnimationControls as x } from "../../animation/utils/is-animation-controls.es.js";
import { isKeyframesTarget as j } from "../../animation/utils/is-keyframes-target.es.js";
import { shallowCompare as J } from "../../utils/shallow-compare.es.js";
import { animateVisualElement as E } from "./animation.es.js";
import { AnimationType as a } from "./types.es.js";
import { isVariantLabels as k, resolveVariant as ee, isVariantLabel as re } from "./variants.es.js";
var N = [
  a.Animate,
  a.InView,
  a.Focus,
  a.Hover,
  a.Tap,
  a.Drag,
  a.Exit
], ae = q([], G(N), !1).reverse(), ie = N.length;
function ne(e) {
  return function(v) {
    return Promise.all(v.map(function(u) {
      var K = u.animation, g = u.options;
      return E(e, K, g);
    }));
  };
}
function pe(e) {
  var v = ne(e), u = oe(), K = {}, g = !0, Q = function(r, m) {
    var o = ee(e, m);
    if (o) {
      o.transition;
      var l = o.transitionEnd, h = $(o, ["transition", "transitionEnd"]);
      r = f(f(f({}, r), h), l);
    }
    return r;
  };
  function U(r) {
    return K[r] !== void 0;
  }
  function W(r) {
    v = r(e);
  }
  function D(r, m) {
    for (var o, l = e.getProps(), h = e.getVariantContext(!0) || {}, p = [], c = /* @__PURE__ */ new Set(), d = {}, O = 1 / 0, Y = function(V) {
      var n = ae[V], i = u[n], t = (o = l[n]) !== null && o !== void 0 ? o : h[n], I = re(t), S = n === m ? i.isActive : null;
      S === !1 && (O = V);
      var P = t === h[n] && t !== l[n] && I;
      if (P && g && e.manuallyAnimateOnMount && (P = !1), i.protectedKeys = f({}, d), !i.isActive && S === null || !t && !i.prevProp || x(t) || typeof t == "boolean")
        return "continue";
      var B = te(i.prevProp, t), b = B || n === m && i.isActive && !P && I || V > O && I, H = Array.isArray(t) ? t : [t], y = H.reduce(Q, {});
      S === !1 && (y = {});
      var M = i.prevResolvedValues, z = M === void 0 ? {} : M, Z = f(f({}, z), y), R = function(T) {
        b = !0, c.delete(T), i.needsAnimating[T] = !0;
      };
      for (var s in Z) {
        var _ = y[s], w = z[s];
        d.hasOwnProperty(s) || (_ !== w ? j(_) && j(w) ? !J(_, w) || B ? R(s) : i.protectedKeys[s] = !0 : _ !== void 0 ? R(s) : c.add(s) : _ !== void 0 && c.has(s) ? R(s) : i.protectedKeys[s] = !0);
      }
      i.prevProp = t, i.prevResolvedValues = y, i.isActive && (d = f(f({}, d), y)), g && e.blockInitialAnimation && (b = !1), b && !P && p.push.apply(p, q([], G(H.map(function(T) {
        return {
          animation: T,
          options: f({ type: n }, r)
        };
      })), !1));
    }, C = 0; C < ie; C++)
      Y(C);
    if (K = f({}, d), c.size) {
      var F = {};
      c.forEach(function(V) {
        var n = e.getBaseTarget(V);
        n !== void 0 && (F[V] = n);
      }), p.push({ animation: F });
    }
    var L = Boolean(p.length);
    return g && l.initial === !1 && !e.manuallyAnimateOnMount && (L = !1), g = !1, L ? v(p) : Promise.resolve();
  }
  function X(r, m, o) {
    var l;
    if (u[r].isActive === m)
      return Promise.resolve();
    (l = e.variantChildren) === null || l === void 0 || l.forEach(function(c) {
      var d;
      return (d = c.animationState) === null || d === void 0 ? void 0 : d.setActive(r, m);
    }), u[r].isActive = m;
    var h = D(o, r);
    for (var p in u)
      u[p].protectedKeys = {};
    return h;
  }
  return {
    isAnimated: U,
    animateChanges: D,
    setActive: X,
    setAnimateFunction: W,
    getState: function() {
      return u;
    }
  };
}
function te(e, v) {
  return typeof v == "string" ? v !== e : k(v) ? !J(v, e) : !1;
}
function A(e) {
  return e === void 0 && (e = !1), {
    isActive: e,
    protectedKeys: {},
    needsAnimating: {},
    prevResolvedValues: {}
  };
}
function oe() {
  var e;
  return e = {}, e[a.Animate] = A(!0), e[a.InView] = A(), e[a.Hover] = A(), e[a.Tap] = A(), e[a.Drag] = A(), e[a.Focus] = A(), e[a.Exit] = A(), e;
}
export {
  te as checkVariantsDidChange,
  pe as createAnimationState,
  N as variantPriorityOrder
};
//# sourceMappingURL=animation-state.es.js.map
