import { ShowGeneralErrorFn } from 'src/hooks/useGeneralError';

export type ErrorChecker = (error: unknown) => string | null; // Returns translation key if error matches, null to continue to next checker

/**
 * Check error against multiple checkers and show appropriate error message
 * Checkers are called in order until one returns a translation key or all fail
 */
export const checkAndShowError = (
  error: unknown,
  showGeneralError: ShowGeneralErrorFn,
  t: Function,
  checkers: ErrorChecker[],
  fallbackMessage?: string,
) => {
  for (const checker of checkers) {
    const translationKey = checker(error);
    if (translationKey) {
      showGeneralError(error, t(translationKey));
      return; // Error was processed
    }
  }

  if (fallbackMessage) {
    showGeneralError(error, t(fallbackMessage));
  } else {
    showGeneralError(error);
  }
};
