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 _EventEmitter = _interopRequireDefault(require("./EventEmitter"));
var _LiveQueryClient = _interopRequireDefault(require("./LiveQueryClient"));
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
function getLiveQueryClient() {
  return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient();
}
var LiveQuery = new _EventEmitter.default();
LiveQuery.open = (0, _asyncToGenerator2.default)(function* () {
  var liveQueryClient = yield getLiveQueryClient();
  liveQueryClient.open();
});
LiveQuery.close = (0, _asyncToGenerator2.default)(function* () {
  var liveQueryClient = yield getLiveQueryClient();
  liveQueryClient.close();
});
LiveQuery.on('error', function () {});
var _default = LiveQuery;
exports.default = _default;
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
      });
      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);