import { ComponentType, IComponent, WebComponent } from '../index'; export interface ComponentMetadata { type: ComponentType; instance?: IComponent; webComponent?: WebComponent; loaded: boolean; dependencies: ComponentType[] | any[]; } export class ComponentRegistry { private static instance: ComponentRegistry; private components = new Map, ComponentMetadata>(); private componentsBySelector = new Map>(); private constructor() {} static get(): ComponentRegistry { if (!ComponentRegistry.instance) { ComponentRegistry.instance = new ComponentRegistry(); } return ComponentRegistry.instance; } register(type: ComponentType, instance?: IComponent): void { const dependencies = type._quarcComponent[0].imports || []; this.components.set(type, { type, instance, loaded: false, dependencies, }); this.componentsBySelector.set(type._quarcComponent[0].selector, type); } markAsLoaded(type: ComponentType, webComponent: WebComponent): void { const metadata = this.components.get(type); if (metadata) { metadata.loaded = true; metadata.webComponent = webComponent; } } isLoaded(type: ComponentType): boolean { return this.components.get(type)?.loaded ?? false; } getMetadata(type: ComponentType): ComponentMetadata | undefined { return this.components.get(type); } getBySelector(selector: string): ComponentMetadata | undefined { const type = this.componentsBySelector.get(selector); return type ? this.components.get(type) : undefined; } getWebComponent(type: ComponentType): WebComponent | undefined { return this.components.get(type)?.webComponent; } getDependencies(type: ComponentType): ComponentType[] { return this.components.get(type)?.dependencies ?? []; } getAllDependencies(type: ComponentType): ComponentType[] { const visited = new Set>(); const dependencies: ComponentType[] = []; const collectDependencies = (componentType: ComponentType) => { if (visited.has(componentType)) return; visited.add(componentType); const deps = this.getDependencies(componentType); deps.forEach(dep => { dependencies.push(dep); collectDependencies(dep); }); }; collectDependencies(type); return dependencies; } clear(): void { this.components.clear(); this.componentsBySelector.clear(); } getAll(): ComponentMetadata[] { return Array.from(this.components.values()); } }