import axios, { InternalAxiosRequestConfig } from "axios";
import { getRouteLang } from "./getLang";

const baseUrl = process.env.NEXT_PUBLIC_BASE_URL;

const httpClient = axios.create({ 
  baseURL: baseUrl,
  headers: {
    'Content-Type': 'application/json',
  }
});

// Request interceptor
httpClient.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const language = getRouteLang();
    config.headers["Accept-Language"] = language;
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// Response interceptor (xatoliklarni boshqarish uchun)
httpClient.interceptors.response.use(
  (response) => response,
  (error) => {
    // Xatolikni formatlash
    const message = error.response?.data?.message || error.message;
    return Promise.reject(message);
  }
);

export default httpClient;