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 _CoreManager = _interopRequireDefault(require("./CoreManager"));
var _promiseUtils = require("./promiseUtils");
var Subscription = function () {
  function Subscription(id, query, sessionToken) {
    var _this = this;
    (0, _classCallCheck2.default)(this, Subscription);
    this.id = id;
    this.query = query;
    this.sessionToken = sessionToken;
    this.subscribePromise = (0, _promiseUtils.resolvingPromise)();
    this.unsubscribePromise = (0, _promiseUtils.resolvingPromise)();
    this.subscribed = false;
    var EventEmitter = _CoreManager.default.getEventEmitter();
    this.emitter = new EventEmitter();
    this.on = function (eventName, listener) {
      return _this.emitter.on(eventName, listener);
    };
    this.emit = function (eventName) {
      var _this$emitter;
      for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        args[_key - 1] = arguments[_key];
      }
      return (_this$emitter = _this.emitter).emit.apply(_this$emitter, [eventName].concat(args));
    };
    this.on('error', function () {});
  }
  return (0, _createClass2.default)(Subscription, [{
    key: "unsubscribe",
    value: function () {
      var _this2 = this;
      return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient().then(function (liveQueryClient) {
        _this2.emit('close');
        return liveQueryClient.unsubscribe(_this2);
      });
    }
  }]);
}();
var _default = exports.default = Subscription;