import { Bitset } from "./Bitset.js";
import {} from "./Component.js";
import { ComponentRegistry } from "./ComponentRegistry.js";
import {} from "./Entity.js";
import { SparseSet } from "./SparseSet.js";
export class ComponentStore {
    registry;
    stores = new Map();
    signatures = new Map();
    constructor(registry) {
        this.registry = registry;
    }
    add(entity, ctor, component) {
        const type = this.registry.require(ctor);
        this.storeFor(type).add(entity, component);
        this.signatureFor(entity).add(type.id);
    }
    remove(entity, ctor) {
        const type = this.registry.require(ctor);
        const removed = this.storeFor(type).delete(entity);
        if (removed)
            this.signatureFor(entity).delete(type.id);
        return removed;
    }
    get(entity, ctor) {
        const type = this.registry.require(ctor);
        return this.storeFor(type).get(entity);
    }
    has(entity, ctor) {
        const type = this.registry.require(ctor);
        return this.storeFor(type).has(entity);
    }
    deleteEntity(entity) {
        for (const store of this.stores.values())
            store.delete(entity);
        this.signatures.delete(entity.id);
    }
    signature(entity) {
        return this.signatures.get(entity.id)?.clone() ?? new Bitset();
    }
    entries(ctor) {
        const type = this.registry.require(ctor);
        return this.storeFor(type).entries();
    }
    entitiesWith(ctor) {
        const type = this.registry.require(ctor);
        return this.storeFor(type).entities();
    }
    storeFor(type) {
        let store = this.stores.get(type.id);
        if (!store) {
            store = new SparseSet();
            this.stores.set(type.id, store);
        }
        return store;
    }
    signatureFor(entity) {
        let signature = this.signatures.get(entity.id);
        if (!signature) {
            signature = new Bitset();
            this.signatures.set(entity.id, signature);
        }
        return signature;
    }
}
//# sourceMappingURL=ComponentStore.js.map