import { getLocale } from "@arcblock/ux/lib/Locale/context";
import { createAxios } from "@blocklet/js-sdk";
import isNull from "lodash/isNull";
import { getPrefix } from "./util.js";
const api = createAxios();
api.interceptors.request.use(
  (config) => {
    const prefix = getPrefix();
    config.baseURL = prefix || "";
    const locale = getLocale(window.blocklet?.languages);
    const query = new URLSearchParams(config.url?.split("?").pop());
    config.params = { ...config.params || {}, locale };
    const authToken = window.__PAYMENT_KIT_AUTH_TOKEN;
    if (authToken && typeof config.params.authToken === "undefined" && !query.has("authToken")) {
      config.params.authToken = authToken;
    }
    if (typeof config.params.livemode === "undefined" && query.has("livemode") === false) {
      const livemode = localStorage.getItem("livemode");
      config.params.livemode = isNull(livemode) ? true : JSON.parse(livemode);
    }
    return config;
  },
  (err) => Promise.reject(err)
);
export default api;
