import { globalEBus } from '../../e-bus/init.mjs';
import { addUrlParams } from '../../url/helper.mjs';
import '../../e-bus/e-bus.mjs';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/slicedToArray';

function getLoginCode() {
  var g = typeof globalThis !== 'undefined' ? globalThis : {};
  var wxObj = g.wx;
  if (!wxObj || typeof wxObj.login !== 'function') {
    return Promise.resolve({
      code: ''
    });
  }
  return new Promise(function (resolve, reject) {
    wxObj.login({
      success: function success(r) {
        return resolve({
          code: (r === null || r === void 0 ? void 0 : r.code) || ''
        });
      },
      fail: reject
    });
  });
}
function loginMp(_ref) {
  var url = _ref.url,
    _ref$method = _ref.method,
    method = _ref$method === void 0 ? 'POST' : _ref$method,
    _ref$header = _ref.header,
    header = _ref$header === void 0 ? '' : _ref$header,
    _ref$body = _ref.body,
    body = _ref$body === void 0 ? {} : _ref$body,
    _ref$appid = _ref.appid,
    appid = _ref$appid === void 0 ? '' : _ref$appid,
    _ref$_ltype = _ref._ltype,
    _ltype = _ref$_ltype === void 0 ? 'tiploginwxproc' : _ref$_ltype,
    _ref$storageKey = _ref.storageKey,
    storageKey = _ref$storageKey === void 0 ? 'cocos_login_info' : _ref$storageKey,
    onLoginInfo = _ref.onLoginInfo,
    storage = _ref.storage;
  return new Promise(function (resolve, reject) {
    getLoginCode().then(function (loginCode) {
      var effectiveUrl = addUrlParams(url, {
        appid: appid,
        _ltype: _ltype,
        code: loginCode.code
      });
      wx.request({
        url: effectiveUrl,
        method: method,
        header: header,
        data: body,
        dataType: 'json',
        success: function success(res) {
          var _a, _b;
          var loginInfo = (_a = res.header) === null || _a === void 0 ? void 0 : _a.logininfo;
          console.log('loginInfo', loginInfo);
          if (loginInfo) {
            var loginInfoObj = {};
            try {
              loginInfoObj = JSON.parse(loginInfo);
            } catch (err) {}
            console.log('loginInfoObj', loginInfoObj);
            if (loginInfoObj === null || loginInfoObj === void 0 ? void 0 : loginInfoObj.sessionId) {
              globalEBus === null || globalEBus === void 0 ? void 0 : globalEBus.emit('event-on-cocos-login');
              onLoginInfo === null || onLoginInfo === void 0 ? void 0 : onLoginInfo(loginInfoObj);
              (_b = storage === null || storage === void 0 ? void 0 : storage.set) === null || _b === void 0 ? void 0 : _b.call(storage, storageKey, loginInfo);
              resolve();
              return;
            }
          }
          resolve();
        },
        fail: function fail(err) {
          reject(new Error((err === null || err === void 0 ? void 0 : err.errMsg) || 'wx.request failed'));
        }
      });
    });
  });
}

export { getLoginCode, loginMp };
