import { getRootLogger } from '../utils/logger.js';
import { Registry } from './Registry.js';
export class ManagedRegistry extends Registry {
    log;
    idField;
    computeId;
    constructor(config) {
        super();
        this.idField = config.idField;
        this.computeId = config.computeId;
        this.log = (config.logger ?? getRootLogger()).child({
            component: config.componentName,
        });
    }
    register(idOrItem, maybeItem) {
        if (Array.isArray(idOrItem)) {
            for (const item of idOrItem) {
                this.register(item);
            }
            return;
        }
        if (typeof idOrItem === 'string') {
            if (!maybeItem) {
                throw new Error('register(id, item) requires an item argument');
            }
            const id = idOrItem;
            const item = maybeItem;
            if (this.has(id)) {
                this.log.warn(`"${id}" already registered, overwriting.`);
            }
            super.register(id, item);
            this.log.info(`Registered: ${id}`);
            return;
        }
        const item = idOrItem;
        const id = this.computeId
            ? this.computeId(item)
            : this.idField
                ? String(item[this.idField])
                : undefined;
        if (id === undefined) {
            throw new Error('register(item) requires idField or computeId to be configured');
        }
        if (this.has(id)) {
            this.log.warn(`"${id}" already registered, overwriting.`);
        }
        super.register(id, item);
        this.log.info(`Registered: ${id}`);
    }
    getOrThrow(id) {
        const item = this.get(id);
        if (!item) {
            throw new Error(`Not found: "${id}". Available: ${this.listIds().join(', ')}`);
        }
        return item;
    }
}
//# sourceMappingURL=ManagedRegistry.js.map