import { QueryClient, QueryKey } from '@tanstack/react-query';
import { ServerResult } from '../types';

export async function prefetchServerQuery<TData>(
  queryClient: QueryClient,
  query: {
    queryKey: QueryKey;
    queryFn: () => Promise<ServerResult<TData>>;
  }
): Promise<void> {
  await queryClient.prefetchQuery({
    queryKey: query.queryKey,
    queryFn: async () => {
      const result = await query.queryFn();
      
      if (!result.success) {
        throw new Error(result.error.userMessage);
      }
      
      return result.data;
    }
  });
}
