'use strict';

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

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var cocos_network_config = require('../../config.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);

/**
 * 归一化响应码/文案拦截器
 *
 * - ret / r 统一为 r
 * - err_desc / err_msg 统一成 msg（URL decode + 去 +）
 * - r=100000（NeedLogin）清空 msg（由 decorator 负责重试，不 toast）
 *
 * 参考 pmd-npm/packages/business/src/network/interceptor/response/common/normalizeResponseRetInterceptor.ts
 */
var NormalizeResponseRetInterceptor = /*#__PURE__*/function () {
  function NormalizeResponseRetInterceptor(options) {
    _classCallCheck__default["default"](this, NormalizeResponseRetInterceptor);
    this.options = options;
  }
  return _createClass__default["default"](NormalizeResponseRetInterceptor, [{
    key: "interceptor",
    value: function interceptor(param) {
      var _a, _b, _c;
      var data = (_a = param.response) === null || _a === void 0 ? void 0 : _a.data;
      if (data && data.r === undefined) {
        if (data.ret === undefined || data.ret === null) {
          data.r = 0;
        } else {
          data.r = data.ret;
        }
      }
      // err_desc / err_msg 归一成 msg
      if (data && !data.msg && data.err_desc && data.err_desc !== '') {
        data.err_desc = String(data.err_desc).replace(/\+/g, ' ');
        try {
          data.msg = decodeURIComponent(data.err_desc);
        } catch (_d) {
          data.msg = data.err_desc;
        }
      } else if (data && !data.msg && data.err_msg && data.err_msg !== '') {
        try {
          data.msg = decodeURIComponent(data.err_msg);
        } catch (_e) {
          data.msg = data.err_msg;
        }
      } else if (data === null || data === void 0 ? void 0 : data.msg) ; else if (data && +data.r !== 0) {
        data.msg = "\u8BF7\u6C42\u5F02\u5E38\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\uFF08#".concat(data.r, "\uFF09");
      }
      // 需要登录的错误，去掉 toast 文案（由 decorator 处理重试）
      if (data && +data.r === cocos_network_config.NEED_LOGIN_RET) {
        data.msg = '';
        (_c = (_b = this.options) === null || _b === void 0 ? void 0 : _b.loginMp) === null || _c === void 0 ? void 0 : _c.call(_b);
      }
      return [false, param];
    }
  }]);
}();

exports.NormalizeResponseRetInterceptor = NormalizeResponseRetInterceptor;
