import { serialize } from '@/util/util';
import { getStore } from '../util/store';
import NProgress from 'nprogress'; // progress bar
import errorCode from '@/const/errorCode';
import router from '@/router/router';
import { Message } from 'element-ui';
import 'nprogress/nprogress.css';
import qs from 'qs';
import store from '@/store'; // progress bar style
axios.defaults.timeout = 3000000;
// 返回其他状态吗
axios.defaults.validateStatus = function (status) {
  return status >= 200 && status <= 500; // 默认的
};
// 跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
// NProgress Configuration
NProgress.configure({
  showSpinner: false
});

// HTTPrequest拦截
axios.interceptors.request.use(
  (config) => {
    NProgress.start(); // start progress bar
    const TENANT_ID = getStore({ name: 'tenantId' });
    const isToken = (config.headers || {}).isToken === false;
    const token = store.getters.access_token;
    if (token && !isToken) {
      config.headers['Authorization'] = 'Bearer ' + token; // token
    }
    if (TENANT_ID) {
      config.headers['TENANT-ID'] = TENANT_ID; // 租户ID
    }

    // headers中配置serialize为true开启序列化
    if (config.method === 'post' && config.headers.serialize) {
      config.data = serialize(config.data);
      delete config.data.serialize;
    }

    if (config.method === 'get') {
      config.paramsSerializer = function (params) {
        return qs.stringify(params, { arrayFormat: 'repeat' });
      };
    }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// HTTPresponse拦截
axios.interceptors.response.use(
  (res) => {
    NProgress.done();
    const status = Number(res.status) || 200;
    const message = res.data.msg || errorCode[status] || errorCode['default'];
    if (status === 401) {
      Message({
        message: message,
        type: 'error'
      });
      store.dispatch('FedLogOut').then(() => {
        const specialPath = store.getters.specialPath;
        const pathQuery = { path: '/login' };
        if (specialPath !== 'default') pathQuery.query = { s: specialPath };
        router.push(pathQuery);
      });
      return;
    }
    if (res.status == 500 && res.data && res.config.responseType === 'arraybuffer') {
      try {
        // 使用 TextDecoder 将 ArrayBuffer 转换为字符串
        const decoder = new TextDecoder('utf-8');
        const message = decoder.decode(res.data);
        const obj = JSON.parse(message);

        // 可以选择抛出一个新的错误，或者返回一个带有错误信息的 Promise
        Message({
          message: obj.msg,
          type: 'error'
        });
        return Promise.reject(new Error(obj.msg));
      } catch (e) {
        console.error('解码错误信息时出错:', e);
        return Promise.reject(e);
      }
    }

    if (status !== 200 || res.data.code === 1) {
      console.log('qaqcc');

      Message({
        message: message,
        type: 'error'
      });
      return Promise.reject(new Error(message));
    }

    return res;
  },
  (error) => {
    NProgress.done();
    return Promise.reject(new Error(error));
  }
);

export default axios;
