import axios from 'axios'

export function getErrorMessage(err: unknown, fallback = 'Lỗi hệ thống'): string {
  if (axios.isAxiosError(err)) {
    const data = err.response?.data as { detail?: string; title?: string; errors?: Record<string, string[]> } | undefined
    if (data?.errors) {
      const first = Object.values(data.errors).flat()[0]
      if (first) return first
    }
    return data?.detail ?? data?.title ?? err.message
  }
  return fallback
}
