import { ValidationError } from "../core/index.js";
import {} from "./Entity.js";
export class EntityManager {
    generations = [];
    alive = new Set();
    free = [];
    create() {
        const id = this.free.pop() ?? this.generations.length;
        this.generations[id] ??= 0;
        this.alive.add(id);
        return { id, generation: this.generations[id] };
    }
    destroy(entity) {
        if (!this.isAlive(entity))
            return false;
        this.alive.delete(entity.id);
        this.generations[entity.id] += 1;
        if (!Number.isSafeInteger(this.generations[entity.id]))
            throw new ValidationError("ENTITY_GENERATION_OVERFLOW", "Entity generation overflow.");
        this.free.push(entity.id);
        return true;
    }
    isAlive(entity) {
        return this.alive.has(entity.id) && this.generations[entity.id] === entity.generation;
    }
    get size() {
        return this.alive.size;
    }
    entities() {
        return [...this.alive].sort((a, b) => a - b).map((id) => ({ id, generation: this.generations[id] }));
    }
}
//# sourceMappingURL=EntityManager.js.map