import { inject } from '@angular/core';
import { TranslocoService } from '@jsverse/transloco';
import { toast } from '@spartan-ng/brain/sonner';
import { type GraphQLErrorHandler, type GraphQLTranslationError } from '../graphql.types';

export function auroraGraphqlErrorHandlerFactory(): GraphQLErrorHandler {
  const transloco = inject(TranslocoService);

  return {
    handleGraphQLErrors(errors: readonly GraphQLTranslationError[]): void {
      for (const error of errors) {
        toast.error(error.message);
      }
    },
    handleNetworkError(error: Error, statusCode?: number): void {
      toast.error(transloco.translate('SomethingWentWrong'));
      console.error(`[Network Error${statusCode != null ? ` ${statusCode}` : ''}]:`, error.message);
    },
  };
}
