import { ValidationError } from "../core/index.js";
import { componentName } from "./Component.js";
export class ComponentRegistry {
    nextId = 0;
    byCtor = new Map();
    byName = new Map();
    register(ctor, schema) {
        const name = componentName(ctor);
        const existingByCtor = this.byCtor.get(ctor);
        if (existingByCtor)
            return existingByCtor;
        if (this.byName.has(name))
            throw new ValidationError("DUPLICATE_COMPONENT", `Duplicate component name: ${name}`);
        const type = { id: this.nextId, name, ctor, schema: schema ?? ctor.schema ?? { version: 1 } };
        this.nextId += 1;
        this.byCtor.set(ctor, type);
        this.byName.set(name, type);
        return type;
    }
    get(ctor) {
        return this.byCtor.get(ctor);
    }
    require(ctor) {
        const type = this.get(ctor);
        if (!type)
            throw new ValidationError("UNREGISTERED_COMPONENT", `Component is not registered: ${componentName(ctor)}`);
        return type;
    }
    getByName(name) {
        return this.byName.get(name);
    }
    list() {
        return [...this.byCtor.values()].sort((a, b) => a.id - b.id);
    }
}
//# sourceMappingURL=ComponentRegistry.js.map