/**
 * Extracts a YouTube video ID from a given URL or returns the ID if a raw ID is provided.
 * Supports various YouTube URL formats including youtu.be, youtube.com/watch, and shorts.
 * 
 * @param urlOrId The full YouTube URL or just the video ID.
 * @returns The 11-character YouTube video ID, or the original string if no match is found.
 */
export function extractYouTubeId(urlOrId: string): string {
  if (!urlOrId) return '';
  
  // Regular expression to match various YouTube URL formats
  const regex = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/|youtube\.com\/shorts\/)([^"&?\/\s]{11})/i;
  const match = urlOrId.match(regex);
  
  if (match && match[1]) {
    return match[1];
  }
  
  // If no match, we assume it might already be an ID or is an invalid format.
  // We'll return the string as-is but trimmed.
  return urlOrId.trim();
}
