import { AWS_CDN_URL } from "./staticValues";

/**
 * Helper function to construct proper image URLs from AWS CDN
 * Ensures proper URL construction with correct slash handling
 * 
 * @param imagePath - The image path from the API (with or without leading slash)
 * @returns Properly formatted CDN URL
 */
export function getImageUrl(imagePath: string): string {
  if (!imagePath) return "";
 
  // Ensure proper URL construction with slash
  const cdnUrl = AWS_CDN_URL.endsWith('/') ? AWS_CDN_URL.slice(0, -1) : AWS_CDN_URL;
  const path = imagePath.startsWith('/') ? imagePath : `/${imagePath}`;
  
  return `${cdnUrl}${path}`;
}

/**
 * Helper function to get image URL with fallback
 * 
 * @param imagePath - The primary image path
 * @param fallbackPath - The fallback image path
 * @returns Properly formatted CDN URL or fallback URL
 */
export function getImageUrlWithFallback(imagePath: string, fallbackPath: string = ""): string {
  if (imagePath) {
    return getImageUrl(imagePath);
  }
  
  if (fallbackPath) {
    return getImageUrl(fallbackPath);
  }
  
  return "";
}

/**
 * Helper function to validate if an image URL is from the CDN
 * 
 * @param url - The URL to validate
 * @returns Boolean indicating if the URL is from the configured CDN
 */
export function isCDNUrl(url: string): boolean {
  return url.startsWith(AWS_CDN_URL);
}
