'use strict';

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

var _axios = require('axios');

var _axios2 = _interopRequireDefault(_axios);

var _authorize = require('../../common/authorize');

var _accessToken = require('../../common/accessToken');

var _constants = require('../../common/constants');

var _sessionExpiredLogin = require('../../common/sessionExpiredLogin');

var _sessionExpiredLogin2 = _interopRequireDefault(_sessionExpiredLogin);

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

var MODULE_SERVER_LINK_MAP = {};
var AXIOS_INSTANCE_MAP = {};
var SINGLE_APP_SERVER = 'SINGLE_APP_SERVER';

function getServerByModule(moduleName) {
  var keys = Object.keys(MODULE_SERVER_LINK_MAP);
  var server = SINGLE_APP_SERVER;
  for (var i = 0; i < keys.length; i += 1) {
    if (MODULE_SERVER_LINK_MAP[keys[i]].includes(moduleName)) {
      server = keys[i];
      break;
    }
  }
  return server;
}

function createAxiosInsByModuleNmae(moduleName) {
  if (AXIOS_INSTANCE_MAP[moduleName]) {
    return AXIOS_INSTANCE_MAP[moduleName];
  }

  var server = getServerByModule(moduleName);
  if (server === SINGLE_APP_SERVER) {
    if (AXIOS_INSTANCE_MAP[SINGLE_APP_SERVER]) {
      return AXIOS_INSTANCE_MAP[SINGLE_APP_SERVER];
    }
  }

  var instance = _axios2['default'].create({
    timeout: 60000,
    baseURL: _constants.API_HOST
  });

  var CSRF_TOKEN = void 0;

  // axios.defaults.baseURL = API_HOST;

  instance.interceptors.request.use(function (config) {
    var newConfig = config;
    newConfig.headers['Content-Type'] = 'application/json';
    newConfig.headers['X-Requested-With'] = 'XMLHttpRequest';
    newConfig.headers.Accept = 'application/json';
    if (CSRF_TOKEN) {
      newConfig.headers['x-csrf-token'] = CSRF_TOKEN;
    }
    var accessToken = (0, _accessToken.getAccessToken)();
    if (accessToken) {
      newConfig.headers.Authorization = accessToken;
    }
    return newConfig;
  }, function (err) {
    var error = err;
    return Promise.reject(error);
  });

  instance.interceptors.response.use(function (response) {
    var status = response.status,
        headers = response.headers;

    if (!CSRF_TOKEN && headers['x-csrf-token']) {
      CSRF_TOKEN = headers['x-csrf-token'];
    }
    if (status === 200 && response.request.responseURL && response.request.responseURL.endsWith('login')) {
      (0, _authorize.authorize)();
      return Promise.reject(new Error('need login'));
    }
    if (status === 200 && response.data && !response.data.success && response.data.code === 'sys_session_timeout') {
      // 弹窗警告登录失效
      (0, _sessionExpiredLogin2['default'])();
    }
    if (status === 204) {
      return response;
    }
    return response.data;
  }, function (error) {
    var response = error.response;

    if (response) {
      var status = response.status;

      switch (status) {
        case 401:
          {
            (0, _accessToken.removeAccessToken)();
            (0, _authorize.authorize)();
            break;
          }
        default:
          break;
      }
    }
    throw error;
  });

  AXIOS_INSTANCE_MAP[moduleName] = instance;
  if (server === SINGLE_APP_SERVER) {
    AXIOS_INSTANCE_MAP[SINGLE_APP_SERVER] = instance;
  }
  return instance;
}

exports['default'] = createAxiosInsByModuleNmae;