/**
 * Turn whatever a fetch/mutation threw into a user-readable string.
 *
 * openapi-fetch rethrows the parsed response body as-is (a plain object like
 * `{ error, message, path, status, timestamp }` for Spring errors), so
 * `String(e)` renders "[object Object]" in a toast. This helper prefers an
 * Error.message, falls back to a `message` field on plain objects, then
 * Spring's `error` field, and finally JSON-stringifies the rest.
 */
export function describeApiError(e: unknown): string {
  if (e instanceof Error) return e.message;
  if (typeof e === 'string') return e;
  if (typeof e === 'object' && e !== null) {
    const obj = e as Record<string, unknown>;
    if (typeof obj.message === 'string' && obj.message) return obj.message;
    if (typeof obj.error === 'string' && obj.error) return obj.error;
    try {
      return JSON.stringify(e);
    } catch {
      return String(e);
    }
  }
  return String(e);
}
