// Auto-suggests a real service/brand icon for a bookmark by matching its
// title (and URL host) against a curated alias list, pointing at the
// dashboard-icons CDN — the same icon pack used by most self-hosted
// dashboards (Homarr, Homepage, etc.) for exactly this purpose.
const ICON_CDN_BASE = 'https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg'

// key: lowercase phrase to match in the bookmark title or URL host.
// value: the icon slug in the dashboard-icons repo.
const SERVICE_ALIASES: Record<string, string> = {
  aws: 'amazon-web-services',
  'amazon web services': 'amazon-web-services',
  ec2: 'amazon-web-services',
  azure: 'azure',
  'microsoft azure': 'azure',
  gcp: 'google-cloud',
  'google cloud': 'google-cloud',
  proxmox: 'proxmox',
  pve: 'proxmox',
  docker: 'docker',
  kubernetes: 'kubernetes',
  k8s: 'kubernetes',
  github: 'github',
  gitlab: 'gitlab',
  gitea: 'gitea',
  bitbucket: 'bitbucket',
  cloudflare: 'cloudflare',
  netbird: 'netbird',
  grafana: 'grafana',
  prometheus: 'prometheus',
  'uptime kuma': 'uptime-kuma',
  'uptime-kuma': 'uptime-kuma',
  nginx: 'nginx',
  'nginx proxy manager': 'nginx-proxy-manager',
  portainer: 'portainer',
  jellyfin: 'jellyfin',
  plex: 'plex',
  sonarr: 'sonarr',
  radarr: 'radarr',
  wordpress: 'wordpress',
  mysql: 'mysql',
  mariadb: 'mariadb',
  postgres: 'postgresql',
  postgresql: 'postgresql',
  mongodb: 'mongodb',
  redis: 'redis',
  'home assistant': 'home-assistant',
  'home-assistant': 'home-assistant',
  pihole: 'pi-hole',
  'pi-hole': 'pi-hole',
  traefik: 'traefik',
  'visual studio code': 'visual-studio-code',
  vscode: 'visual-studio-code',
  slack: 'slack',
  discord: 'discord',
  notion: 'notion',
  jira: 'jira',
  confluence: 'confluence',
  jenkins: 'jenkins',
  ansible: 'ansible',
  terraform: 'terraform',
  vault: 'vault',
  minio: 'minio',
  truenas: 'truenas',
  synology: 'synology',
  unifi: 'unifi',
  ubiquiti: 'unifi',
  pfsense: 'pfsense',
  opnsense: 'opnsense',
  vmware: 'vmware',
  vsphere: 'vmware',
  esxi: 'vmware',
  windows: 'windows',
  linux: 'linux',
  ubuntu: 'ubuntu',
  debian: 'debian',
  fedora: 'fedora',
  archlinux: 'archlinux',
  'arch linux': 'archlinux',
  rancher: 'rancher',
  jupyter: 'jupyter',
  gitlabci: 'gitlab',
  npm: 'npm',
  nodejs: 'nodejs',
  'node.js': 'nodejs',
  python: 'python',
  react: 'react',
  vaultwarden: 'vaultwarden',
  bitwarden: 'bitwarden',
  netdata: 'netdata',
  zabbix: 'zabbix',
  nextcloud: 'nextcloud',
  owncloud: 'owncloud',
  gitkraken: 'gitkraken',
  digitalocean: 'digitalocean',
  'digital ocean': 'digitalocean',
  linode: 'linode',
  vultr: 'vultr',
  heroku: 'heroku',
  vercel: 'vercel',
  netlify: 'netlify',
  cpanel: 'cpanel',
  webmin: 'webmin',
  pterodactyl: 'pterodactyl',
  rabbitmq: 'rabbitmq',
  kafka: 'kafka',
  elasticsearch: 'elasticsearch',
  kibana: 'kibana',
  logstash: 'logstash',
}

function normalize(s: string): string {
  return s.toLowerCase().trim()
}

/** Best-effort guess at a real brand icon for a bookmark, or null if nothing matched. */
export function guessServiceIconUrl(title: string, url?: string): string | null {
  const haystack = [normalize(title)]
  if (url) {
    try {
      haystack.push(normalize(new URL(url).hostname))
    } catch {
      // not a valid absolute URL yet (e.g. still being typed) — ignore
    }
  }

  for (const [alias, slug] of Object.entries(SERVICE_ALIASES)) {
    if (haystack.some((h) => h.includes(alias))) {
      return `${ICON_CDN_BASE}/${slug}.svg`
    }
  }
  return null
}
