import { ValidationError } from "../core/index.js";
import {} from "./Component.js";
import { ComponentRegistry } from "./ComponentRegistry.js";
import { ComponentStore } from "./ComponentStore.js";
import {} from "./Entity.js";
import { EntityManager } from "./EntityManager.js";
import { Query } from "./Query.js";
import { SystemScheduler } from "./SystemScheduler.js";
import {} from "./System.js";
export class World {
    entities = new EntityManager();
    registry = new ComponentRegistry();
    components = new ComponentStore(this.registry);
    systems = new SystemScheduler();
    version = 0;
    registerComponent(ctor, schema) {
        this.registry.register(ctor, schema);
    }
    createEntity() {
        this.version += 1;
        return this.entities.create();
    }
    destroyEntity(entity) {
        this.assertAlive(entity);
        const destroyed = this.entities.destroy(entity);
        if (destroyed) {
            this.components.deleteEntity(entity);
            this.version += 1;
        }
        return destroyed;
    }
    add(entity, ctor, component) {
        this.assertAlive(entity);
        if (!this.registry.get(ctor))
            this.registry.register(ctor);
        this.components.add(entity, ctor, component);
        this.version += 1;
    }
    remove(entity, ctor) {
        this.assertAlive(entity);
        const removed = this.components.remove(entity, ctor);
        if (removed)
            this.version += 1;
        return removed;
    }
    get(entity, ctor) {
        this.assertAlive(entity);
        return this.components.get(entity, ctor);
    }
    has(entity, ctor) {
        this.assertAlive(entity);
        return this.components.has(entity, ctor);
    }
    query(options) {
        return new Query(this.entities, this.components, this.registry, options);
    }
    update(context) {
        this.systems.update(this, context);
    }
    getVersion() {
        return this.version;
    }
    assertAlive(entity) {
        if (!this.entities.isAlive(entity))
            throw new ValidationError("ENTITY_NOT_ALIVE", `Entity is not alive: ${entity.id}:${entity.generation}`);
    }
}
//# sourceMappingURL=World.js.map