import { useQuery } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
import type { LeagueScoringPresetDTO } from '@/lib/types/generated/LeagueScoringPresetDTO';

export function useLeagueScoringPresets() {
  const leagueService = useInject(LEAGUE_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['leagueScoringPresets'],
    queryFn: async (): Promise<LeagueScoringPresetDTO[]> => {
      const result = await leagueService.getScoringPresets();
      if (result.isErr()) {
        throw new Error(result.getError().message);
      }
      return result.unwrap() as unknown as LeagueScoringPresetDTO[];
    },
  });

  return enhanceQueryResult(queryResult);
}
