import { getClientSideURL } from '@/utilities/getURL';

/**
 * Processes media resource URL to ensure proper formatting
 * @param url The original URL from the resource
 * @param cacheTag Optional cache tag to append to the URL
 * @returns Properly formatted URL with cache tag if provided
 */
export const getMediaUrl = (url: string | null | undefined, cacheTag?: string | null): string => {
  if (!url) return '';

  if (cacheTag && cacheTag !== '') {
    cacheTag = encodeURIComponent(cacheTag);
  }

  // Check if URL already has http/https protocol
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return cacheTag ? `${url}?${cacheTag}` : url;
  }

  // Use NEXT_PUBLIC_VERCEL_URL if available (set in Vercel), otherwise use client-side URL
  const baseUrl =
    process.env.NEXT_PUBLIC_DEPLOYMENT_URL || getClientSideURL();
  return cacheTag ? `${baseUrl}${url}?${cacheTag}` : `${baseUrl}${url}`;
};
