import axios from 'axios'
import { getApiBaseURL } from './tenant'
import { toFriendlyApiError } from '@shared/apiError'

export const api = axios.create({
  baseURL: getApiBaseURL(),
  withCredentials: true,
})

api.interceptors.response.use(
  (resp) => resp,
  (err) => {
    const fe = toFriendlyApiError(err)
    const wrapped = new Error(fe.detail || fe.summary)
    ;(wrapped as any).friendly = fe
    ;(wrapped as any).cause = err
    throw wrapped
  },
)
