

export const normalizeImageUrl = (imageUrl: string | null | undefined): string => {
  if (!imageUrl) {
    return '';
  }

  // If already a full URL, return as is
  if (imageUrl.startsWith('http://') || imageUrl.startsWith('https://')) {
    return imageUrl;
  }

  const apiBaseUrl = import.meta.env.VITE_API_URL?.replace('/api', '') || 'http://localhost:8000';
  
  // Ensure path starts with / and handle both /uploads/avatars/... and avatars/... cases
  let cleanPath = imageUrl.startsWith('/') ? imageUrl : `/${imageUrl}`;
  
  // If path starts with /avatars/ but not /uploads/, add /uploads prefix
  if (cleanPath.startsWith('/avatars/') && !cleanPath.startsWith('/uploads/')) {
    cleanPath = `/uploads${cleanPath}`;
  }
  
  return `${apiBaseUrl}${cleanPath}`;
};

