import { useQuery, skipToken } from '@tanstack/react-query';
import { apiClient } from '@/api/client';

/**
 * Resolves an avatar object id to a signed download URL. Mirrors
 * {@link import('./use-download-url').useDownloadUrl} — React Query handles
 * caching and de-duping, so many avatars sharing an id make a single request.
 */
export function useAvatarUrl(
  objectId: string | null | undefined,
): string | undefined {
  const { data } = useQuery({
    queryKey: ['avatar-url', objectId],
    queryFn: objectId
      ? () => apiClient.getAvatarDownloadUrl(objectId)
      : skipToken,
    staleTime: 1000 * 60 * 60, // 1 hour — signed URLs valid for 24h
  });
  return data;
}
