import { useQuery } from '@tanstack/react-query';
import { getContentCatalog } from '@/shared/api/contentCatalog';
import { CONTENT_CATALOG_QUERY_KEYS } from '@/shared/constants/contentCatalog';

export function useContentCatalogPayload<TPayload>(
  contentType: string,
  emptyPayload: TPayload,
) {
  const query = useQuery({
    queryKey: [...CONTENT_CATALOG_QUERY_KEYS.content, contentType],
    queryFn: async () => {
      const response = await getContentCatalog<TPayload>(contentType);
      return response.payload;
    },
  });

  return {
    payload: query.data ?? emptyPayload,
    isLoading: query.isLoading,
    error: query.error,
    refresh: query.refetch,
  };
}
