import type { I18n, MessageDescriptor } from "@lingui/core";
import { msg } from "@lingui/core/macro";
import { ErrorType } from "@/lib/tanstack-query/types";

// Mirrors the fixed userMessage strings mapKnownError produces
// (src/lib/tanstack-query/utils/to-server-result.ts) so they can be localized.
export const errorMessages: Partial<Record<ErrorType, MessageDescriptor>> = {
  [ErrorType.VALIDATION]: msg`Invalid request`,
  [ErrorType.NOT_FOUND]: msg`The requested resource was not found`,
  [ErrorType.UNAUTHORIZED]: msg`You are not authorized to perform this action`,
  [ErrorType.FORBIDDEN]: msg`Access denied`,
  [ErrorType.NETWORK]: msg`Network error. Please check your connection and try again.`,
  [ErrorType.POCKETBASE]: msg`Something went wrong. Please try again.`,
  [ErrorType.UNKNOWN]: msg`An unexpected error occurred. Please try again.`,
};

export function localizeServerError(
  i18n: I18n,
  error: { code: ErrorType; userMessage: string }
): string {
  const descriptor = errorMessages[error.code];
  return descriptor ? i18n._(descriptor) : error.userMessage;
}
