/**
 * ProjectionClient - In-process projection consumer
 * DD §13.2. Direct ref (not IPC). TUI imports ONLY contracts + this client.
 *
 * @module packages/tui/src/ProjectionClient
 */

import type { SessionProjection, ProjectionSubscriber } from './types.js'

export class ProjectionClient {
  private snapshot: SessionProjection | null = null
  private subscribers: Set<ProjectionSubscriber> = new Set()

  /**
   * Receive and cache a projection snapshot.
   */
  receive_snapshot(projection: SessionProjection): void {
    this.snapshot = projection
    for (const sub of this.subscribers) {
      sub(projection)
    }
  }

  /**
   * Subscribe to projection updates.
   */
  subscribe(subscriber: ProjectionSubscriber): () => void {
    this.subscribers.add(subscriber)
    return () => this.subscribers.delete(subscriber)
  }

  /**
   * Get current snapshot.
   */
  get_snapshot(): SessionProjection | null {
    return this.snapshot
  }
}
