System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4"], function (_export, _context) {
  "use strict";

  var _reporterNs, _cclegacy, gameServiceProto, AWsClient, logger, Result, GameClient, _crd;

  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

  function _reportPossibleCrUseOfBaseWsClient(extras) {
    _reporterNs.report("BaseWsClient", "tsrpc-base-client", _context.meta, extras);
  }

  function _reportPossibleCrUseOfBaseWsClientOptions(extras) {
    _reporterNs.report("BaseWsClientOptions", "tsrpc-base-client", _context.meta, extras);
  }

  function _reportPossibleCrUseOfServiceProto(extras) {
    _reporterNs.report("ServiceProto", "tsrpc-proto", _context.meta, extras);
  }

  function _reportPossibleCrUseOfgameServiceProto(extras) {
    _reporterNs.report("gameServiceProto", "./protocols/serviceProto", _context.meta, extras);
  }

  function _reportPossibleCrUseOfgameServiceType(extras) {
    _reporterNs.report("gameServiceType", "./protocols/serviceProto", _context.meta, extras);
  }

  function _reportPossibleCrUseOfAWsClient(extras) {
    _reporterNs.report("AWsClient", "../tsgf/AClient", _context.meta, extras);
  }

  function _reportPossibleCrUseOflogger(extras) {
    _reporterNs.report("logger", "../tsgf/logger", _context.meta, extras);
  }

  function _reportPossibleCrUseOfResult(extras) {
    _reporterNs.report("Result", "../tsgf/Result", _context.meta, extras);
  }

  _export("GameClient", void 0);

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc) {
      _cclegacy = _cc.cclegacy;
    }, function (_unresolved_2) {
      gameServiceProto = _unresolved_2.serviceProto;
    }, function (_unresolved_3) {
      AWsClient = _unresolved_3.AWsClient;
    }, function (_unresolved_4) {
      logger = _unresolved_4.logger;
    }, function (_unresolved_5) {
      Result = _unresolved_5.Result;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "549e4Irjc1OvrC4WbwubGQn", "gameClient", undefined);

      _export("GameClient", GameClient = class GameClient extends (_crd && AWsClient === void 0 ? (_reportPossibleCrUseOfAWsClient({
        error: Error()
      }), AWsClient) : AWsClient) {
        constructor(buildClient, serverUrl) {
          var _this;

          super(buildClient, _crd && gameServiceProto === void 0 ? (_reportPossibleCrUseOfgameServiceProto({
            error: Error()
          }), gameServiceProto) : gameServiceProto, {
            server: serverUrl,
            json: false,
            logger: _crd && logger === void 0 ? (_reportPossibleCrUseOflogger({
              error: Error()
            }), logger) : logger
          });
          _this = this;

          _defineProperty(this, "connectionId", void 0);

          _defineProperty(this, "reconnectWaitSec", 2);

          _defineProperty(this, "reconnectTimerHD", void 0);

          _defineProperty(this, "disconnectHandler", void 0);

          _defineProperty(this, "onReconnectResult", void 0);

          this.connectionId = ""; //设置断线重连的中间件

          this.client.flows.postDisconnectFlow.push( /*#__PURE__*/function () {
            var _ref = _asyncToGenerator(function* (v) {
              var _this$disconnectHandl;

              //如果都没连上过就断开,那么忽略
              if (!_this.connectionId) return v; //判断是否需要重连

              if (!v.isManual && ((_this$disconnectHandl = _this.disconnectHandler) === null || _this$disconnectHandl === void 0 ? void 0 : _this$disconnectHandl.call(_this))) {
                var _this$client$logger;

                (_this$client$logger = _this.client.logger) === null || _this$client$logger === void 0 ? void 0 : _this$client$logger.error('连接已断开,等待' + _this.reconnectWaitSec + '秒后自动重连');
                if (_this.reconnectTimerHD) clearTimeout(_this.reconnectTimerHD);
                _this.reconnectTimerHD = setTimeout( /*#__PURE__*/_asyncToGenerator(function* () {
                  return _this.startReconnect(true);
                }), _this.reconnectWaitSec * 1000);
              } else {
                var _this$client$logger2;

                (_this$client$logger2 = _this.client.logger) === null || _this$client$logger2 === void 0 ? void 0 : _this$client$logger2.error('连接已断开');
              }

              return v;
            });

            return function (_x) {
              return _ref.apply(this, arguments);
            };
          }());
        }

        disconnect() {
          var _this2 = this;

          return _asyncToGenerator(function* () {
            _this2.connectionId = '';
            yield _this2.client.disconnect();

            _this2.stopReconnect();
          })();
        }

        stopReconnect() {
          if (this.reconnectTimerHD) clearTimeout(this.reconnectTimerHD);
        }

        startReconnect(failReTry) {
          var _this3 = this;

          return _asyncToGenerator(function* () {
            var _this3$disconnectHand;

            if (failReTry === void 0) {
              failReTry = true;
            }

            var result = yield _this3.reconnect(); // 重连也错误，弹出错误提示

            if (result.succ) {
              var _this3$client$logger, _this3$onReconnectRes;

              (_this3$client$logger = _this3.client.logger) === null || _this3$client$logger === void 0 ? void 0 : _this3$client$logger.log('重连成功!');
              (_this3$onReconnectRes = _this3.onReconnectResult) === null || _this3$onReconnectRes === void 0 ? void 0 : _this3$onReconnectRes.call(_this3, true, null);
              return true;
            } //如果是逻辑拒绝则不需要重连


            if (!_this3.connectionId || result.code == 5001) failReTry = false;

            if (failReTry && ((_this3$disconnectHand = _this3.disconnectHandler) === null || _this3$disconnectHand === void 0 ? void 0 : _this3$disconnectHand.call(_this3))) {
              var _this3$client$logger2;

              (_this3$client$logger2 = _this3.client.logger) === null || _this3$client$logger2 === void 0 ? void 0 : _this3$client$logger2.error('重连失败:' + result.err + ' ' + _this3.reconnectWaitSec + '秒后自动重连!');
              if (_this3.reconnectTimerHD) clearTimeout(_this3.reconnectTimerHD);
              _this3.reconnectTimerHD = setTimeout(() => _this3.startReconnect(failReTry), _this3.reconnectWaitSec * 1000);
            } else {
              var _this3$client$logger3, _this3$onReconnectRes2;

              (_this3$client$logger3 = _this3.client.logger) === null || _this3$client$logger3 === void 0 ? void 0 : _this3$client$logger3.error('重连失败:' + result.err);

              _this3.disconnect();

              (_this3$onReconnectRes2 = _this3.onReconnectResult) === null || _this3$onReconnectRes2 === void 0 ? void 0 : _this3$onReconnectRes2.call(_this3, false, result.err);
            }

            return false;
          })();
        }
        /**
         * 使用临时分配的令牌登录
         * @param loginToken 
         * @returns  
         */


        login(loginToken) {
          var _this4 = this;

          return _asyncToGenerator(function* () {
            var connectRet = yield _this4.client.connect();

            if (!connectRet.isSucc) {
              return "连接失败:" + connectRet.errMsg;
            }

            var loginRet = yield _this4.client.callApi("Login", {
              loginToken: loginToken
            });

            if (!loginRet.isSucc) {
              _this4.disconnect();

              return loginRet.err.message;
            }

            _this4.connectionId = loginRet.res.connectionId;
            return null;
          })();
        }
        /**
         * 断线重连, 失败的话要看code,5001表示逻辑拒绝,不需要重连
         * @param loginToken 
         * @returns  
         */


        reconnect() {
          var _this5 = this;

          return _asyncToGenerator(function* () {
            var connectRet = yield _this5.client.connect();

            if (!connectRet.isSucc) {
              return (_crd && Result === void 0 ? (_reportPossibleCrUseOfResult({
                error: Error()
              }), Result) : Result).buildErr("连接失败:" + connectRet.errMsg);
            }

            var loginRet = yield _this5.client.callApi("Reconnect", {
              connectionId: _this5.connectionId
            });

            if (!loginRet.isSucc) {
              var _loginRet$err$code;

              var code = (_loginRet$err$code = loginRet.err.code) !== null && _loginRet$err$code !== void 0 ? _loginRet$err$code : 0;
              return (_crd && Result === void 0 ? (_reportPossibleCrUseOfResult({
                error: Error()
              }), Result) : Result).buildErr(loginRet.err.message, code);
            }

            _this5.connectionId = loginRet.res.connectionId;
            return (_crd && Result === void 0 ? (_reportPossibleCrUseOfResult({
              error: Error()
            }), Result) : Result).buildSucc(null);
          })();
        }

      });

      _cclegacy._RF.pop();

      _crd = false;
    }
  };
});
//# sourceMappingURL=gameClient.js.map