import { useQuery } from '@tanstack/react-query';
import { geographyApi } from '../apis';
import { geographyKeys } from '../keys';
import { GEO_GC_TIME, GEO_STALE_TIME } from '../constants';

/**
 * The active cities of a province (ordered). Enabled only once a province is selected; each
 * province's cities are cached independently and forever within the session, so switching back
 * to a previously-opened province never refetches.
 */
export function useCities(provinceId?: number | null) {
  return useQuery({
    queryKey: geographyKeys.cities(provinceId),
    queryFn: () => geographyApi.listCities(provinceId as number),
    enabled: provinceId != null,
    staleTime: GEO_STALE_TIME,
    gcTime: GEO_GC_TIME,
  });
}
