"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var ADFTraversor = /*#__PURE__*/function () {
  function ADFTraversor(doc) {
    (0, _classCallCheck2.default)(this, ADFTraversor);
    (0, _defineProperty2.default)(this, "subscribers", new Map());
    this.doc = doc;
  }
  (0, _createClass2.default)(ADFTraversor, [{
    key: "subscribe",
    value: function subscribe(type, callback) {
      var callbacks = this.subscribers.get(type);
      if (!callbacks) {
        this.subscribers.set(type, [callback]);
      } else {
        callbacks.push(callback);
      }
    }
  }, {
    key: "exec",
    value: function exec() {
      if (!this.doc || !Array.isArray(this.doc.content)) {
        return;
      }
      var candidates = this.doc.content.slice(0);
      while (candidates.length) {
        var _node = candidates.shift();
        if (Array.isArray(_node.content)) {
          candidates = candidates.concat(_node.content);
        }
        var callbacks = this.subscribers.get(_node.type);
        if (!callbacks) {
          continue;
        }
        for (var i = 0; i < callbacks.length; i++) {
          callbacks[i](_node);
        }
      }
    }
  }]);
  return ADFTraversor;
}();
var _default = exports.default = ADFTraversor;