function hostMatches(host: string, domain: string): boolean {
  return host === domain || host.endsWith(`.${domain}`)
}

/**
 * Picks an icon for an outbound link based on its domain — `map-pin` for the
 * major map providers, brand icons for a couple of common social platforms,
 * and a generic `link` glyph for everything else.
 */
export function linkIconName(url: string): string {
  let host: string
  let path: string
  try {
    const parsed = new URL(url)
    host = parsed.hostname.toLowerCase().replace(/^www\./, '')
    path = parsed.pathname.toLowerCase()
  } catch {
    return 'link'
  }

  if (hostMatches(host, 'youtube.com') || host === 'youtu.be') {
    return 'youtube'
  }
  if (hostMatches(host, 'instagram.com')) return 'instagram'
  if (
    (hostMatches(host, 'google.com') && path.startsWith('/maps')) ||
    hostMatches(host, 'maps.app.goo.gl') ||
    hostMatches(host, 'maps.apple.com')
  ) {
    return 'map-pin'
  }
  return 'link'
}
