/**
 * `@platform-modules/util` — public seam.
 *
 * Export the module's contract here. Be lazy inside (coding-standard §2 ladder),
 * strict at every export (§4 checklist). One behavioral test per export, co-located.
 */

export interface ErrorCopy {
  title: string
  message: string
}

/**
 * Host-overridable copy for the common HTTP error pages. Pure data — no status→render wiring
 * (that is a thin host template over `ErrorState`), no `noindex`/`Retry-After` (host headers).
 * 500 is the universal fallback so {@link errorCopy} always resolves.
 */
export const DEFAULT_ERROR_COPY: Readonly<Record<number, ErrorCopy>> = {
  400: { title: 'Bad request', message: 'The request could not be understood. Check the address and try again.' },
  401: { title: 'Sign in required', message: 'You need to sign in to view this page.' },
  403: { title: 'Access denied', message: 'You do not have permission to view this page.' },
  404: { title: 'Page not found', message: 'The page you are looking for does not exist or has moved.' },
  429: { title: 'Too many requests', message: 'You have made too many requests. Please wait a moment and try again.' },
  500: { title: 'Something went wrong', message: 'An unexpected error occurred on our end. Please try again later.' },
  503: { title: 'Temporarily unavailable', message: 'The site is down for maintenance. Please check back shortly.' },
}

/**
 * Resolve copy for an HTTP status code: a per-code override wins, else the default for that code,
 * else the 500 copy. ALWAYS resolves — an unknown code never yields `undefined` (the page never
 * renders blank).
 */
export function errorCopy(code: number, overrides?: Record<number, ErrorCopy>): ErrorCopy {
  return overrides?.[code] ?? DEFAULT_ERROR_COPY[code] ?? DEFAULT_ERROR_COPY[500]!
}
