// Lazy loader + types for the bundled `src/data/licenses.json` data.
//
// The JSON is generated by `node scripts/generate-licenses.mjs` (maintainer-only)
// and committed to source. Vite bundles it as a separate JS chunk via dynamic
// import below, so the heavy ~1–2 MB payload is only loaded when the user
// actually opens the Open Source Licenses panel — no runtime fetch, the data
// lives entirely inside the build artifact.

export interface LicenseEntry {
  name: string;
  version: string;
  source: 'npm' | 'cargo';
  licenses: string[];
  repository?: string;
  homepage?: string;
  description?: string;
  publisher?: string;
  licenseText?: string;
}

export interface LicensesData {
  generatedAt: string;
  project: { name: string; version: string };
  stats: { npm: number; cargo: number; total: number; withFullText: number };
  entries: LicenseEntry[];
}

let cache: Promise<LicensesData> | null = null;

export function loadLicensesData(): Promise<LicensesData> {
  if (cache) return cache;
  cache = import('@/data/licenses.json')
    .then((mod) => (mod.default ?? mod) as LicensesData)
    .catch((e) => {
      cache = null;
      throw e;
    });
  return cache;
}

/**
 * Curated list of dependency names shown in the highlight block at the top of
 * the panel — the user-recognisable backbone of the app. Order matters here;
 * the panel renders them top-to-bottom as listed.
 */
export const HIGHLIGHTED_DEPENDENCIES: Array<{ source: 'npm' | 'cargo'; name: string }> = [
  { source: 'cargo', name: 'tauri' },
  { source: 'npm', name: 'react' },
  { source: 'npm', name: 'zustand' },
  { source: 'cargo', name: 'rodio' },
  { source: 'cargo', name: 'symphonia' },
  { source: 'cargo', name: 'cpal' },
  { source: 'npm', name: 'axios' },
  { source: 'npm', name: '@tanstack/react-virtual' },
  { source: 'npm', name: 'react-i18next' },
  { source: 'npm', name: 'lucide-react' },
];
