import { useQuery } from '@tanstack/react-query';
import { listPublicCampuses } from '@/shared/api/campuses';
import { CAMPUS_QUERY_KEYS } from '@/shared/constants/campuses';
import { toCampusCatalogViewModels } from '@/business/campuses/mappers';
import { selectApiListRows } from '@/shared/business/apiListRows';

const EMPTY_CAMPUSES: ReturnType<typeof toCampusCatalogViewModels> = [];

export function useCampusCatalog() {
  const query = useQuery({
    queryKey: CAMPUS_QUERY_KEYS.catalog,
    queryFn: () => selectApiListRows(listPublicCampuses(), toCampusCatalogViewModels),
  });

  return {
    campuses: query.data ?? EMPTY_CAMPUSES,
    isLoading: query.isLoading,
    error: query.error,
    refresh: query.refetch,
  };
}
