'use strict';

var axios = require('axios');
var elementPlus = require('element-plus');
var Cookies = require('js-cookie');
var index = require('../../../hooks/store/useLogin/index.js');
var objects = require('../objects.js');
var interceptors = require('./interceptors.js');
var errorMessage = require('./errorMessage.js');
var url = require('./url.js');
var index$1 = require('../../../hooks/base/useGlobalConfig/index.js');

function createAxios(config, options) {
  const successCode = config.successCode ?? 0;
  const hideAuthorization = config.hideAuthorization ?? false;
  const isUseParamsToken = config.isUseParamsToken ?? false;
  const service = axios.create({
    withCredentials: true,
    // send cookies when cross-domain requests
    timeout: 12e4,
    headers: {
      "content-type": "application/json",
      Accept: "*/*"
    },
    ...config
  });
  service.interceptors.request.use(
    async (config2) => {
      const logingStore = index.useLoginStore();
      const _token = Cookies.get("token") ?? logingStore.userToken;
      const _user = Cookies.get("sso_user_id") ?? logingStore.userId;
      const loginEnvKey = "VITE_GIIME_LOGIN_GK_SERVICE_USER";
      const importMetaEnv = index$1.useGlobalConfig("env");
      const gkServiceUser = url.getHeaderValue(config2.headers, "gk-service-user");
      if (!gkServiceUser) {
        config2.headers["gk-service-user"] = importMetaEnv.value[loginEnvKey] || void 0;
      }
      if (!config2.params) {
        config2.params = {};
      }
      if (!hideAuthorization) {
        if (isUseParamsToken) {
          config2.params._token = _token;
          config2.params._user = _user;
        }
        if (_token) {
          config2.headers.Authorization = `Bearer ${_token}`;
        }
      }
      await options?.requestInterceptors?.(config2);
      return config2;
    },
    (error) => {
      console.error("\u8BF7\u6C42\u9519\u8BEF", error);
      return Promise.reject(error);
    }
  );
  service.interceptors.response.use(
    async (response) => {
      const res = response.data;
      const msg = (res?.comment ?? res?.message ?? res?.errMsg ?? res?.msg) || "\u63A5\u53E3\u8BF7\u6C42\u9519\u8BEF";
      if (!res || !objects.hasOwn(res, "code")) {
        return response;
      } else if (res.code === successCode || res.errCode === successCode) {
        return response;
      } else if (Array.isArray(successCode) && (successCode.includes(res.code) || successCode.includes(res.errCode))) {
        return response;
      } else if (res.code === 401) {
        const res2 = await interceptors.res401Interceptors(service, response.config);
        return res2;
      }
      await options?.responseInterceptors?.(response);
      elementPlus.ElMessage({
        message: msg,
        type: "error",
        grouping: true,
        duration: 5 * 1e3,
        showClose: true
      });
      return Promise.reject(response);
    },
    async (err) => {
      console.error(err);
      if (err?.response?.status === 401 && !err.message.includes("timeout")) {
        const res = await interceptors.res401Interceptors(service, err.config);
        return res;
      } else {
        errorMessage.responseErrorMessage(err);
        return Promise.reject(err);
      }
    }
  );
  return {
    service
  };
}

exports.createAxios = createAxios;
//# sourceMappingURL=createAxios.js.map
