import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { LEAGUE_SETTINGS_SERVICE_TOKEN } from '@/lib/di/tokens';
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
import { ApiError } from '@/lib/api/base/ApiError';
import type { LeagueSettingsViewModel } from '@/lib/view-models/LeagueSettingsViewModel';

export function useLeagueSettings(
  leagueId: string,
  options?: Omit<UseQueryOptions<LeagueSettingsViewModel | null, ApiError>, 'queryKey' | 'queryFn'>
) {
  const leagueSettingsService = useInject(LEAGUE_SETTINGS_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['leagueSettings', leagueId],
    queryFn: () => leagueSettingsService.getLeagueSettings(leagueId),
    enabled: !!leagueId,
    retry: false,
    refetchOnWindowFocus: false,
    ...options,
  });

  return enhanceQueryResult(queryResult);
}
