/**
 * CollabAwareness — broadcasts workbook state changes to collab peers.
 *
 * Subscribes to workbook events (sheetAdded, sheetRemoved, sheetRenamed,
 * activeSheetChanged) and sends awareness updates via the CollabClient.
 * Debounces to avoid flooding (100ms).
 */
export class CollabAwareness {
    workbook;
    client;
    unsubscribers = [];
    debounceTimer = null;
    constructor(workbook, client) {
        this.workbook = workbook;
        this.client = client;
        const events = ['sheetAdded', 'sheetRemoved', 'sheetRenamed', 'activeSheetChanged'];
        for (const event of events) {
            const unsub = this.workbook.sheets.on(event, () => this.scheduleUpdate());
            this.unsubscribers.push(unsub);
        }
        // Send initial awareness immediately.
        this.sendUpdate();
    }
    scheduleUpdate() {
        if (this.debounceTimer)
            clearTimeout(this.debounceTimer);
        this.debounceTimer = setTimeout(() => this.sendUpdate(), 100);
    }
    async sendUpdate() {
        try {
            const snapshot = await this.workbook.exportSnapshot();
            const sheets = snapshot.sheets.map((s) => ({
                id: s.id,
                name: s.name,
                usedRange: s.usedRange,
                cellCount: s.cellCount
            }));
            this.client.sendAwareness(sheets);
        }
        catch {
            // Silently ignore — awareness is best-effort.
        }
    }
    destroy() {
        if (this.debounceTimer)
            clearTimeout(this.debounceTimer);
        for (const unsub of this.unsubscribers)
            unsub();
        this.unsubscribers = [];
    }
}
//# sourceMappingURL=awareness.js.map