"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getJwtToken = exports["default"] = void 0;
exports.setToken = setToken;

var _axios = _interopRequireDefault(require("axios"));

var _window$BSGlobal$tena, _window$BSGlobal$logi;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var requestDomain = function requestDomain() {
  if (!window.location.hostname.match(/dev/)) return '';
  return "".concat(window.location.protocol, "//www.tita.gift");
};

var tenantId = window.BSGlobal && ((_window$BSGlobal$tena = window.BSGlobal.tenantInfo) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Id) || 0;
var userId = window.BSGlobal && ((_window$BSGlobal$logi = window.BSGlobal.loginUserInfo) === null || _window$BSGlobal$logi === void 0 ? void 0 : _window$BSGlobal$logi.Id) || 0;

function getToken() {
  return sessionStorage.getItem('DEMOFREE_TOKEN') || '';
}

function clearToken() {
  sessionStorage.removeItem('DEMOFREE_TOKEN');
}

function setToken(token) {
  sessionStorage.setItem('DEMOFREE_TOKEN', token);
}

var getJwtToken = function getJwtToken() {
  return _axios["default"].get("".concat(requestDomain(), "/api/v1/").concat(tenantId, "/").concat(userId, "/const/skynetToken")).then(function (res) {
    if (res.data.Data) {
      setToken(JSON.parse(res.data.Data.obj).token);
      return JSON.parse(res.data.Data.obj).token;
    }

    return '';
  });
};

exports.getJwtToken = getJwtToken;

var instance = _axios["default"].create({
  timeout: 10 * 1000
});

var beforeRequest = function beforeRequest(config) {
  var copyConfig = _objectSpread({}, config);

  copyConfig.headers.authorization = "Bearer ".concat(getToken());
  return copyConfig;
};

var beforeResponse = function beforeResponse(res) {
  var config = res.config;
  config.retry = 3;
  config.retryCount = config.retryCount || 0;

  if (res.data.code === 401) {
    // 401错误表示未登录授权或者token失效
    clearToken();

    if (config.retryCount <= config.retry) {
      config.retryCount += 1;
      return getJwtToken().then(function () {
        return instance.request(res.config);
      });
    }

    return Promise.reject(res);
  }

  return res.data;
};

var requestError = function requestError(error) {
  return Promise.reject(error);
};

instance.interceptors.request.use(beforeRequest, requestError);
instance.interceptors.response.use(beforeResponse, function (error) {
  throw new Error(error);
});
var _default = instance;
exports["default"] = _default;