import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';

var OverlayManager = /*#__PURE__*/function () {
  function OverlayManager() {
    _classCallCheck(this, OverlayManager);
    this.overlays = [];
    this.previouslyFocusedElements = [];
    this.subscribers = new Set();
    this.add = this.add.bind(this);
    this.remove = this.remove.bind(this);
    this.isTopOverlay = this.isTopOverlay.bind(this);
    this.hasOverlays = this.hasOverlays.bind(this);
    this.getFinalFocusInOverlay = this.getFinalFocusInOverlay.bind(this);
    this.getTopMostOverlay = this.getTopMostOverlay.bind(this);
    this.subscribe = this.subscribe.bind(this);
  }
  return _createClass(OverlayManager, [{
    key: "subscribe",
    value: function subscribe(callback) {
      var _this = this;
      this.subscribers.add(callback);
      return function () {
        return _this.subscribers["delete"](callback);
      };
    }
  }, {
    key: "add",
    value: function add(overlay, elementToFocus) {
      this.overlays.push(overlay);
      this.previouslyFocusedElements.push(elementToFocus);
      this.subscribers.forEach(function (subscriber) {
        return subscriber();
      });
    }
  }, {
    key: "remove",
    value: function remove(overlay, elementToFocus) {
      this.overlays = this.overlays.filter(function (_overlay) {
        return _overlay !== overlay;
      });
      this.previouslyFocusedElements = this.previouslyFocusedElements.filter(function (_elementToFocus) {
        return _elementToFocus !== elementToFocus;
      });
      this.subscribers.forEach(function (subscriber) {
        return subscriber();
      });
    }
  }, {
    key: "isTopOverlay",
    value: function isTopOverlay(overlay) {
      var topMostOverlay = this.overlays[this.overlays.length - 1];
      return topMostOverlay === overlay;
    }
  }, {
    key: "hasOverlays",
    value: function hasOverlays() {
      return this.overlays.length > 0;
    }
  }, {
    key: "getFinalFocusInOverlay",
    value: function getFinalFocusInOverlay() {
      var finalFocus = this.previouslyFocusedElements.pop();
      this.subscribers.forEach(function (subscriber) {
        return subscriber();
      });
      return finalFocus;
    }
  }, {
    key: "getTopMostOverlay",
    value: function getTopMostOverlay() {
      return this.overlays[this.overlays.length - 1];
    }
  }]);
}();
var manager = new OverlayManager();

export { manager };
//# sourceMappingURL=overlayManager.js.map
