export function getYouTubeId(url) {
  try {
    const u = new URL(url)
    if (u.hostname === 'youtu.be') {
      return u.pathname.slice(1)
    }
    if (u.hostname.includes('youtube.com')) {
      const pathMatch = u.pathname.match(/^\/(?:embed|shorts)\/([^/?]+)/)
      if (pathMatch) return pathMatch[1]
      return u.searchParams.get('v')
    }
  } catch {
    // ignore
  }
  return null
}
