type AnyError = {
  message?: string;
  error?: { message?: string; statusText?: string };
};

export function formatErrorMessage(
  error: unknown,
  fallback = "Something went wrong",
): string {
  if (!error) return fallback;
  if (typeof error === "string") return error;

  const e = error as AnyError;

  if (e.error && typeof e.error === "object") {
    const inner = e.error.message?.trim() || e.error.statusText?.trim();
    if (inner) return inner;
  }

  if (typeof e.message === "string" && e.message.trim()) {
    return e.message.trim();
  }

  return fallback;
}
