import * as LucideIcons from 'lucide-static'
import icons from 'lucide-static/font/info.json'
import { kebabToPascal } from '../src/util/string'
import { addNameSpaceXML, encodeSVG } from '../src/util/svg'

export function getIcon(iconName: string, target: HTMLElement) {
  try {
    const kebabName = iconName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
    const pascalName = kebabToPascal(kebabName)

    let svg = ''
    if (LucideIcons[pascalName as keyof typeof LucideIcons]) {
      svg = LucideIcons[pascalName as keyof typeof LucideIcons] as string
    }

    return {
      name: kebabName,
      svg: LucideIcons[pascalName as keyof typeof LucideIcons]
        ? (LucideIcons[pascalName as keyof typeof LucideIcons] as string)
        : '',
      encodedSvg: LucideIcons[pascalName as keyof typeof LucideIcons]
        ? encodeSVG(addNameSpaceXML(svg))
        : '',
      compat: LucideIcons[pascalName as keyof typeof LucideIcons]
        ? icons[kebabName as keyof typeof icons]?.encodedCode || ''
        : icons['circle-dashed'].encodedCode,
    }
  } catch (e) {
    console.error(`Error loading Lucide icon ${iconName}:`, e)
  }
}
