/**
 * Adapter that forwards assistant streaming output to connected RC mobile
 * clients. Taps into the REPL's stream event pipeline so remote clients
 * see responses in real-time.
 *
 * Activated when `/rc` registers a session. All functions are no-ops when
 * no session is active or no RC clients are connected.
 */
import { isRCActive, sendAssistantMessage, sendSessionState, sendToolResult } from './bridge.js'

let activeSessionId: string | null = null

/** Called by the /rc command after registering a session. */
export function setRCStreamSession(sessionId: string): void {
  activeSessionId = sessionId
}

/** Called when the RC server is stopped. */
export function clearRCStreamSession(): void {
  activeSessionId = null
}

/** Returns the active RC session ID, or null if not set. */
export function getRCStreamSessionId(): string | null {
  return activeSessionId
}

/** Forward a streaming text delta to RC clients. */
export function rcForwardTextDelta(text: string): void {
  if (!activeSessionId || !isRCActive()) return
  sendAssistantMessage(activeSessionId, text, false)
}

/** Signal that the assistant response is complete. */
export function rcForwardResponseDone(): void {
  if (!activeSessionId || !isRCActive()) return
  sendAssistantMessage(activeSessionId, '', true)
}

/** Forward a tool result to RC clients. */
export function rcForwardToolResult(requestId: string, output: string): void {
  if (!activeSessionId || !isRCActive()) return
  sendToolResult(activeSessionId, requestId, output)
}

/** Forward session state changes (idle, thinking, streaming, etc.). */
export function rcForwardSessionState(state: import('./types.js').SessionState): void {
  if (!activeSessionId || !isRCActive()) return
  sendSessionState(activeSessionId, state)
}
