"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _util = require("../../../util");

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var ServiceQueue =
/*#__PURE__*/
function () {
  function ServiceQueue() {
    _classCallCheck(this, ServiceQueue);

    this.innerQueue = [];
    this.innerSubscribers = [];
  }

  _createClass(ServiceQueue, [{
    key: "add",
    value: function add(value) {
      this.queue = [].concat(_toConsumableArray(this.queue), [value]);
      return value;
    }
  }, {
    key: "delete",
    value: function _delete(key) {
      this.queue = this.queue.filter(function (stack) {
        return stack.key !== key;
      });
    }
  }, {
    key: "deleteAll",
    value: function deleteAll() {
      this.queue = [];
    }
  }, {
    key: "subscribe",
    value: function subscribe(next) {
      var key = (0, _util.guid)();
      var nextSubscriber = {
        key: key,
        next: next
      };
      this.subscribers = [].concat(_toConsumableArray(this.subscribers), [nextSubscriber]);
      return key;
    }
  }, {
    key: "unsubscribe",
    value: function unsubscribe(key) {
      this.subscribers = this.subscribers.filter(function (subscriber) {
        return subscriber.key !== key;
      });
    }
  }, {
    key: "unsubscribeAll",
    value: function unsubscribeAll() {
      this.subscribers = [];
    }
  }, {
    key: "publish",
    value: function publish() {
      var _this = this;

      if (this.subscribers.length !== 0) {
        this.subscribers.forEach(function (subscriber) {
          var next = subscriber.next;
          next(_this.queue);
        });
      }
    }
  }, {
    key: "queue",
    get: function get() {
      return this.innerQueue;
    },
    set: function set(value) {
      this.innerQueue = value;
      this.publish();
    }
  }, {
    key: "subscribers",
    get: function get() {
      return this.innerSubscribers;
    },
    set: function set(value) {
      this.innerSubscribers = value;
      this.publish();
    }
  }]);

  return ServiceQueue;
}();

exports["default"] = ServiceQueue;