import { HttpError, InUseError } from "./errors";

/** Maps a caught mutation error to an i18n key (+ interpolation opts). The single
 *  source of truth shared by the global toast fallback and every inline display. */
export function errorMessageKey(error: unknown): { key: string; opts?: Record<string, unknown> } {
  if (error instanceof InUseError) return { key: "actions.inUse", opts: { count: error.count } };
  if (error instanceof HttpError) return { key: statusKey(error.status) };
  return { key: "toast.error" };
}

function statusKey(status: number): string {
  if (status === 403) return "errors.forbidden";
  if (status === 404) return "errors.notFound";
  if (status === 409) return "errors.conflict";
  if (status === 422) return "errors.validation";
  if (status >= 500) return "errors.server";
  return "toast.error";
}
