import axios from 'axios';
import { ElMessage } from 'element-plus';
import Cookies from 'js-cookie';
import { useLoginStore } from '../../../hooks/store/useLogin/index.mjs';
import { hasOwn } from '../objects.mjs';
import { res401Interceptors } from './interceptors.mjs';
import { responseErrorMessage } from './errorMessage.mjs';
import { getHeaderValue } from './url.mjs';
import { useGlobalConfig } from '../../../hooks/base/useGlobalConfig/index.mjs';

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 = 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 = useGlobalConfig("env");
      const gkServiceUser = 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 || !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 res401Interceptors(service, response.config);
        return res2;
      }
      await options?.responseInterceptors?.(response);
      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 res401Interceptors(service, err.config);
        return res;
      } else {
        responseErrorMessage(err);
        return Promise.reject(err);
      }
    }
  );
  return {
    service
  };
}

export { createAxios };
//# sourceMappingURL=createAxios.mjs.map
