import axios from 'axios'
import { useAuthStore } from '@/stores/auth-store'

const apiOrigin =
  (import.meta.env.VITE_API_BASE_URL as string | undefined)?.replace(
    /\/$/,
    ''
  ) || 'https://bogazici-api.test'

export const adminApi = axios.create({
  baseURL: `${apiOrigin}/api/admin`,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
})

adminApi.interceptors.request.use((config) => {
  const token = useAuthStore.getState().auth.accessToken

  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }

  return config
})

export { apiOrigin }
