import type { ComponentType } from 'react';

/**
 * A component a package contributes by name, so code that only knows the name
 * (usually because it came from the server) can render it.
 */
const registry = new Map<string, ComponentType<never>>();

export function registerComponent<P>(
  name: string,
  component: ComponentType<P>,
): void {
  registry.set(name, component as ComponentType<never>);
}

export function getComponent<P>(
  name: string | undefined,
): ComponentType<P> | undefined {
  return name
    ? (registry.get(name) as ComponentType<P> | undefined)
    : undefined;
}
