import { copyForCode } from './error-copy';

/**
 * Returns the `code` field from an unknown error value, or 'unknown'
 * when the value isn't shaped like { code: string }.
 */
export function errCode(err: unknown): string {
  return (err as { code?: string })?.code ?? 'unknown';
}

/**
 * Returns user-facing copy for an unknown error value. Looks up the
 * error's code in the error-copy map; falls back to the supplied
 * fallback (default: "Something went wrong.") when the code is unknown.
 */
export function errMessage(err: unknown, fallback = 'Something went wrong.'): string {
  return copyForCode(errCode(err)) ?? fallback;
}
