import { ApiError, AuthExpiredError } from '@/shared/api/httpClient';

export const DEFAULT_ERROR_MESSAGE = 'Request failed';
export const AUTH_EXPIRED_ERROR_MESSAGE = 'Please sign in again.';

export function getErrorMessage(error: unknown, fallbackMessage = DEFAULT_ERROR_MESSAGE): string {
  if (error instanceof AuthExpiredError) {
    return AUTH_EXPIRED_ERROR_MESSAGE;
  }

  if (error instanceof ApiError) {
    return error.message || fallbackMessage;
  }

  if (error instanceof Error) {
    return error.message || fallbackMessage;
  }

  return fallbackMessage;
}

export function getOptionalErrorMessage(error: unknown, fallbackMessage = DEFAULT_ERROR_MESSAGE): string | null {
  return error ? getErrorMessage(error, fallbackMessage) : null;
}
