import { logger } from "../../utils/logger.js";
export class BaseRegistry {
    items = new Map();
    initialized = false;
    initPromise = null;
    async ensureInitialized() {
        if (this.initialized) {
            return;
        }
        if (this.initPromise) {
            return this.initPromise;
        }
        this.initPromise = this.registerAll();
        await this.initPromise;
        this.initialized = true;
    }
    register(id, factory, aliases = [], options) {
        const metadata = options?.metadata ?? {};
        this.items.set(id, { factory, metadata });
        for (const alias of aliases) {
            this.items.set(alias.toLowerCase(), { factory, metadata });
        }
        logger.debug(`Registered ${id} in registry`);
    }
    async get(id) {
        await this.ensureInitialized();
        const entry = this.items.get(id);
        if (!entry) {
            return undefined;
        }
        if (!entry.instance) {
            entry.instance = await entry.factory();
        }
        return entry.instance;
    }
    has(id) {
        return this.items.has(id);
    }
    list() {
        return Array.from(this.items.entries()).map(([id, entry]) => ({
            id,
            metadata: entry.metadata,
        }));
    }
    clear() {
        this.items.clear();
        this.initialized = false;
        this.initPromise = null;
    }
    isInitialized() {
        return this.initialized;
    }
}
