export function contactPairKey(colliderA, colliderB) {
    return colliderA < colliderB ? `${colliderA}:${colliderB}` : `${colliderB}:${colliderA}`;
}
export class CollisionEventQueue {
    previousContacts = new Map();
    events = [];
    update(currentContacts) {
        const next = new Map();
        const events = [];
        for (const contact of currentContacts) {
            const pairKey = contactPairKey(contact.colliderA, contact.colliderB);
            next.set(pairKey, contact);
            events.push({ type: this.previousContacts.has(pairKey) ? "stay" : "begin", pairKey, contact });
        }
        for (const [pairKey, contact] of this.previousContacts) {
            if (!next.has(pairKey)) {
                events.push({ type: "end", pairKey, contact });
            }
        }
        events.sort((a, b) => a.pairKey.localeCompare(b.pairKey) || a.type.localeCompare(b.type));
        this.previousContacts = next;
        this.events = events;
        return this.drain();
    }
    removeCollider(colliderId) {
        const removedEvents = [];
        for (const [pairKey, contact] of this.previousContacts) {
            if (contact.colliderA === colliderId || contact.colliderB === colliderId) {
                const event = { type: "end", pairKey, contact };
                this.events.push(event);
                removedEvents.push(event);
                this.previousContacts.delete(pairKey);
            }
        }
        return removedEvents;
    }
    drain() {
        const events = this.events;
        this.events = [];
        return events;
    }
    snapshotContacts() {
        return Array.from(this.previousContacts.values());
    }
}
//# sourceMappingURL=CollisionEvents.js.map