import { addUniqueItem as o, removeItem as a } from "../../../../../../../motion-utils@12.36.0/node_modules/motion-utils/dist/es/array.es.js";
class p {
  constructor() {
    this.members = [];
  }
  add(e) {
    o(this.members, e);
    for (let t = this.members.length - 1; t >= 0; t--) {
      const s = this.members[t];
      if (s === e || s === this.lead || s === this.prevLead)
        continue;
      const i = s.instance;
      (!i || i.isConnected === !1) && !s.snapshot && (a(this.members, s), s.unmount());
    }
    e.scheduleRender();
  }
  remove(e) {
    if (a(this.members, e), e === this.prevLead && (this.prevLead = void 0), e === this.lead) {
      const t = this.members[this.members.length - 1];
      t && this.promote(t);
    }
  }
  relegate(e) {
    var t;
    for (let s = this.members.indexOf(e) - 1; s >= 0; s--) {
      const i = this.members[s];
      if (i.isPresent !== !1 && ((t = i.instance) == null ? void 0 : t.isConnected) !== !1)
        return this.promote(i), !0;
    }
    return !1;
  }
  promote(e, t) {
    var i;
    const s = this.lead;
    if (e !== s && (this.prevLead = s, this.lead = e, e.show(), s)) {
      s.updateSnapshot(), e.scheduleRender();
      const { layoutDependency: r } = s.options, { layoutDependency: n } = e.options;
      (r === void 0 || r !== n) && (e.resumeFrom = s, t && (s.preserveOpacity = !0), s.snapshot && (e.snapshot = s.snapshot, e.snapshot.latestValues = s.animationValues || s.latestValues), (i = e.root) != null && i.isUpdating && (e.isLayoutDirty = !0)), e.options.crossfade === !1 && s.hide();
    }
  }
  exitAnimationComplete() {
    this.members.forEach((e) => {
      var t, s, i, r, n;
      (s = (t = e.options).onExitComplete) == null || s.call(t), (n = (i = e.resumingFrom) == null ? void 0 : (r = i.options).onExitComplete) == null || n.call(r);
    });
  }
  scheduleRender() {
    this.members.forEach((e) => e.instance && e.scheduleRender(!1));
  }
  removeLeadSnapshot() {
    var e;
    (e = this.lead) != null && e.snapshot && (this.lead.snapshot = void 0);
  }
}
export {
  p as NodeStack
};
//# sourceMappingURL=stack.es.js.map
