import { useQuery } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';

export function useLeagueRaces(leagueId: string) {
  const raceService = useInject(RACE_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['leagueRaces', leagueId],
    queryFn: () => raceService.findByLeagueId(leagueId),
    enabled: !!leagueId,
  });

  return enhanceQueryResult(queryResult);
}