export class Selection {
    ids = new Set();
    listeners = new Set();
    current() {
        return [...this.ids];
    }
    has(id) {
        return this.ids.has(id);
    }
    set(ids) {
        this.replace(new Set(ids));
    }
    add(id) {
        this.replace(new Set([...this.ids, id]));
    }
    remove(id) {
        const next = new Set(this.ids);
        next.delete(id);
        this.replace(next);
    }
    clear() {
        this.replace(new Set());
    }
    subscribe(listener) {
        this.listeners.add(listener);
        return () => this.listeners.delete(listener);
    }
    prune(alive) {
        this.replace(new Set([...this.ids].filter(alive)));
    }
    replace(next) {
        const previous = this.current();
        this.ids.clear();
        for (const id of next) {
            this.ids.add(id);
        }
        const current = this.current();
        if (previous.length !== current.length || previous.some((id, index) => id !== current[index])) {
            const change = { previous, current };
            for (const listener of [...this.listeners]) {
                listener(change);
            }
        }
    }
}
//# sourceMappingURL=Selection.js.map