import { AppError, ErrorType, isAppError } from './AppError'

export { AppError, ErrorType, isAppError }

export function getErrorMessage(error: unknown, fallback: string): string {
  if (isAppError(error)) {
    return error.getUserMessage()
  }

  if (error instanceof Error && error.message) {
    return error.message
  }

  return fallback
}

export function isFormValidationError(
  error: unknown,
): error is { errorFields: unknown[] } {
  return (
    typeof error === 'object' &&
    error !== null &&
    'errorFields' in error &&
    Array.isArray((error as { errorFields?: unknown[] }).errorFields)
  )
}
