import type { ComponentProps } from 'react';
import type { LucideIcon } from 'lucide-react';

/**
 * Menu items carry an icon *name* because they are built on the server. Packages
 * register the lucide icons they use from their `module.ts`.
 */
const icons: Record<string, LucideIcon> = {};

export function registerIcons(map: Record<string, LucideIcon>): void {
  Object.assign(icons, map);
}

export function getIcon(name?: string | null): LucideIcon | undefined {
  return name ? icons[name] : undefined;
}

export function Icon({
  name,
  ...props
}: { name?: string | null } & ComponentProps<LucideIcon>) {
  const Component = getIcon(name);

  return Component ? <Component {...props} /> : null;
}
