// 统一的 OS logo 解析。
// 输入可能是带分隔符的镜像标识（如 "centos9_64|CentOS 9 Stream x64"），
// 也可能是用户友好名称。返回 /os-logos/*.svg 路径，匹配不到时返回兜底 linux.svg。

const OS_LOGO_MAP: { keywords: string[]; file: string }[] = [
  { keywords: ["ubuntu"],                        file: "ubuntu.svg" },
  { keywords: ["centos"],                        file: "centos.svg" },
  { keywords: ["almalinux", "alma linux", "alma"], file: "linux-almalinux.svg" },
  { keywords: ["rocky"],                         file: "linux-rocky_linux.svg" },
  { keywords: ["opensuse", "open suse", "suse"], file: "opensuse.svg" },
  { keywords: ["arch"],                          file: "arch-linux.svg" },
  { keywords: ["alpine"],                        file: "Alpine-Linux.svg" },
  { keywords: ["oracle linux", "oracle"],        file: "oracle-linux.svg" },
  { keywords: ["kali"],                          file: "linux-kali_linux.svg" },
  { keywords: ["tencentos", "tencent"],          file: "TencentOS.svg" },
  { keywords: ["debian"],                        file: "debian.svg" },
  { keywords: ["fnos", "飞牛"],                  file: "fnos1.svg" },
  { keywords: ["windows"],                       file: "windows.svg" },
  { keywords: ["linux"],                         file: "linux.svg" },
]

const LINUX_FALLBACK = "/os-logos/linux.svg"

/** 返回 logo 路径；匹配不到返回 linux fallback。 */
export function osLogo(osImage: string | null | undefined): string {
  if (!osImage) return LINUX_FALLBACK
  const label = osImage.includes("|") ? osImage.split("|")[1] : osImage
  const lower = label.toLowerCase()
  for (const entry of OS_LOGO_MAP) {
    if (entry.keywords.some((k) => lower.includes(k))) return `/os-logos/${entry.file}`
  }
  return LINUX_FALLBACK
}

/** 匹配命中时返回 logo 路径；否则返回 null（让调用者决定 fallback UI）。 */
export function osLogoOrNull(name: string | null | undefined): string | null {
  if (!name) return null
  const lower = name.toLowerCase()
  for (const entry of OS_LOGO_MAP) {
    if (entry.keywords.some((k) => lower.includes(k))) return `/os-logos/${entry.file}`
  }
  return null
}
