var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
function getLiveQueryClient() {
  return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
}
var LiveQuery = function () {
  function LiveQuery() {
    var _this = this;
    (0, _classCallCheck2.default)(this, LiveQuery);
    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)(LiveQuery, [{
    key: "open",
    value: function () {
      var _open = (0, _asyncToGenerator2.default)(function* () {
        var liveQueryClient = yield getLiveQueryClient();
        liveQueryClient.open();
      });
      function open() {
        return _open.apply(this, arguments);
      }
      return open;
    }()
  }, {
    key: "close",
    value: function () {
      var _close = (0, _asyncToGenerator2.default)(function* () {
        var liveQueryClient = yield getLiveQueryClient();
        liveQueryClient.close();
      });
      function close() {
        return _close.apply(this, arguments);
      }
      return close;
    }()
  }]);
}();
var _default = exports.default = LiveQuery;
var defaultLiveQueryClient;
var DefaultLiveQueryController = {
  setDefaultLiveQueryClient: function (liveQueryClient) {
    defaultLiveQueryClient = liveQueryClient;
  },
  getDefaultLiveQueryClient: function () {
    var _getDefaultLiveQueryClient = (0, _asyncToGenerator2.default)(function* () {
      if (defaultLiveQueryClient) {
        return defaultLiveQueryClient;
      }
      var _yield$Promise$all = yield Promise.all([_CoreManager.default.getUserController().currentUserAsync(), _CoreManager.default.getInstallationController().currentInstallationId()]),
        _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 2),
        currentUser = _yield$Promise$all2[0],
        installationId = _yield$Promise$all2[1];
      var sessionToken = currentUser ? currentUser.getSessionToken() : undefined;
      var liveQueryServerURL = _CoreManager.default.get('LIVEQUERY_SERVER_URL');
      if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
        throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
      }
      if (!liveQueryServerURL) {
        var serverURL = _CoreManager.default.get('SERVER_URL');
        var protocol = serverURL.indexOf('https') === 0 ? 'wss://' : 'ws://';
        var host = serverURL.replace(/^https?:\/\//, '');
        liveQueryServerURL = protocol + host;
        _CoreManager.default.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
      }
      var applicationId = _CoreManager.default.get('APPLICATION_ID');
      var javascriptKey = _CoreManager.default.get('JAVASCRIPT_KEY');
      var masterKey = _CoreManager.default.get('MASTER_KEY');
      defaultLiveQueryClient = new _LiveQueryClient.default({
        applicationId: applicationId,
        serverURL: liveQueryServerURL,
        javascriptKey: javascriptKey,
        masterKey: masterKey,
        sessionToken: sessionToken,
        installationId: installationId
      });
      var LiveQuery = _CoreManager.default.getLiveQuery();
      defaultLiveQueryClient.on('error', function (error) {
        LiveQuery.emit('error', error);
      });
      defaultLiveQueryClient.on('open', function () {
        LiveQuery.emit('open');
      });
      defaultLiveQueryClient.on('close', function () {
        LiveQuery.emit('close');
      });
      return defaultLiveQueryClient;
    });
    function getDefaultLiveQueryClient() {
      return _getDefaultLiveQueryClient.apply(this, arguments);
    }
    return getDefaultLiveQueryClient;
  }(),
  _clearCachedDefaultClient: function () {
    defaultLiveQueryClient = null;
  }
};
_CoreManager.default.setLiveQueryController(DefaultLiveQueryController);