// 一次性工具：从 @lobehub/icons-static-svg (MIT) 拉取单色 provider logo，
// 内联生成 desktop/src/react/ui/provider-icons.ts，供 ProviderIcon 使用（零运行时依赖）。
// 新增 provider 时在 ICONS 里加 lobehub 图标名后重跑：node scripts/gen-provider-icons.mjs
import { writeFileSync } from 'node:fs';

// lobehub 图标名（已逐个核验存在）。ProviderIcon 负责把 provider id 映射到这些 key。
const ICONS = [
  'deepseek', 'qwen', 'openai', 'gemini', 'volcengine', 'moonshot',
  'kimi', 'zhipu', 'siliconcloud', 'groq', 'mistral', 'minimax',
  'openrouter', 'xiaomimimo', 'ollama',
];

const out = {};
for (const name of ICONS) {
  const res = await fetch(`https://unpkg.com/@lobehub/icons-static-svg/icons/${name}.svg`);
  if (!res.ok) throw new Error(`fetch ${name} failed: ${res.status}`);
  const svg = await res.text();
  const viewBox = svg.match(/viewBox="([^"]*)"/)?.[1] || '0 0 24 24';
  const content = svg
    .replace(/^[\s\S]*?<svg[^>]*>/, '')
    .replace(/<\/svg>\s*$/, '')
    .replace(/<title>[\s\S]*?<\/title>/, '')
    .trim();
  if (!content.includes('<path') && !content.includes('<g')) {
    throw new Error(`${name}: unexpected svg content -> ${content.slice(0, 80)}`);
  }
  out[name] = { viewBox, content };
  console.log(`✓ ${name} (${content.length} chars)`);
}

const header = '// Auto-generated by scripts/gen-provider-icons.mjs from @lobehub/icons (MIT).\n'
  + '// Single-color provider logos (fill: currentColor). Do not edit by hand.\n\n';
writeFileSync(
  'desktop/src/react/ui/provider-icons.ts',
  header
    + 'export const PROVIDER_ICONS: Record<string, { viewBox: string; content: string }> = '
    + JSON.stringify(out, null, 2) + ';\n',
);
console.log(`\nGenerated ${Object.keys(out).length} icons -> desktop/src/react/ui/provider-icons.ts`);
