/**
 * Batched update message format - matches Python server's 'u' type
 * The updates are sent as a flat object with 'type: u' for compatibility
 */
export interface BatchedMessage {
  type: 'u';
  [key: string]: unknown;
}

export class BatchedUpdate {
  private updates: Map<string, unknown> = new Map();

  add(key: string, value: unknown): void {
    this.updates.set(key, value);
  }

  get isEmpty(): boolean {
    return this.updates.size === 0;
  }

  /**
   * Convert to message format matching Python server's 'u' type
   * Updates are spread as top-level properties (not nested)
   */
  toMessage(): BatchedMessage {
    const message: BatchedMessage = { type: 'u' };
    for (const [key, value] of this.updates) {
      message[key] = value;
    }
    this.updates.clear();
    return message;
  }

  clear(): void {
    this.updates.clear();
  }
}
