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