"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Deferred = void 0;
Object.defineProperty(exports, "DeferredError", {
  enumerable: true,
  get: function get() {
    return _DeferredError["default"];
  }
});
exports["default"] = exports.DeferredGroup = void 0;
var _DeferredError = _interopRequireDefault(require("../../Shared/ErrorTypes/DeferredError"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * @private
 * @type {{}}
 */
var promises = {};
var Deferred = exports.Deferred = /*#__PURE__*/function () {
  function Deferred() {
    var _this = this;
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
    _classCallCheck(this, Deferred);
    _defineProperty(this, "resolve", function () {
      _this.remove();
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
      _this._resolve.apply(0, args);
    });
    _defineProperty(this, "reject", function () {
      _this.remove();
      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }
      _this._reject.apply(0, args);
    });
    if (promises[name]) {
      throw new _DeferredError["default"]('Instance already exists with the name: ' + name);
    }
    this.name = name;
    this.promise = new Promise(function (resolve, reject) {
      _this._resolve = resolve;
      _this._reject = reject;
    });
    if (name) {
      promises[name] = this;
    }
  }
  _createClass(Deferred, [{
    key: "remove",
    value: function remove() {
      delete promises[this.name];
    }
  }, {
    key: "then",
    value: function then(fn) {
      this.promise.then(fn);
    }
  }], [{
    key: "get",
    value: function get(name) {
      if (!promises[name]) {
        throw new _DeferredError["default"]('Instance does not exist with the name: ' + name);
      }
      return promises[name];
    }
  }]);
  return Deferred;
}();
var _default = exports["default"] = Deferred;
var DeferredGroup = exports.DeferredGroup = /*#__PURE__*/function () {
  function DeferredGroup() {
    _classCallCheck(this, DeferredGroup);
    _defineProperty(this, "deferredList", []);
    if (arguments.length > 0) {
      this.deferredList = this.deferredList.concat(arguments);
    }
  }
  _createClass(DeferredGroup, [{
    key: "create",
    value: function create() {
      var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
      var deferred = new Deferred(name);
      this.add(deferred);
      return deferred;
    }
  }, {
    key: "createResolver",
    value: function createResolver() {
      var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
      return this.create(name).resolve;
    }
  }, {
    key: "add",
    value: function add(deferred) {
      this.deferredList.push(deferred);
    }
  }, {
    key: "then",
    value: function then(fn) {
      return Promise.all(this.deferredList.map(function (d) {
        return d.promise;
      })).then(fn);
    }
  }]);
  return DeferredGroup;
}();