"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Observable = void 0;
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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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); }
var Observable = exports.Observable = /*#__PURE__*/function () {
  function Observable(object) {
    _classCallCheck(this, Observable);
    _defineProperty(this, "_eventListeners", []);
    _defineProperty(this, "_object", null);
    this._object = object;
    /*
    , inject = ['on', 'off', 'dispatch']
    if (inject && (inject instanceof Array)) {
        for (let m of inject) {
            object[m] = this[m].bind(this);
        }
    }
    */
  }
  _createClass(Observable, [{
    key: "on",
    value: function on(eventName, callback) {
      var _this = this;
      if (this.indexOf(eventName, callback) !== -1) {
        // eslint-disable-next-line no-console
        console.warn("The following callback is already added to the event: ".concat(eventName), callback);
        return;
      }
      this._eventListeners.push({
        eventName: eventName,
        callback: callback
      });
      return function () {
        return _this.off(eventName, callback);
      };
    }
  }, {
    key: "off",
    value: function off(eventName, callback) {
      var idx = this.indexOf(eventName, callback);
      if (idx !== -1) {
        this._eventListeners.splice(idx, 1);
      }
    }
  }, {
    key: "indexOf",
    value: function indexOf(eventName, callback) {
      for (var o = 0; o < this._eventListeners.length; o++) {
        var el = this._eventListeners[o];
        if (el.eventName === eventName && el.callback === callback) {
          return o;
        }
      }
      return -1;
    }
  }, {
    key: "dispatch",
    value: function dispatch(eventName, params) {
      var direct = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
      var notify = this.createNotifyFunction(eventName, params);
      if (direct) {
        notify();
      } else {
        setTimeout(notify);
      }
    }
  }, {
    key: "createNotifyFunction",
    value: function createNotifyFunction(eventName, params) {
      var _this2 = this;
      return function () {
        var _iterator = _createForOfIteratorHelper(_this2._eventListeners),
          _step;
        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var el = _step.value;
            if (el.eventName === eventName) {
              el.callback.call(_this2._object, {
                eventName: eventName,
                time: new Date(),
                data: params
              });
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      };
    }
  }, {
    key: "dispose",
    value: function dispose() {
      this._eventListeners = [];
      this._object = undefined;
    }
  }]);
  return Observable;
}();
var _default = exports["default"] = Observable;