import { animateSingleValue as at } from "../../animation/animate/single-value.es.js";
import { getOptimisedAppearId as rt } from "../../animation/optimized-appear/get-appear-id.es.js";
import { getValueTransition as nt } from "../../animation/utils/get-value-transition.es.js";
import { microtask as ht } from "../../frameloop/microtask.es.js";
import { time as lt } from "../../frameloop/sync-time.es.js";
import { scaleCorrectors as b } from "../styles/scale-correction.es.js";
import { delay as ut } from "../../utils/delay.es.js";
import { isSVGElement as ct } from "../../utils/is-svg-element.es.js";
import { isSVGSVGElement as dt } from "../../utils/is-svg-svg-element.es.js";
import { mixNumber as E } from "../../utils/mix/number.es.js";
import { motionValue as ft } from "../../value/index.es.js";
import { resolveMotionValue as U } from "../../value/utils/resolve-motion-value.es.js";
import { mixValues as mt } from "../animation/mix-values.es.js";
import { copyBoxInto as y, copyAxisDeltaInto as z, copyAxisInto as pt } from "../geometry/copy.es.js";
import { translateAxis as S, transformBox as C, applyBoxDelta as yt, applyTreeDeltas as gt } from "../geometry/delta-apply.es.js";
import { calcLength as A, calcRelativeBox as vt, calcRelativePosition as w, calcBoxDelta as L, isNear as Tt } from "../geometry/delta-calc.es.js";
import { removeBoxTransforms as W } from "../geometry/delta-remove.es.js";
import { createBox as f, createDelta as R } from "../geometry/models.es.js";
import { boxEqualsRounded as et, isDeltaZero as it, axisDeltaEquals as N, boxEquals as xt, aspectRatio as H } from "../geometry/utils.es.js";
import { NodeStack as Dt } from "../shared/stack.es.js";
import { buildProjectionTransform as Bt } from "../styles/transform.es.js";
import { eachAxis as G } from "../utils/each-axis.es.js";
import { FlatTree as St } from "../utils/flat-tree.es.js";
import { hasTransform as P, hasScale as q, has2DTranslate as Pt } from "../utils/has-transform.es.js";
import { globalProjectionState as F } from "./state.es.js";
import { frame as k, cancelFrame as X, frameSteps as O, frameData as g } from "../../frameloop/frame.es.js";
import { SubscriptionManager as At } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/subscription-manager.es.js";
import { clamp as Rt } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/clamp.es.js";
import { noop as jt } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/noop.es.js";
const I = ["", "X", "Y", "Z"], Vt = 1e3;
let kt = 0;
function M(e, n, c, u) {
  const { latestValues: T } = n;
  T[e] && (c[e] = T[e], n.setStaticValue(e, 0), u && (u[e] = 0));
}
function st(e) {
  if (e.hasCheckedOptimisedAppear = !0, e.root === e)
    return;
  const { visualElement: n } = e.options;
  if (!n)
    return;
  const c = rt(n);
  if (window.MotionHasOptimisedAnimation(c, "transform")) {
    const { layout: T, layoutId: x } = e.options;
    window.MotionCancelOptimisedAnimation(c, "transform", k, !(T || x));
  }
  const { parent: u } = e;
  u && !u.hasCheckedOptimisedAppear && st(u);
}
function Se({ attachResizeListener: e, defaultParent: n, measureScroll: c, checkIsScrollRoot: u, resetTransform: T }) {
  return class {
    constructor(t = {}, i = n == null ? void 0 : n()) {
      this.id = kt++, this.animationId = 0, this.animationCommitId = 0, this.children = /* @__PURE__ */ new Set(), this.options = {}, this.isTreeAnimating = !1, this.isAnimationBlocked = !1, this.isLayoutDirty = !1, this.isProjectionDirty = !1, this.isSharedProjectionDirty = !1, this.isTransformDirty = !1, this.updateManuallyBlocked = !1, this.updateBlockedByResize = !1, this.isUpdating = !1, this.isSVG = !1, this.needsReset = !1, this.shouldResetTransform = !1, this.hasCheckedOptimisedAppear = !1, this.treeScale = { x: 1, y: 1 }, this.eventHandlers = /* @__PURE__ */ new Map(), this.hasTreeAnimated = !1, this.layoutVersion = 0, this.updateScheduled = !1, this.scheduleUpdate = () => this.update(), this.projectionUpdateScheduled = !1, this.checkUpdateFailed = () => {
        this.isUpdating && (this.isUpdating = !1, this.clearAllSnapshots());
      }, this.updateProjection = () => {
        this.projectionUpdateScheduled = !1, this.nodes.forEach(wt), this.nodes.forEach(Mt), this.nodes.forEach(bt), this.nodes.forEach(Ut);
      }, this.resolvedRelativeTargetAt = 0, this.linkedParentVersion = 0, this.hasProjected = !1, this.isVisible = !0, this.animationProgress = 0, this.sharedNodes = /* @__PURE__ */ new Map(), this.latestValues = t, this.root = i ? i.root || i : this, this.path = i ? [...i.path, i] : [], this.parent = i, this.depth = i ? i.depth + 1 : 0;
      for (let s = 0; s < this.path.length; s++)
        this.path[s].shouldResetTransform = !0;
      this.root === this && (this.nodes = new St());
    }
    addEventListener(t, i) {
      return this.eventHandlers.has(t) || this.eventHandlers.set(t, new At()), this.eventHandlers.get(t).add(i);
    }
    notifyListeners(t, ...i) {
      const s = this.eventHandlers.get(t);
      s && s.notify(...i);
    }
    hasListeners(t) {
      return this.eventHandlers.has(t);
    }
    /**
     * Lifecycles
     */
    mount(t) {
      if (this.instance)
        return;
      this.isSVG = ct(t) && !dt(t), this.instance = t;
      const { layoutId: i, layout: s, visualElement: o } = this.options;
      if (o && !o.current && o.mount(t), this.root.nodes.add(this), this.parent && this.parent.children.add(this), this.root.hasTreeAnimated && (s || i) && (this.isLayoutDirty = !0), e) {
        let a, r = 0;
        const h = () => this.root.updateBlockedByResize = !1;
        k.read(() => {
          r = window.innerWidth;
        }), e(t, () => {
          const l = window.innerWidth;
          l !== r && (r = l, this.root.updateBlockedByResize = !0, a && a(), a = ut(h, 250), F.hasAnimatedSinceResize && (F.hasAnimatedSinceResize = !1, this.nodes.forEach(Z)));
        });
      }
      i && this.root.registerSharedNode(i, this), this.options.animate !== !1 && o && (i || s) && this.addEventListener("didUpdate", ({ delta: a, hasLayoutChanged: r, hasRelativeLayoutChanged: h, layout: l }) => {
        if (this.isTreeAnimationBlocked()) {
          this.target = void 0, this.relativeTarget = void 0;
          return;
        }
        const d = this.options.transition || o.getDefaultTransition() || Gt, { onLayoutAnimationStart: v, onLayoutAnimationComplete: m } = o.getProps(), B = !this.targetLayout || !et(this.targetLayout, l), D = !r && h;
        if (this.options.layoutRoot || this.resumeFrom || D || r && (B || !this.currentAnimation)) {
          this.resumeFrom && (this.resumingFrom = this.resumeFrom, this.resumingFrom.resumingFrom = void 0);
          const p = {
            ...nt(d, "layout"),
            onPlay: v,
            onComplete: m
          };
          (o.shouldReduceMotion || this.options.layoutRoot) && (p.delay = 0, p.type = !1), this.startAnimation(p), this.setAnimationOrigin(a, D);
        } else
          r || Z(this), this.isLead() && this.options.onExitComplete && this.options.onExitComplete();
        this.targetLayout = l;
      });
    }
    unmount() {
      this.options.layoutId && this.willUpdate(), this.root.nodes.remove(this);
      const t = this.getStack();
      t && t.remove(this), this.parent && this.parent.children.delete(this), this.instance = void 0, this.eventHandlers.clear(), X(this.updateProjection);
    }
    // only on the root
    blockUpdate() {
      this.updateManuallyBlocked = !0;
    }
    unblockUpdate() {
      this.updateManuallyBlocked = !1;
    }
    isUpdateBlocked() {
      return this.updateManuallyBlocked || this.updateBlockedByResize;
    }
    isTreeAnimationBlocked() {
      return this.isAnimationBlocked || this.parent && this.parent.isTreeAnimationBlocked() || !1;
    }
    // Note: currently only running on root node
    startUpdate() {
      this.isUpdateBlocked() || (this.isUpdating = !0, this.nodes && this.nodes.forEach(zt), this.animationId++);
    }
    getTransformTemplate() {
      const { visualElement: t } = this.options;
      return t && t.getProps().transformTemplate;
    }
    willUpdate(t = !0) {
      if (this.root.hasTreeAnimated = !0, this.root.isUpdateBlocked()) {
        this.options.onExitComplete && this.options.onExitComplete();
        return;
      }
      if (window.MotionCancelOptimisedAnimation && !this.hasCheckedOptimisedAppear && st(this), !this.root.isUpdating && this.root.startUpdate(), this.isLayoutDirty)
        return;
      this.isLayoutDirty = !0;
      for (let a = 0; a < this.path.length; a++) {
        const r = this.path[a];
        r.shouldResetTransform = !0, (typeof r.latestValues.x == "string" || typeof r.latestValues.y == "string") && (r.isLayoutDirty = !0), r.updateScroll("snapshot"), r.options.layoutRoot && r.willUpdate(!1);
      }
      const { layoutId: i, layout: s } = this.options;
      if (i === void 0 && !s)
        return;
      const o = this.getTransformTemplate();
      this.prevTransformTemplateValue = o ? o(this.latestValues, "") : void 0, this.updateSnapshot(), t && this.notifyListeners("willUpdate");
    }
    update() {
      if (this.updateScheduled = !1, this.isUpdateBlocked()) {
        const s = this.updateBlockedByResize;
        this.unblockUpdate(), this.updateBlockedByResize = !1, this.clearAllSnapshots(), s && this.nodes.forEach(Ft), this.nodes.forEach(Y);
        return;
      }
      if (this.animationId <= this.animationCommitId) {
        this.nodes.forEach($);
        return;
      }
      this.animationCommitId = this.animationId, this.isUpdating ? (this.isUpdating = !1, this.nodes.forEach(Ot), this.nodes.forEach(It), this.nodes.forEach(Lt), this.nodes.forEach(Et)) : this.nodes.forEach($), this.clearAllSnapshots();
      const i = lt.now();
      g.delta = Rt(0, 1e3 / 60, i - g.timestamp), g.timestamp = i, g.isProcessing = !0, O.update.process(g), O.preRender.process(g), O.render.process(g), g.isProcessing = !1;
    }
    didUpdate() {
      this.updateScheduled || (this.updateScheduled = !0, ht.read(this.scheduleUpdate));
    }
    clearAllSnapshots() {
      this.nodes.forEach(Ct), this.sharedNodes.forEach(Wt);
    }
    scheduleUpdateProjection() {
      this.projectionUpdateScheduled || (this.projectionUpdateScheduled = !0, k.preRender(this.updateProjection, !1, !0));
    }
    scheduleCheckAfterUnmount() {
      k.postRender(() => {
        this.isLayoutDirty ? this.root.didUpdate() : this.root.checkUpdateFailed();
      });
    }
    /**
     * Update measurements
     */
    updateSnapshot() {
      this.snapshot || !this.instance || (this.snapshot = this.measure(), this.snapshot && !A(this.snapshot.measuredBox.x) && !A(this.snapshot.measuredBox.y) && (this.snapshot = void 0));
    }
    updateLayout() {
      if (!this.instance || (this.updateScroll(), !(this.options.alwaysMeasureLayout && this.isLead()) && !this.isLayoutDirty))
        return;
      if (this.resumeFrom && !this.resumeFrom.instance)
        for (let s = 0; s < this.path.length; s++)
          this.path[s].updateScroll();
      const t = this.layout;
      this.layout = this.measure(!1), this.layoutVersion++, this.layoutCorrected || (this.layoutCorrected = f()), this.isLayoutDirty = !1, this.projectionDelta = void 0, this.notifyListeners("measure", this.layout.layoutBox);
      const { visualElement: i } = this.options;
      i && i.notify("LayoutMeasure", this.layout.layoutBox, t ? t.layoutBox : void 0);
    }
    updateScroll(t = "measure") {
      let i = !!(this.options.layoutScroll && this.instance);
      if (this.scroll && this.scroll.animationId === this.root.animationId && this.scroll.phase === t && (i = !1), i && this.instance) {
        const s = u(this.instance);
        this.scroll = {
          animationId: this.root.animationId,
          phase: t,
          isRoot: s,
          offset: c(this.instance),
          wasRoot: this.scroll ? this.scroll.isRoot : s
        };
      }
    }
    resetTransform() {
      if (!T)
        return;
      const t = this.isLayoutDirty || this.shouldResetTransform || this.options.alwaysMeasureLayout, i = this.projectionDelta && !it(this.projectionDelta), s = this.getTransformTemplate(), o = s ? s(this.latestValues, "") : void 0, a = o !== this.prevTransformTemplateValue;
      t && this.instance && (i || P(this.latestValues) || a) && (T(this.instance, o), this.shouldResetTransform = !1, this.scheduleRender());
    }
    measure(t = !0) {
      const i = this.measurePageBox();
      let s = this.removeElementScroll(i);
      return t && (s = this.removeTransform(s)), qt(s), {
        animationId: this.root.animationId,
        measuredBox: i,
        layoutBox: s,
        latestValues: {},
        source: this.id
      };
    }
    measurePageBox() {
      var o;
      const { visualElement: t } = this.options;
      if (!t)
        return f();
      const i = t.measureViewportBox();
      if (!(((o = this.scroll) == null ? void 0 : o.wasRoot) || this.path.some(Xt))) {
        const { scroll: a } = this.root;
        a && (S(i.x, a.offset.x), S(i.y, a.offset.y));
      }
      return i;
    }
    removeElementScroll(t) {
      var s;
      const i = f();
      if (y(i, t), (s = this.scroll) != null && s.wasRoot)
        return i;
      for (let o = 0; o < this.path.length; o++) {
        const a = this.path[o], { scroll: r, options: h } = a;
        a !== this.root && r && h.layoutScroll && (r.wasRoot && y(i, t), S(i.x, r.offset.x), S(i.y, r.offset.y));
      }
      return i;
    }
    applyTransform(t, i = !1, s) {
      var a, r;
      const o = s || f();
      y(o, t);
      for (let h = 0; h < this.path.length; h++) {
        const l = this.path[h];
        !i && l.options.layoutScroll && l.scroll && l !== l.root && (S(o.x, -l.scroll.offset.x), S(o.y, -l.scroll.offset.y)), P(l.latestValues) && C(o, l.latestValues, (a = l.layout) == null ? void 0 : a.layoutBox);
      }
      return P(this.latestValues) && C(o, this.latestValues, (r = this.layout) == null ? void 0 : r.layoutBox), o;
    }
    removeTransform(t) {
      var s;
      const i = f();
      y(i, t);
      for (let o = 0; o < this.path.length; o++) {
        const a = this.path[o];
        if (!P(a.latestValues))
          continue;
        let r;
        a.instance && (q(a.latestValues) && a.updateSnapshot(), r = f(), y(r, a.measurePageBox())), W(i, a.latestValues, (s = a.snapshot) == null ? void 0 : s.layoutBox, r);
      }
      return P(this.latestValues) && W(i, this.latestValues), i;
    }
    setTargetDelta(t) {
      this.targetDelta = t, this.root.scheduleUpdateProjection(), this.isProjectionDirty = !0;
    }
    setOptions(t) {
      this.options = {
        ...this.options,
        ...t,
        crossfade: t.crossfade !== void 0 ? t.crossfade : !0
      };
    }
    clearMeasurements() {
      this.scroll = void 0, this.layout = void 0, this.snapshot = void 0, this.prevTransformTemplateValue = void 0, this.targetDelta = void 0, this.target = void 0, this.isLayoutDirty = !1;
    }
    forceRelativeParentToResolveTarget() {
      this.relativeParent && this.relativeParent.resolvedRelativeTargetAt !== g.timestamp && this.relativeParent.resolveTargetDelta(!0);
    }
    resolveTargetDelta(t = !1) {
      var l;
      const i = this.getLead();
      this.isProjectionDirty || (this.isProjectionDirty = i.isProjectionDirty), this.isTransformDirty || (this.isTransformDirty = i.isTransformDirty), this.isSharedProjectionDirty || (this.isSharedProjectionDirty = i.isSharedProjectionDirty);
      const s = !!this.resumingFrom || this !== i;
      if (!(t || s && this.isSharedProjectionDirty || this.isProjectionDirty || (l = this.parent) != null && l.isProjectionDirty || this.attemptToResolveRelativeTarget || this.root.updateBlockedByResize))
        return;
      const { layout: a, layoutId: r } = this.options;
      if (!this.layout || !(a || r))
        return;
      this.resolvedRelativeTargetAt = g.timestamp;
      const h = this.getClosestProjectingParent();
      h && this.linkedParentVersion !== h.layoutVersion && !h.options.layoutRoot && this.removeRelativeTarget(), !this.targetDelta && !this.relativeTarget && (this.options.layoutAnchor !== !1 && h && h.layout ? this.createRelativeTarget(h, this.layout.layoutBox, h.layout.layoutBox) : this.removeRelativeTarget()), !(!this.relativeTarget && !this.targetDelta) && (this.target || (this.target = f(), this.targetWithTransforms = f()), this.relativeTarget && this.relativeTargetOrigin && this.relativeParent && this.relativeParent.target ? (this.forceRelativeParentToResolveTarget(), vt(this.target, this.relativeTarget, this.relativeParent.target, this.options.layoutAnchor || void 0)) : this.targetDelta ? (this.resumingFrom ? this.applyTransform(this.layout.layoutBox, !1, this.target) : y(this.target, this.layout.layoutBox), yt(this.target, this.targetDelta)) : y(this.target, this.layout.layoutBox), this.attemptToResolveRelativeTarget && (this.attemptToResolveRelativeTarget = !1, this.options.layoutAnchor !== !1 && h && !!h.resumingFrom == !!this.resumingFrom && !h.options.layoutScroll && h.target && this.animationProgress !== 1 ? this.createRelativeTarget(h, this.target, h.target) : this.relativeParent = this.relativeTarget = void 0));
    }
    getClosestProjectingParent() {
      if (!(!this.parent || q(this.parent.latestValues) || Pt(this.parent.latestValues)))
        return this.parent.isProjecting() ? this.parent : this.parent.getClosestProjectingParent();
    }
    isProjecting() {
      return !!((this.relativeTarget || this.targetDelta || this.options.layoutRoot) && this.layout);
    }
    createRelativeTarget(t, i, s) {
      this.relativeParent = t, this.linkedParentVersion = t.layoutVersion, this.forceRelativeParentToResolveTarget(), this.relativeTarget = f(), this.relativeTargetOrigin = f(), w(this.relativeTargetOrigin, i, s, this.options.layoutAnchor || void 0), y(this.relativeTarget, this.relativeTargetOrigin);
    }
    removeRelativeTarget() {
      this.relativeParent = this.relativeTarget = void 0;
    }
    calcProjection() {
      var d;
      const t = this.getLead(), i = !!this.resumingFrom || this !== t;
      let s = !0;
      if ((this.isProjectionDirty || (d = this.parent) != null && d.isProjectionDirty) && (s = !1), i && (this.isSharedProjectionDirty || this.isTransformDirty) && (s = !1), this.resolvedRelativeTargetAt === g.timestamp && (s = !1), s)
        return;
      const { layout: o, layoutId: a } = this.options;
      if (this.isTreeAnimating = !!(this.parent && this.parent.isTreeAnimating || this.currentAnimation || this.pendingAnimation), this.isTreeAnimating || (this.targetDelta = this.relativeTarget = void 0), !this.layout || !(o || a))
        return;
      y(this.layoutCorrected, this.layout.layoutBox);
      const r = this.treeScale.x, h = this.treeScale.y;
      gt(this.layoutCorrected, this.treeScale, this.path, i), t.layout && !t.target && (this.treeScale.x !== 1 || this.treeScale.y !== 1) && (t.target = t.layout.layoutBox, t.targetWithTransforms = f());
      const { target: l } = t;
      if (!l) {
        this.prevProjectionDelta && (this.createProjectionDeltas(), this.scheduleRender());
        return;
      }
      !this.projectionDelta || !this.prevProjectionDelta ? this.createProjectionDeltas() : (z(this.prevProjectionDelta.x, this.projectionDelta.x), z(this.prevProjectionDelta.y, this.projectionDelta.y)), L(this.projectionDelta, this.layoutCorrected, l, this.latestValues), (this.treeScale.x !== r || this.treeScale.y !== h || !N(this.projectionDelta.x, this.prevProjectionDelta.x) || !N(this.projectionDelta.y, this.prevProjectionDelta.y)) && (this.hasProjected = !0, this.scheduleRender(), this.notifyListeners("projectionUpdate", l));
    }
    hide() {
      this.isVisible = !1;
    }
    show() {
      this.isVisible = !0;
    }
    scheduleRender(t = !0) {
      var i;
      if ((i = this.options.visualElement) == null || i.scheduleRender(), t) {
        const s = this.getStack();
        s && s.scheduleRender();
      }
      this.resumingFrom && !this.resumingFrom.instance && (this.resumingFrom = void 0);
    }
    createProjectionDeltas() {
      this.prevProjectionDelta = R(), this.projectionDelta = R(), this.projectionDeltaWithTransform = R();
    }
    setAnimationOrigin(t, i = !1) {
      const s = this.snapshot, o = s ? s.latestValues : {}, a = { ...this.latestValues }, r = R();
      (!this.relativeParent || !this.relativeParent.options.layoutRoot) && (this.relativeTarget = this.relativeTargetOrigin = void 0), this.attemptToResolveRelativeTarget = !i;
      const h = f(), l = s ? s.source : void 0, d = this.layout ? this.layout.source : void 0, v = l !== d, m = this.getStack(), B = !m || m.members.length <= 1, D = !!(v && !B && this.options.crossfade === !0 && !this.path.some(Ht));
      this.animationProgress = 0;
      let p;
      this.mixTargetDelta = (j) => {
        const V = j / 1e3;
        J(r.x, t.x, V), J(r.y, t.y, V), this.setTargetDelta(r), this.relativeTarget && this.relativeTargetOrigin && this.layout && this.relativeParent && this.relativeParent.layout && (w(h, this.layout.layoutBox, this.relativeParent.layout.layoutBox, this.options.layoutAnchor || void 0), Nt(this.relativeTarget, this.relativeTargetOrigin, h, V), p && xt(this.relativeTarget, p) && (this.isProjectionDirty = !1), p || (p = f()), y(p, this.relativeTarget)), v && (this.animationValues = a, mt(a, o, this.latestValues, V, D, B)), this.root.scheduleUpdateProjection(), this.scheduleRender(), this.animationProgress = V;
      }, this.mixTargetDelta(this.options.layoutRoot ? 1e3 : 0);
    }
    startAnimation(t) {
      var i, s, o;
      this.notifyListeners("animationStart"), (i = this.currentAnimation) == null || i.stop(), (o = (s = this.resumingFrom) == null ? void 0 : s.currentAnimation) == null || o.stop(), this.pendingAnimation && (X(this.pendingAnimation), this.pendingAnimation = void 0), this.pendingAnimation = k.update(() => {
        F.hasAnimatedSinceResize = !0, this.motionValue || (this.motionValue = ft(0)), this.motionValue.jump(0, !1), this.currentAnimation = at(this.motionValue, [0, 1e3], {
          ...t,
          velocity: 0,
          isSync: !0,
          onUpdate: (a) => {
            this.mixTargetDelta(a), t.onUpdate && t.onUpdate(a);
          },
          onStop: () => {
          },
          onComplete: () => {
            t.onComplete && t.onComplete(), this.completeAnimation();
          }
        }), this.resumingFrom && (this.resumingFrom.currentAnimation = this.currentAnimation), this.pendingAnimation = void 0;
      });
    }
    completeAnimation() {
      this.resumingFrom && (this.resumingFrom.currentAnimation = void 0, this.resumingFrom.preserveOpacity = void 0);
      const t = this.getStack();
      t && t.exitAnimationComplete(), this.resumingFrom = this.currentAnimation = this.animationValues = void 0, this.notifyListeners("animationComplete");
    }
    finishAnimation() {
      this.currentAnimation && (this.mixTargetDelta && this.mixTargetDelta(Vt), this.currentAnimation.stop()), this.completeAnimation();
    }
    applyTransformsToTarget() {
      const t = this.getLead();
      let { targetWithTransforms: i, target: s, layout: o, latestValues: a } = t;
      if (!(!i || !s || !o)) {
        if (this !== t && this.layout && o && ot(this.options.animationType, this.layout.layoutBox, o.layoutBox)) {
          s = this.target || f();
          const r = A(this.layout.layoutBox.x);
          s.x.min = t.target.x.min, s.x.max = s.x.min + r;
          const h = A(this.layout.layoutBox.y);
          s.y.min = t.target.y.min, s.y.max = s.y.min + h;
        }
        y(i, s), C(i, a), L(this.projectionDeltaWithTransform, this.layoutCorrected, i, a);
      }
    }
    registerSharedNode(t, i) {
      this.sharedNodes.has(t) || this.sharedNodes.set(t, new Dt()), this.sharedNodes.get(t).add(i);
      const o = i.options.initialPromotionConfig;
      i.promote({
        transition: o ? o.transition : void 0,
        preserveFollowOpacity: o && o.shouldPreserveFollowOpacity ? o.shouldPreserveFollowOpacity(i) : void 0
      });
    }
    isLead() {
      const t = this.getStack();
      return t ? t.lead === this : !0;
    }
    getLead() {
      var i;
      const { layoutId: t } = this.options;
      return t ? ((i = this.getStack()) == null ? void 0 : i.lead) || this : this;
    }
    getPrevLead() {
      var i;
      const { layoutId: t } = this.options;
      return t ? (i = this.getStack()) == null ? void 0 : i.prevLead : void 0;
    }
    getStack() {
      const { layoutId: t } = this.options;
      if (t)
        return this.root.sharedNodes.get(t);
    }
    promote({ needsReset: t, transition: i, preserveFollowOpacity: s } = {}) {
      const o = this.getStack();
      o && o.promote(this, s), t && (this.projectionDelta = void 0, this.needsReset = !0), i && this.setOptions({ transition: i });
    }
    relegate() {
      const t = this.getStack();
      return t ? t.relegate(this) : !1;
    }
    resetSkewAndRotation() {
      const { visualElement: t } = this.options;
      if (!t)
        return;
      let i = !1;
      const { latestValues: s } = t;
      if ((s.z || s.rotate || s.rotateX || s.rotateY || s.rotateZ || s.skewX || s.skewY) && (i = !0), !i)
        return;
      const o = {};
      s.z && M("z", t, o, this.animationValues);
      for (let a = 0; a < I.length; a++)
        M(`rotate${I[a]}`, t, o, this.animationValues), M(`skew${I[a]}`, t, o, this.animationValues);
      t.render();
      for (const a in o)
        t.setStaticValue(a, o[a]), this.animationValues && (this.animationValues[a] = o[a]);
      t.scheduleRender();
    }
    applyProjectionStyles(t, i) {
      if (!this.instance || this.isSVG)
        return;
      if (!this.isVisible) {
        t.visibility = "hidden";
        return;
      }
      const s = this.getTransformTemplate();
      if (this.needsReset) {
        this.needsReset = !1, t.visibility = "", t.opacity = "", t.pointerEvents = U(i == null ? void 0 : i.pointerEvents) || "", t.transform = s ? s(this.latestValues, "") : "none";
        return;
      }
      const o = this.getLead();
      if (!this.projectionDelta || !this.layout || !o.target) {
        this.options.layoutId && (t.opacity = this.latestValues.opacity !== void 0 ? this.latestValues.opacity : 1, t.pointerEvents = U(i == null ? void 0 : i.pointerEvents) || ""), this.hasProjected && !P(this.latestValues) && (t.transform = s ? s({}, "") : "none", this.hasProjected = !1);
        return;
      }
      t.visibility = "";
      const a = o.animationValues || o.latestValues;
      this.applyTransformsToTarget();
      let r = Bt(this.projectionDeltaWithTransform, this.treeScale, a);
      s && (r = s(a, r)), t.transform = r;
      const { x: h, y: l } = this.projectionDelta;
      t.transformOrigin = `${h.origin * 100}% ${l.origin * 100}% 0`, o.animationValues ? t.opacity = o === this ? a.opacity ?? this.latestValues.opacity ?? 1 : this.preserveOpacity ? this.latestValues.opacity : a.opacityExit : t.opacity = o === this ? a.opacity !== void 0 ? a.opacity : "" : a.opacityExit !== void 0 ? a.opacityExit : 0;
      for (const d in b) {
        if (a[d] === void 0)
          continue;
        const { correct: v, applyTo: m, isCSSVariable: B } = b[d], D = r === "none" ? a[d] : v(a[d], o);
        if (m) {
          const p = m.length;
          for (let j = 0; j < p; j++)
            t[m[j]] = D;
        } else
          B ? this.options.visualElement.renderState.vars[d] = D : t[d] = D;
      }
      this.options.layoutId && (t.pointerEvents = o === this ? U(i == null ? void 0 : i.pointerEvents) || "" : "none");
    }
    clearSnapshot() {
      this.resumeFrom = this.snapshot = void 0;
    }
    // Only run on root
    resetTree() {
      this.root.nodes.forEach((t) => {
        var i;
        return (i = t.currentAnimation) == null ? void 0 : i.stop();
      }), this.root.nodes.forEach(Y), this.root.sharedNodes.clear();
    }
  };
}
function Lt(e) {
  e.updateLayout();
}
function Et(e) {
  var c;
  const n = ((c = e.resumeFrom) == null ? void 0 : c.snapshot) || e.snapshot;
  if (e.isLead() && e.layout && n && e.hasListeners("didUpdate")) {
    const { layoutBox: u, measuredBox: T } = e.layout, { animationType: x } = e.options, t = n.source !== e.layout.source;
    if (x === "size")
      G((r) => {
        const h = t ? n.measuredBox[r] : n.layoutBox[r], l = A(h);
        h.min = u[r].min, h.max = h.min + l;
      });
    else if (x === "x" || x === "y") {
      const r = x === "x" ? "y" : "x";
      pt(t ? n.measuredBox[r] : n.layoutBox[r], u[r]);
    } else ot(x, n.layoutBox, u) && G((r) => {
      const h = t ? n.measuredBox[r] : n.layoutBox[r], l = A(u[r]);
      h.max = h.min + l, e.relativeTarget && !e.currentAnimation && (e.isProjectionDirty = !0, e.relativeTarget[r].max = e.relativeTarget[r].min + l);
    });
    const i = R();
    L(i, u, n.layoutBox);
    const s = R();
    t ? L(s, e.applyTransform(T, !0), n.measuredBox) : L(s, u, n.layoutBox);
    const o = !it(i);
    let a = !1;
    if (!e.resumeFrom) {
      const r = e.getClosestProjectingParent();
      if (r && !r.resumeFrom) {
        const { snapshot: h, layout: l } = r;
        if (h && l) {
          const d = e.options.layoutAnchor || void 0, v = f();
          w(v, n.layoutBox, h.layoutBox, d);
          const m = f();
          w(m, u, l.layoutBox, d), et(v, m) || (a = !0), r.options.layoutRoot && (e.relativeTarget = m, e.relativeTargetOrigin = v, e.relativeParent = r);
        }
      }
    }
    e.notifyListeners("didUpdate", {
      layout: u,
      snapshot: n,
      delta: s,
      layoutDelta: i,
      hasLayoutChanged: o,
      hasRelativeLayoutChanged: a
    });
  } else if (e.isLead()) {
    const { onExitComplete: u } = e.options;
    u && u();
  }
  e.options.transition = void 0;
}
function wt(e) {
  e.parent && (e.isProjecting() || (e.isProjectionDirty = e.parent.isProjectionDirty), e.isSharedProjectionDirty || (e.isSharedProjectionDirty = !!(e.isProjectionDirty || e.parent.isProjectionDirty || e.parent.isSharedProjectionDirty)), e.isTransformDirty || (e.isTransformDirty = e.parent.isTransformDirty));
}
function Ut(e) {
  e.isProjectionDirty = e.isSharedProjectionDirty = e.isTransformDirty = !1;
}
function Ct(e) {
  e.clearSnapshot();
}
function Y(e) {
  e.clearMeasurements();
}
function Ft(e) {
  e.isLayoutDirty = !0, e.updateLayout();
}
function $(e) {
  e.isLayoutDirty = !1;
}
function Ot(e) {
  e.isAnimationBlocked && e.layout && !e.isLayoutDirty && (e.snapshot = e.layout, e.isLayoutDirty = !0);
}
function It(e) {
  const { visualElement: n } = e.options;
  n && n.getProps().onBeforeLayoutMeasure && n.notify("BeforeLayoutMeasure"), e.resetTransform();
}
function Z(e) {
  e.finishAnimation(), e.targetDelta = e.relativeTarget = e.target = void 0, e.isProjectionDirty = !0;
}
function Mt(e) {
  e.resolveTargetDelta();
}
function bt(e) {
  e.calcProjection();
}
function zt(e) {
  e.resetSkewAndRotation();
}
function Wt(e) {
  e.removeLeadSnapshot();
}
function J(e, n, c) {
  e.translate = E(n.translate, 0, c), e.scale = E(n.scale, 1, c), e.origin = n.origin, e.originPoint = n.originPoint;
}
function K(e, n, c, u) {
  e.min = E(n.min, c.min, u), e.max = E(n.max, c.max, u);
}
function Nt(e, n, c, u) {
  K(e.x, n.x, c.x, u), K(e.y, n.y, c.y, u);
}
function Ht(e) {
  return e.animationValues && e.animationValues.opacityExit !== void 0;
}
const Gt = {
  duration: 0.45,
  ease: [0.4, 0, 0.1, 1]
}, Q = (e) => typeof navigator < "u" && navigator.userAgent && navigator.userAgent.toLowerCase().includes(e), _ = Q("applewebkit/") && !Q("chrome/") ? Math.round : jt;
function tt(e) {
  e.min = _(e.min), e.max = _(e.max);
}
function qt(e) {
  tt(e.x), tt(e.y);
}
function ot(e, n, c) {
  return e === "position" || e === "preserve-aspect" && !Tt(H(n), H(c), 0.2);
}
function Xt(e) {
  var n;
  return e !== e.root && ((n = e.scroll) == null ? void 0 : n.wasRoot);
}
export {
  Ut as cleanDirtyNodes,
  Se as createProjectionNode,
  K as mixAxis,
  J as mixAxisDelta,
  Nt as mixBox,
  wt as propagateDirtyNodes
};
//# sourceMappingURL=create-projection-node.es.js.map
