import _typeof from '@babel/runtime/helpers/typeof';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';

/** 存储登录态的默认 key */
var COCOS_LOGIN_INFO_STORAGE_KEY = 'cocos_login_info';
/** 给 url 追加 query 参数（自动判断 ?） */
function addUrlParam(url, key, value) {
  if (!key) return url;
  var sep = url.includes('?') ? '&' : '?';
  return "".concat(url).concat(sep).concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
}
/**
 * Cocos 端登录态参数拦截器
 *
 * 规则：
 *   - 公共 query：tstamp（每次都带，防缓存）
 *   - 有登录态 → body.login_info = storage.get(...)，url 不再拼登录相关 query
 *   - 无登录态 → url 追加 appid / platform / _ltype，并调 getLoginCode() 拿 wx.code 塞进 url 的 code query
 *
 * 幂等：每次基于 CocosHostInterceptor 产出的 `param.url` 拼 query，
 * decorator 重试时 cocos-host 会用 __cocosOriginUrl 重拼 URL，
 * 所以本拦截器不会累加 query。
 *
 * 参考 pmd-npm/packages/business/src/network/interceptor/request/cocos/cocosLoginInfoParamInterceptor.ts
 */
var CocosLoginInfoParamInterceptor = /*#__PURE__*/function () {
  function CocosLoginInfoParamInterceptor(options) {
    _classCallCheck(this, CocosLoginInfoParamInterceptor);
    this.options = options;
  }
  return _createClass(CocosLoginInfoParamInterceptor, [{
    key: "interceptor",
    value: function interceptor(param) {
      var _a, _b, _c;
      var _this$options = this.options,
        storage = _this$options.storage,
        _this$options$storage = _this$options.storageKey,
        storageKey = _this$options$storage === void 0 ? COCOS_LOGIN_INFO_STORAGE_KEY : _this$options$storage;
      var url = param.url;
      // tstamp 始终带（防缓存）
      url = addUrlParam(url, 'tstamp', String(Date.now()));
      param.header = Object.assign(Object.assign({}, param.header), {
        Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json;charset=UTF-8;',
        'tip-channel': 'wxproc'
      });
      // 确保 reqData 是对象，方便塞 login_info
      if (!param.reqData || _typeof(param.reqData) !== 'object') {
        param.reqData = {};
      }
      var loginInfo = (_a = storage === null || storage === void 0 ? void 0 : storage.get) === null || _a === void 0 ? void 0 : _a.call(storage, storageKey);
      if (loginInfo) {
        url = addUrlParam(url, 'g_app_tk', loginInfo.g_tk);
        var sessionKey = ((_c = (_b = this.options) === null || _b === void 0 ? void 0 : _b.isTestEnv) === null || _c === void 0 ? void 0 : _c.call(_b)) ? 'tiplogin_sessionid_test' : 'tiplogin_sessionid';
        var cookie = "".concat(sessionKey, "=").concat(loginInfo.sessionId, ";tiplogin_openid=").concat(loginInfo.ouid, ";tip_token=").concat(loginInfo.g_tk, ";tiplogin_headurl=").concat(loginInfo.head, ";tiplogin_nick=").concat(loginInfo.nick, ";");
        param.header = Object.assign(Object.assign({}, param.header), {
          Cookie: cookie,
          cookie: cookie
        });
        // 有登录态：仅通过 body.login_info 透传，url 不再拼登录相关 query
        // param.reqData.login_info = loginInfo;
      }
      param.url = url;
      return Promise.resolve([false, param]);
    }
  }]);
}();

export { COCOS_LOGIN_INFO_STORAGE_KEY, CocosLoginInfoParamInterceptor };
