// Simple in-memory cache for avatar URLs that have been successfully loaded
// This prevents the flicker when avatars re-render

const loadedAvatars = new Set<string>();

export function isAvatarCached(url: string | undefined): boolean {
  if (!url) return false;
  return loadedAvatars.has(url);
}

export function cacheAvatar(url: string | undefined): void {
  if (url) {
    loadedAvatars.add(url);
  }
}

// Optional: limit cache size to prevent memory issues
const MAX_CACHE_SIZE = 1000;

export function pruneAvatarCache(): void {
  if (loadedAvatars.size > MAX_CACHE_SIZE) {
    const iterator = loadedAvatars.values();
    // Remove oldest entries (first added)
    for (let i = 0; i < loadedAvatars.size - MAX_CACHE_SIZE; i++) {
      const result = iterator.next();
      if (!result.done) {
        loadedAvatars.delete(result.value);
      }
    }
  }
}
