import "../../node_modules/axios/index.mjs";
import axios$1 from "../../node_modules/axios/lib/axios.mjs";
const axios = () => {
  const service = axios$1.create({
    withCredentials: false
  });
  const pendingMap = /* @__PURE__ */ new Map();
  service.interceptors.request.use(
    (config) => {
      removePending(config);
      addPending(config);
      if (config.url != "/login") {
        config.headers.token = sessionStorage.getItem("token");
        config.headers.refreshToken = sessionStorage.getItem("refreshToken");
      }
      config.headers.crypto = false;
      config.headers.sig = false;
      return config;
    },
    (error) => {
      Promise.reject(error);
    }
  );
  service.interceptors.response.use(
    (response) => {
      removePending(response.config);
      if (response.config.responseType == "blob") {
        return response;
      }
      if (response.data && response.data.code == "500") {
        return Promise.reject(response.data);
      }
      if (response.data.message) ;
      let responseData = response.data;
      return responseData;
    },
    async (error) => {
      error.config && removePending(error.config);
      if (axios$1.isCancel(error)) {
        return console.error("请求的重复请求：" + error.message);
      }
      if (error.response.config.responseType == "blob") {
        let reader = new FileReader();
        reader.onload = function(event) {
          let data = JSON.parse(reader.result);
          showErrorMessage(data.message, data.messageType);
          return Promise.reject(error);
        };
        reader.readAsText(error.response.data);
      } else {
        return Promise.reject(error);
      }
    }
  );
  function addPending(config) {
    const pendingKey = getPendingKey(config);
    config.cancelToken = config.cancelToken || new axios$1.CancelToken((cancel) => {
      if (!pendingMap.has(pendingKey)) {
        pendingMap.set(pendingKey, cancel);
      }
    });
  }
  function removePending(config) {
    const pendingKey = getPendingKey(config);
    if (pendingMap.has(pendingKey)) {
      const cancelToken = pendingMap.get(pendingKey);
      cancelToken(pendingKey);
      pendingMap.delete(pendingKey);
    }
  }
  function getPendingKey(config) {
    let { url, method, params, data } = config;
    if (typeof data === "string") data = JSON.parse(data);
    return [url, method, JSON.stringify(params), JSON.stringify(data)].join(
      "&"
    );
  }
  return {
    request(config) {
      return service(config);
    },
    get(url, config) {
      return service.get(url, config);
    },
    delete(url, config) {
      return service.delete(url, config);
    },
    head(url, config) {
      return service.head(url, config);
    },
    post(url, data, config) {
      return service.post(url, data, config);
    },
    postForm(url, data, config) {
      return service.postForm(url, data, config);
    },
    put(url, data, config) {
      return service.put(url, data, config);
    },
    upload(url, data, config) {
      return service.postForm(url, data, config);
    },
    interceptors: {
      request: {
        use(fnHandle, fnError) {
          return service.interceptors.request.use(fnHandle, fnError);
        },
        eject(id) {
          return service.interceptors.request.eject(id);
        }
      },
      response: {
        use(fnHandle, fnError) {
          return service.interceptors.response.use(fnHandle, fnError);
        },
        eject(id) {
          return service.interceptors.response.eject(id);
        }
      }
    }
  };
};
export {
  axios as default
};
