import { animateVisualElement as N } from "../../animation/interfaces/visual-element.es.js";
import { calcChildStagger as Q } from "../../animation/utils/calc-child-stagger.es.js";
import { getVariantContext as U } from "./get-variant-context.es.js";
import { isAnimationControls as W } from "./is-animation-controls.es.js";
import { isKeyframesTarget as k } from "./is-keyframes-target.es.js";
import { isVariantLabel as X } from "./is-variant-label.es.js";
import { resolveVariant as K } from "./resolve-dynamic-variants.es.js";
import { shallowCompare as H } from "./shallow-compare.es.js";
import { variantPriorityOrder as L } from "./variant-props.es.js";
const Y = [...L].reverse(), Z = L.length;
function _(e) {
  return (u) => Promise.all(u.map(({ animation: d, options: m }) => N(e, d, m)));
}
function fe(e) {
  let u = _(e), d = B(), m = !0, V = !1;
  const z = (l) => (n, y) => {
    var a;
    const f = K(e, y, l === "exit" ? (a = e.presenceContext) == null ? void 0 : a.custom : void 0);
    if (f) {
      const { transition: c, transitionEnd: C, ...w } = f;
      n = { ...n, ...w, ...C };
    }
    return n;
  };
  function j(l) {
    u = l(e);
  }
  function b(l) {
    const { props: n } = e, y = U(e.parent) || {}, f = [], a = /* @__PURE__ */ new Set();
    let c = {}, C = 1 / 0;
    for (let p = 0; p < Z; p++) {
      const t = Y[p], i = d[t], o = n[t] !== void 0 ? n[t] : y[t], R = X(o), P = t === l ? i.isActive : null;
      P === !1 && (C = p);
      let S = o === y[t] && o !== n[t] && R;
      if (S && (m || V) && e.manuallyAnimateOnMount && (S = !1), i.protectedKeys = { ...c }, // If it isn't active and hasn't *just* been set as inactive
      !i.isActive && P === null || // If we didn't and don't have any defined prop for this animation type
      !o && !i.prevProp || // Or if the prop doesn't define an animation
      W(o) || typeof o == "boolean")
        continue;
      if (t === "exit" && i.isActive && P !== !0) {
        i.prevResolvedValues && (c = {
          ...c,
          ...i.prevResolvedValues
        });
        continue;
      }
      const I = $(i.prevProp, o);
      let T = I || // If we're making this variant active, we want to always make it active
      t === l && i.isActive && !S && R || // If we removed a higher-priority variant (i is in reverse order)
      p > C && R, x = !1;
      const O = Array.isArray(o) ? o : [o];
      let v = O.reduce(z(t), {});
      P === !1 && (v = {});
      const { prevResolvedValues: D = {} } = i, G = {
        ...D,
        ...v
      }, F = (r) => {
        T = !0, a.has(r) && (x = !0, a.delete(r)), i.needsAnimating[r] = !0;
        const s = e.getValue(r);
        s && (s.liveStyle = !1);
      };
      for (const r in G) {
        const s = v[r], A = D[r];
        if (c.hasOwnProperty(r))
          continue;
        let g = !1;
        k(s) && k(A) ? g = !H(s, A) : g = s !== A, g ? s != null ? F(r) : a.add(r) : s !== void 0 && a.has(r) ? F(r) : i.protectedKeys[r] = !0;
      }
      i.prevProp = o, i.prevResolvedValues = v, i.isActive && (c = { ...c, ...v }), (m || V) && e.blockInitialAnimation && (T = !1);
      const M = S && I;
      T && (!M || x) && f.push(...O.map((r) => {
        const s = { type: t };
        if (typeof r == "string" && (m || V) && !M && e.manuallyAnimateOnMount && e.parent) {
          const { parent: A } = e, g = K(A, r);
          if (A.enteringChildren && g) {
            const { delayChildren: J } = g.transition || {};
            s.delay = Q(A.enteringChildren, e, J);
          }
        }
        return {
          animation: r,
          options: s
        };
      }));
    }
    if (a.size) {
      const p = {};
      if (typeof n.initial != "boolean") {
        const t = K(e, Array.isArray(n.initial) ? n.initial[0] : n.initial);
        t && t.transition && (p.transition = t.transition);
      }
      a.forEach((t) => {
        const i = e.getBaseTarget(t), o = e.getValue(t);
        o && (o.liveStyle = !0), p[t] = i ?? null;
      }), f.push({ animation: p });
    }
    let w = !!f.length;
    return m && (n.initial === !1 || n.initial === n.animate) && !e.manuallyAnimateOnMount && (w = !1), m = !1, V = !1, w ? u(f) : Promise.resolve();
  }
  function q(l, n) {
    var f;
    if (d[l].isActive === n)
      return Promise.resolve();
    (f = e.variantChildren) == null || f.forEach((a) => {
      var c;
      return (c = a.animationState) == null ? void 0 : c.setActive(l, n);
    }), d[l].isActive = n;
    const y = b(l);
    for (const a in d)
      d[a].protectedKeys = {};
    return y;
  }
  return {
    animateChanges: b,
    setActive: q,
    setAnimateFunction: j,
    getState: () => d,
    reset: () => {
      d = B(), V = !0;
    }
  };
}
function $(e, u) {
  return typeof u == "string" ? u !== e : Array.isArray(u) ? !H(u, e) : !1;
}
function h(e = !1) {
  return {
    isActive: e,
    protectedKeys: {},
    needsAnimating: {},
    prevResolvedValues: {}
  };
}
function B() {
  return {
    animate: h(!0),
    whileInView: h(),
    whileHover: h(),
    whileTap: h(),
    whileDrag: h(),
    whileFocus: h(),
    exit: h()
  };
}
export {
  $ as checkVariantsDidChange,
  fe as createAnimationState
};
//# sourceMappingURL=animation-state.es.js.map
