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

export function useAvailableLeagues() {
  const sponsorService = useInject(SPONSOR_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['availableLeagues'],
    queryFn: async () => {
      const result = await sponsorService.getAvailableLeagues();
      if (result.isErr()) {
        throw result.getError();
      }
      return result.unwrap();
    },
  });

  return enhanceQueryResult(queryResult);
}