"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const array = require("../../utils/array.js");
var NodeStack = (
  /** @class */
  function() {
    function NodeStack2() {
      this.members = [];
    }
    NodeStack2.prototype.add = function(node) {
      array.addUniqueItem(this.members, node);
      node.scheduleRender();
    };
    NodeStack2.prototype.remove = function(node) {
      array.removeItem(this.members, node);
      if (node === this.prevLead) {
        this.prevLead = void 0;
      }
      if (node === this.lead) {
        var prevLead = this.members[this.members.length - 1];
        if (prevLead) {
          this.promote(prevLead);
        }
      }
    };
    NodeStack2.prototype.relegate = function(node) {
      var indexOfNode = this.members.findIndex(function(member2) {
        return node === member2;
      });
      if (indexOfNode === 0)
        return false;
      var prevLead;
      for (var i = indexOfNode; i >= 0; i--) {
        var member = this.members[i];
        if (member.isPresent !== false) {
          prevLead = member;
          break;
        }
      }
      if (prevLead) {
        this.promote(prevLead);
        return true;
      } else {
        return false;
      }
    };
    NodeStack2.prototype.promote = function(node, preserveFollowOpacity) {
      var _a;
      var prevLead = this.lead;
      if (node === prevLead)
        return;
      this.prevLead = prevLead;
      this.lead = node;
      node.show();
      if (prevLead) {
        prevLead.instance && prevLead.scheduleRender();
        node.scheduleRender();
        node.resumeFrom = prevLead;
        if (preserveFollowOpacity) {
          node.resumeFrom.preserveOpacity = true;
        }
        if (prevLead.snapshot) {
          node.snapshot = prevLead.snapshot;
          node.snapshot.latestValues = prevLead.animationValues || prevLead.latestValues;
          node.snapshot.isShared = true;
        }
        if ((_a = node.root) === null || _a === void 0 ? void 0 : _a.isUpdating) {
          node.isLayoutDirty = true;
        }
        var crossfade = node.options.crossfade;
        if (crossfade === false) {
          prevLead.hide();
        }
      }
    };
    NodeStack2.prototype.exitAnimationComplete = function() {
      this.members.forEach(function(node) {
        var _a, _b, _c, _d, _e;
        (_b = (_a = node.options).onExitComplete) === null || _b === void 0 ? void 0 : _b.call(_a);
        (_e = (_c = node.resumingFrom) === null || _c === void 0 ? void 0 : (_d = _c.options).onExitComplete) === null || _e === void 0 ? void 0 : _e.call(_d);
      });
    };
    NodeStack2.prototype.scheduleRender = function() {
      this.members.forEach(function(node) {
        node.instance && node.scheduleRender(false);
      });
    };
    NodeStack2.prototype.removeLeadSnapshot = function() {
      if (this.lead && this.lead.snapshot) {
        this.lead.snapshot = void 0;
      }
    };
    return NodeStack2;
  }()
);
exports.NodeStack = NodeStack;
//# sourceMappingURL=stack.js.map
