import { globalEBus } from '../../../e-bus/init.mjs';
import { NEED_LOGIN_RET } from '../config.mjs';
import '../../../e-bus/e-bus.mjs';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

function cocosLoginDecorator(request, param) {
  var needReRequest = false;
  return new Promise(function (resolve, reject) {
    globalEBus === null || globalEBus === void 0 ? void 0 : globalEBus.on('event-on-cocos-login', function () {
      if (needReRequest) {
        needReRequest = false;
        request().then(function (res) {
          resolve(res);
        })["catch"](function (err) {
          reject(err);
        });
      }
    });
    request().then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      var _a, _b;
      if (+(err === null || err === void 0 ? void 0 : err.r) === NEED_LOGIN_RET || ((_a = param === null || param === void 0 ? void 0 : param.extra) === null || _a === void 0 ? void 0 : _a.remainData) && +((_b = err === null || err === void 0 ? void 0 : err.data) === null || _b === void 0 ? void 0 : _b.r) === NEED_LOGIN_RET) {
        needReRequest = true;
      } else {
        reject(err);
      }
    });
  });
}

export { cocosLoginDecorator };
