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

  var _reporterNs, _cclegacy, Component, UIManager, HttpRequest, _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 _reportPossibleCrUseOfUIManager(extras) {
    _reporterNs.report("UIManager", "../UIManager", _context.meta, extras);
  }

  _export("HttpRequest", void 0);

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc) {
      _cclegacy = _cc.cclegacy;
      Component = _cc.Component;
    }, function (_unresolved_2) {
      UIManager = _unresolved_2.UIManager;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "dcd37qeW4JKa7oVrtL8bLgd", "HttpRequest", undefined);

      _export("HttpRequest", HttpRequest = class HttpRequest extends Component {
        constructor() {
          super(...arguments);

          _defineProperty(this, "baseUrl", "https://api.mytopcity.com");

          _defineProperty(this, "token", '');
        }

        static get instance() {
          if (!this._instance) {
            this._instance = new HttpRequest();
          }

          return this._instance;
        }
        /**
         * 设置用户token
         */


        setUserToken(token) {
          this.token = token;
        }
        /**
         * 
         * @param method 提交类型
         * @param action 方法
         * @param data 数据
         * @param showWait 
         * @returns 
         */


        request(method, action, data, showWait) {
          var _this = this;

          return _asyncToGenerator(function* () {
            if (showWait === void 0) {
              showWait = true;
            }

            if (!_this.token) {
              return false;
            }

            var p = new Promise((resolve, reject) => {
              var xhr = new XMLHttpRequest();
              xhr.timeout = 5000;

              xhr.ontimeout = () => {
                (_crd && UIManager === void 0 ? (_reportPossibleCrUseOfUIManager({
                  error: Error()
                }), UIManager) : UIManager).instance.showToast("链接超时");
                reject("timeout");
              };

              xhr.onabort = () => {
                reject("user abort");
              };

              xhr.onerror = () => {
                (_crd && UIManager === void 0 ? (_reportPossibleCrUseOfUIManager({
                  error: Error()
                }), UIManager) : UIManager).instance.showToast("网络连接失败");
                reject("network error");
              };

              xhr.onreadystatechange = () => {
                if (xhr.readyState == 4 && xhr.status >= 200 && xhr.status < 400) {
                  var response = xhr.responseText;
                  resolve(JSON.parse(response));
                }
              };

              if (method == "GET") {
                var dataStr = '';

                if (data) {
                  Object.keys(data).forEach(key => {
                    dataStr += key + '=' + encodeURIComponent(data[key]) + '&';
                  });

                  if (dataStr !== '') {
                    dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
                  }
                }

                xhr.open("GET", _this.baseUrl + action + '?' + dataStr, true);
                xhr.setRequestHeader('Authorization', 'Bearer ' + _this.token);
                xhr.send();
              } else if (method == "POST") {
                xhr.open("POST", _this.baseUrl + action, true);
                xhr.setRequestHeader('Authorization', 'Bearer ' + _this.token);
                xhr.send(data);
              }
            });
            return p;
          })();
        }

      });

      _defineProperty(HttpRequest, "_instance", null);

      _cclegacy._RF.pop();

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