'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var eBus_init = require('../../../e-bus/init.js');
var cocos_network_config = require('../config.js');
require('../../../e-bus/e-bus.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');

function cocosLoginDecorator(request, param) {
  var needReRequest = false;
  return new Promise(function (resolve, reject) {
    eBus_init.globalEBus === null || eBus_init.globalEBus === void 0 ? void 0 : eBus_init.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) === cocos_network_config.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) === cocos_network_config.NEED_LOGIN_RET) {
        needReRequest = true;
      } else {
        reject(err);
      }
    });
  });
}

exports.cocosLoginDecorator = cocosLoginDecorator;
