import axios from 'axios';
import { toast } from 'vue-sonner';
import { h as useNuxtApp, b as useRuntimeConfig } from './server.mjs';

function useApi() {
  const { $i18n } = useNuxtApp();
  const config = useRuntimeConfig();
  const apiClient = axios.create({
    baseURL: config.public.apiBase,
    headers: {
      "Content-Type": "application/json"
    },
    timeout: 6e4
  });
  apiClient.interceptors.request.use(
    (config2) => {
      const locale = $i18n.locale.value;
      config2.headers["Content-Language"] = locale === "en" ? "en" : "ar";
      return config2;
    },
    (error) => {
      return Promise.reject(error);
    }
  );
  apiClient.interceptors.response.use(
    (response) => {
      return response;
    },
    (error) => {
      var _a, _b;
      if (((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status) === 500) {
        toast.error("Error", {
          description: ((_b = error.response.data) == null ? void 0 : _b.message) || "An unexpected error occurred",
          class: `!text-white rounded-lg !text-[16px]`,
          style: {
            background: "linear-gradient(90deg, #112231 0%, #191A2E 100%)"
          }
        });
      } else {
        console.error(error.message);
      }
      return Promise.reject(error);
    }
  );
  return {
    get: async (url, config2) => {
      const response = await apiClient.get(url, config2);
      return response;
    },
    post: async (url, data, config2) => {
      const response = await apiClient.post(url, data, config2);
      return response;
    },
    patch: async (url, data, config2) => {
      const response = await apiClient.patch(url, data, config2);
      return response;
    },
    put: async (url, data, config2) => {
      const response = await apiClient.put(url, data, config2);
      return response;
    },
    delete: async (url, config2) => {
      const response = await apiClient.delete(url, config2);
      return response;
    }
  };
}

export { useApi as u };
//# sourceMappingURL=useApi-Dv-cyvzn.mjs.map
