// utils/api.ts
import axios from 'axios';
import type { AxiosInstance } from 'axios';

// Create a new Axios instance with custom configuration
const api: AxiosInstance = axios.create({
  baseURL: 'https://housiey-be.siplsolutions.com/', // Replace with your API URL
  timeout: 5000, // Set a timeout for requests
});

// Interceptors to attach the authentication token
api.interceptors.request.use((config: any) => {
  // Get the authentication token from where it's stored (e.g., localStorage or Redux store)
  const authToken = localStorage.getItem('token');

  // If the token exists, attach it to the request headers
  if (authToken !== null) {
    config.headers = {
      ...config.headers,
      Authorization: `Bearer ${authToken}`,
    };
  }

  return config;
});

export default api;
