import { ApiError } from '@/api'
import { formatValidationDetail } from '@/utils/validationErrorFormat'

/** Turn FastAPI error bodies into a human-readable string. */
export function formatApiError(e: unknown, fallback: string): string {
  if (e instanceof ApiError) {
    const d = (e as ApiError & { detail?: unknown }).detail ?? e.message
    return formatValidationDetail(d, e.message || fallback)
  }
  if (e instanceof Error) {
    const msg = e.message.trim()
    return msg || fallback
  }
  return fallback
}
