#!/usr/bin/env bun

import { join } from 'path';

async function generateLucideIcons() {
	console.log('Generating Lucide icons...');
	
	// Read all lucide icons data using Bun.file
	const lucideJsonFile = Bun.file(join(process.cwd(), 'node_modules/@iconify-json/lucide/icons.json'));
	const lucideData = await lucideJsonFile.json() as { icons: Record<string, any> };
	const iconNames = Object.keys(lucideData.icons);
	
	// Generate import statement
	const imports = `import lucideIcons from '@iconify-json/lucide/icons.json';`;
	
	// Generate registry entries for ALL icons
	const registryEntries = iconNames.map(iconName => {
		return `\t'lucide:${iconName}': icons['${iconName}'],`;
	}).join('\n');
	
	// Create the lucide-icons.ts file
	const lucideIconsContent = `// AUTO-GENERATED FILE: All Lucide Icons (${iconNames.length} icons)
// Generated by generate-icons.ts
// DO NOT EDIT MANUALLY

${imports}

// Type the icons object properly
const icons = lucideIcons.icons as Record<string, { body: string; width?: number; height?: number; top?: number }>;

export const lucideIconRegistry = {
${registryEntries}
} as const;

export type LucideIconName = keyof typeof lucideIconRegistry;
`;
	
	// Write the file using Bun.write
	await Bun.write('src/components/common/lucide-icons.ts', lucideIconsContent);
	console.log(`Generated lucide-icons.ts with ${iconNames.length} icons`);
}

async function generateMaterialIcons() {
	console.log('Generating Material Icon Theme icons...');
	
	// Read all Material Icon Theme icons from the icons.json file using Bun.file
	const iconsJsonFile = Bun.file(join(process.cwd(), 'node_modules/@iconify-json/material-icon-theme/icons.json'));
	const iconsData = await iconsJsonFile.json() as { icons: Record<string, any> };
	const iconNames = Object.keys(iconsData.icons);
	
	// Generate import statements
	const imports = `import materialIcons from '@iconify-json/material-icon-theme/icons.json';`;
	
	// Generate registry entries
	const registryEntries = iconNames.map(iconName => {
		return `\t'material:${iconName}': icons['${iconName}'],`;
	}).join('\n');
	
	// Create the material-icons.ts file
	const materialIconsContent = `// AUTO-GENERATED FILE: All Material Icon Theme Icons (${iconNames.length} icons)
// Generated by generate-icons.ts
// DO NOT EDIT MANUALLY

${imports}

// Type the icons object properly
const icons = materialIcons.icons as Record<string, { body: string; width?: number; height?: number; top?: number }>;

// Registry mapping for Icon.svelte
export const materialIconRegistry = {
${registryEntries}
} as const;

export type MaterialIconName = keyof typeof materialIconRegistry;
`;
	
	// Write the file using Bun.write
	await Bun.write('src/components/common/material-icons.ts', materialIconsContent);
	console.log(`Generated material-icons.ts with ${iconNames.length} icons`);
}

// Generate both icon sets
console.log('Starting icon generation...\n');
await generateLucideIcons();
console.log('');
await generateMaterialIcons();
console.log('\nIcon generation complete!');