System.register(["k8w-extend-native", "core-js/modules/es.object.to-string.js", "core-js/modules/es.regexp.to-string.js", "core-js/modules/es.number.max-safe-integer.js", "core-js/modules/es.number.constructor.js", "core-js/modules/es.regexp.exec.js", "core-js/modules/es.regexp.test.js", "core-js/modules/es.array.iterator.js", "core-js/modules/es.array-buffer.slice.js", "core-js/modules/es.typed-array.uint8-array.js", "core-js/modules/esnext.typed-array.at.js", "core-js/modules/es.typed-array.copy-within.js", "core-js/modules/es.typed-array.every.js", "core-js/modules/es.typed-array.fill.js", "core-js/modules/es.typed-array.filter.js", "core-js/modules/es.typed-array.find.js", "core-js/modules/es.typed-array.find-index.js", "core-js/modules/es.typed-array.for-each.js", "core-js/modules/es.typed-array.includes.js", "core-js/modules/es.typed-array.index-of.js", "core-js/modules/es.typed-array.iterator.js", "core-js/modules/es.typed-array.join.js", "core-js/modules/es.typed-array.last-index-of.js", "core-js/modules/es.typed-array.map.js", "core-js/modules/es.typed-array.reduce.js", "core-js/modules/es.typed-array.reduce-right.js", "core-js/modules/es.typed-array.reverse.js", "core-js/modules/es.typed-array.set.js", "core-js/modules/es.typed-array.slice.js", "core-js/modules/es.typed-array.some.js", "core-js/modules/es.typed-array.sort.js", "core-js/modules/es.typed-array.subarray.js", "core-js/modules/es.typed-array.to-locale-string.js", "core-js/modules/es.typed-array.to-string.js", "core-js/modules/es.array.from.js", "core-js/modules/es.string.iterator.js", "core-js/modules/es.number.parse-int.js", "core-js/modules/es.array.join.js", "core-js/modules/es.string.match.js", "core-js/modules/es.function.name.js", "core-js/modules/es.json.stringify.js", "core-js/modules/es.promise.js", "core-js/modules/es.regexp.constructor.js", "core-js/modules/es.regexp.sticky.js", "core-js/modules/web.dom-collections.for-each.js", "core-js/modules/es.array.filter.js", "core-js/modules/es.object.keys.js", "core-js/modules/es.array.find-index.js", "core-js/modules/es.array.splice.js", "core-js/modules/es.array.slice.js", "core-js/modules/es.array.find.js", "core-js/modules/es.string.ends-with.js", "core-js/modules/es.array.includes.js", "core-js/modules/es.string.includes.js", "core-js/modules/es.string.starts-with.js", "core-js/modules/es.typed-array.int8-array.js", "core-js/modules/es.typed-array.int16-array.js", "core-js/modules/es.typed-array.int32-array.js", "core-js/modules/es.typed-array.uint16-array.js", "core-js/modules/es.typed-array.uint32-array.js", "core-js/modules/es.typed-array.float32-array.js", "core-js/modules/es.typed-array.float64-array.js", "core-js/modules/es.array.map.js", "core-js/modules/es.object.assign.js", "core-js/modules/es.array.index-of.js", "core-js/modules/web.dom-collections.iterator.js", "core-js/modules/es.array-buffer.constructor.js", "core-js/modules/es.array-buffer.is-view.js", "core-js/modules/web.url.to-json.js", "core-js/modules/es.object.entries.js", "core-js/modules/es.number.is-integer.js", "core-js/modules/es.object.get-prototype-of.js", "core-js/modules/es.parse-int.js", "tsrpc-proto", "tslib", "tsrpc-base-client"], function (_export, _context) {
  "use strict";

  var TsrpcError, __awaiter, __generator, __extends, __assign, BaseHttpClient, defaultBaseHttpClientOptions, BaseWsClient, defaultBaseWsClientOptions, HttpProxy, HttpClient, defaultHttpClientOptions, WebSocketProxy, WsClient, defaultWsClientOptions;

  return {
    setters: [function (_k8wExtendNative) {}, function (_coreJsModulesEsObjectToStringJs) {}, function (_coreJsModulesEsRegexpToStringJs) {}, function (_coreJsModulesEsNumberMaxSafeIntegerJs) {}, function (_coreJsModulesEsNumberConstructorJs) {}, function (_coreJsModulesEsRegexpExecJs) {}, function (_coreJsModulesEsRegexpTestJs) {}, function (_coreJsModulesEsArrayIteratorJs) {}, function (_coreJsModulesEsArrayBufferSliceJs) {}, function (_coreJsModulesEsTypedArrayUint8ArrayJs) {}, function (_coreJsModulesEsnextTypedArrayAtJs) {}, function (_coreJsModulesEsTypedArrayCopyWithinJs) {}, function (_coreJsModulesEsTypedArrayEveryJs) {}, function (_coreJsModulesEsTypedArrayFillJs) {}, function (_coreJsModulesEsTypedArrayFilterJs) {}, function (_coreJsModulesEsTypedArrayFindJs) {}, function (_coreJsModulesEsTypedArrayFindIndexJs) {}, function (_coreJsModulesEsTypedArrayForEachJs) {}, function (_coreJsModulesEsTypedArrayIncludesJs) {}, function (_coreJsModulesEsTypedArrayIndexOfJs) {}, function (_coreJsModulesEsTypedArrayIteratorJs) {}, function (_coreJsModulesEsTypedArrayJoinJs) {}, function (_coreJsModulesEsTypedArrayLastIndexOfJs) {}, function (_coreJsModulesEsTypedArrayMapJs) {}, function (_coreJsModulesEsTypedArrayReduceJs) {}, function (_coreJsModulesEsTypedArrayReduceRightJs) {}, function (_coreJsModulesEsTypedArrayReverseJs) {}, function (_coreJsModulesEsTypedArraySetJs) {}, function (_coreJsModulesEsTypedArraySliceJs) {}, function (_coreJsModulesEsTypedArraySomeJs) {}, function (_coreJsModulesEsTypedArraySortJs) {}, function (_coreJsModulesEsTypedArraySubarrayJs) {}, function (_coreJsModulesEsTypedArrayToLocaleStringJs) {}, function (_coreJsModulesEsTypedArrayToStringJs) {}, function (_coreJsModulesEsArrayFromJs) {}, function (_coreJsModulesEsStringIteratorJs) {}, function (_coreJsModulesEsNumberParseIntJs) {}, function (_coreJsModulesEsArrayJoinJs) {}, function (_coreJsModulesEsStringMatchJs) {}, function (_coreJsModulesEsFunctionNameJs) {}, function (_coreJsModulesEsJsonStringifyJs) {}, function (_coreJsModulesEsPromiseJs) {}, function (_coreJsModulesEsRegexpConstructorJs) {}, function (_coreJsModulesEsRegexpStickyJs) {}, function (_coreJsModulesWebDomCollectionsForEachJs) {}, function (_coreJsModulesEsArrayFilterJs) {}, function (_coreJsModulesEsObjectKeysJs) {}, function (_coreJsModulesEsArrayFindIndexJs) {}, function (_coreJsModulesEsArraySpliceJs) {}, function (_coreJsModulesEsArraySliceJs) {}, function (_coreJsModulesEsArrayFindJs) {}, function (_coreJsModulesEsStringEndsWithJs) {}, function (_coreJsModulesEsArrayIncludesJs) {}, function (_coreJsModulesEsStringIncludesJs) {}, function (_coreJsModulesEsStringStartsWithJs) {}, function (_coreJsModulesEsTypedArrayInt8ArrayJs) {}, function (_coreJsModulesEsTypedArrayInt16ArrayJs) {}, function (_coreJsModulesEsTypedArrayInt32ArrayJs) {}, function (_coreJsModulesEsTypedArrayUint16ArrayJs) {}, function (_coreJsModulesEsTypedArrayUint32ArrayJs) {}, function (_coreJsModulesEsTypedArrayFloat32ArrayJs) {}, function (_coreJsModulesEsTypedArrayFloat64ArrayJs) {}, function (_coreJsModulesEsArrayMapJs) {}, function (_coreJsModulesEsObjectAssignJs) {}, function (_coreJsModulesEsArrayIndexOfJs) {}, function (_coreJsModulesWebDomCollectionsIteratorJs) {}, function (_coreJsModulesEsArrayBufferConstructorJs) {}, function (_coreJsModulesEsArrayBufferIsViewJs) {}, function (_coreJsModulesWebUrlToJsonJs) {}, function (_coreJsModulesEsObjectEntriesJs) {}, function (_coreJsModulesEsNumberIsIntegerJs) {}, function (_coreJsModulesEsObjectGetPrototypeOfJs) {}, function (_coreJsModulesEsParseIntJs) {}, function (_tsrpcProto) {
      TsrpcError = _tsrpcProto.TsrpcError;
      var _exportObj = {};

      for (var _key in _tsrpcProto) {
        if (_key !== "default" && _key !== "__esModule") _exportObj[_key] = _tsrpcProto[_key];
      }

      _export(_exportObj);
    }, function (_tslib) {
      __awaiter = _tslib.__awaiter;
      __generator = _tslib.__generator;
      __extends = _tslib.__extends;
      __assign = _tslib.__assign;
    }, function (_tsrpcBaseClient) {
      BaseHttpClient = _tsrpcBaseClient.BaseHttpClient;
      defaultBaseHttpClientOptions = _tsrpcBaseClient.defaultBaseHttpClientOptions;
      BaseWsClient = _tsrpcBaseClient.BaseWsClient;
      defaultBaseWsClientOptions = _tsrpcBaseClient.defaultBaseWsClientOptions;
    }],
    execute: function () {
      /*!
       * TSRPC Browser v3.1.3
       * -----------------------------------------
       * Copyright (c) King Wang.
       * MIT License
       * https://github.com/k8w/tsrpc-browser
       */

      /**
       * @internal
       */
      HttpProxy =
      /** @class */
      function () {
        function HttpProxy() {}

        HttpProxy.prototype.fetch = function (options) {
          var _this = this;

          var rs;
          var promise = new Promise(function (_rs) {
            rs = _rs;
          });
          var xhr = new XMLHttpRequest();

          if (navigator.userAgent.indexOf('MSIE 8.0;') > -1) {
            //IE8 不支持onload onabort onerror事件
            xhr.onreadystatechange = function () {
              return __awaiter(_this, void 0, void 0, function () {
                return __generator(this, function (_a) {
                  if (xhr.readyState == 4) {
                    //Network Error
                    if (xhr.status == 0 || xhr.response == null && xhr.responseText == null) {
                      rs({
                        isSucc: false,
                        err: new TsrpcError('Network Error', {
                          type: TsrpcError.Type.NetworkError,
                          httpCode: xhr.status
                        })
                      });
                      return [2
                      /*return*/
                      ];
                    } //IE9 wrongURL 会返回12029


                    if (xhr.status == 12029) {
                      rs({
                        isSucc: false,
                        err: new TsrpcError({
                          message: 'Network Error',
                          type: TsrpcError.Type.NetworkError,
                          httpCode: xhr.status
                        })
                      });
                      return [2
                      /*return*/
                      ];
                    } // Res


                    rs({
                      isSucc: true,
                      res: options.responseType === 'text' ? xhr.responseText : new Uint8Array(xhr.response)
                    });
                  }

                  return [2
                  /*return*/
                  ];
                });
              });
            };
          } else {
            xhr.onerror = function () {
              rs({
                isSucc: false,
                err: new TsrpcError({
                  message: 'Network Error',
                  type: TsrpcError.Type.NetworkError,
                  httpCode: xhr.status
                })
              });
            }; // 有的平台 超时不触发onerror


            xhr.ontimeout = function () {
              rs({
                isSucc: false,
                err: new TsrpcError({
                  message: 'Request Timeout',
                  type: TsrpcError.Type.NetworkError,
                  code: 'TIMEOUT'
                })
              });
            }; // Res


            xhr.onload = function () {
              return __awaiter(_this, void 0, void 0, function () {
                return __generator(this, function (_a) {
                  if (xhr.status === 200) {
                    rs({
                      isSucc: true,
                      res: xhr.response && (options.responseType === 'text' ? xhr.responseText : new Uint8Array(xhr.response))
                    });
                  } else {
                    rs({
                      isSucc: false,
                      err: new TsrpcError({
                        message: 'HTTP Error ' + xhr.status,
                        type: TsrpcError.Type.ServerError,
                        httpCode: xhr.status
                      })
                    });
                  }

                  return [2
                  /*return*/
                  ];
                });
              });
            };

            var transportOptions_1 = options.transportOptions;

            if (!!transportOptions_1.onProgress) {
              xhr.upload.onprogress = function (e) {
                var _a;

                (_a = transportOptions_1.onProgress) === null || _a === void 0 ? void 0 : _a.call(transportOptions_1, e.loaded / e.total);
              };
            }
          }

          xhr.open(options.method, options.url, true);

          if (options.headers) {
            for (var key in options.headers) {
              xhr.setRequestHeader(key, options.headers[key]);
            }
          }

          xhr.responseType = options.responseType;
          var timeout = options.timeout;

          if (timeout) {
            xhr.timeout = timeout;
          }

          xhr.send(options.data);
          var abort = xhr.abort.bind(xhr);
          return {
            promise: promise,
            abort: abort
          };
        };

        return HttpProxy;
      }();
      /**
       * HTTP Client for TSRPC.
       * It uses XMLHttpRequest to send requests.
       * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
       */


      _export("HttpClient", HttpClient =
      /** @class */
      function (_super) {
        __extends(HttpClient, _super);

        function HttpClient(proto, options) {
          var _this = this;

          var httpProxy = new HttpProxy();
          _this = _super.call(this, proto, httpProxy, __assign(__assign({}, defaultHttpClientOptions), options)) || this;
          return _this;
        }

        HttpClient.prototype.callApi = function (apiName, req, options) {
          if (options === void 0) {
            options = {};
          }

          return _super.prototype.callApi.call(this, apiName, req, options);
        };

        HttpClient.prototype.sendMsg = function (msgName, msg, options) {
          if (options === void 0) {
            options = {};
          }

          return _super.prototype.sendMsg.call(this, msgName, msg, options);
        };

        return HttpClient;
      }(BaseHttpClient));

      defaultHttpClientOptions = __assign(__assign({}, defaultBaseHttpClientOptions), {
        customObjectIdClass: String
      });
      /**
       * @internal
       */

      WebSocketProxy =
      /** @class */
      function () {
        function WebSocketProxy() {}

        WebSocketProxy.prototype.connect = function (server) {
          var _this = this;

          this._ws = new WebSocket(server);
          this._ws.binaryType = 'arraybuffer';
          this._ws.onopen = this.options.onOpen;

          this._ws.onclose = function (e) {
            _this.options.onClose(e.code, e.reason);

            _this._ws = undefined;
          };

          this._ws.onmessage = function (e) {
            var _a;

            if (e.data instanceof ArrayBuffer) {
              _this.options.onMessage(new Uint8Array(e.data));
            } else if (typeof e.data === 'string') {
              _this.options.onMessage(e.data);
            } else {
              (_a = _this.options.logger) === null || _a === void 0 ? void 0 : _a.warn('[Unresolved Recv]', e.data);
            }
          };
        };

        WebSocketProxy.prototype.close = function (code, reason) {
          var _a;

          (_a = this._ws) === null || _a === void 0 ? void 0 : _a.close(code, reason);
          this._ws = undefined;
        };

        WebSocketProxy.prototype.send = function (data) {
          return __awaiter(this, void 0, void 0, function () {
            var sendData, buf;
            return __generator(this, function (_a) {
              try {
                sendData = void 0;

                if (typeof data === 'string') {
                  sendData = data;
                } else {
                  buf = data;

                  if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
                    sendData = buf.buffer;
                  } else {
                    sendData = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
                  }
                }

                this._ws.send(sendData);

                return [2
                /*return*/
                , {}];
              } catch (err) {
                return [2
                /*return*/
                , {
                  err: new TsrpcError('Network Error', {
                    code: 'SEND_BUF_ERR',
                    type: TsrpcError.Type.NetworkError,
                    innerErr: err
                  })
                }];
              }

              return [2
              /*return*/
              ];
            });
          });
        };

        return WebSocketProxy;
      }();
      /**
       * Client for TSRPC WebSocket Server.
       * @typeParam ServiceType - `ServiceType` from generated `proto.ts`
       */


      _export("WsClient", WsClient =
      /** @class */
      function (_super) {
        __extends(WsClient, _super);

        function WsClient(proto, options) {
          var _this = this;

          var wsp = new WebSocketProxy();
          _this = _super.call(this, proto, wsp, __assign(__assign({}, defaultWsClientOptions), options)) || this;
          return _this;
        }

        return WsClient;
      }(BaseWsClient));

      defaultWsClientOptions = __assign(__assign({}, defaultBaseWsClientOptions), {
        customObjectIdClass: String
      });
    }
  };
});
//# sourceMappingURL=index.js.map