import type { ACPMessage } from './acp'

/**
 * ACP-style streaming event "types" are encoded on `ACPMessage.metadata.originalEventType`.
 *
 * Notes:
 * - This is intentionally compatible with GoatChain's streaming events.
 * - `tool_call_delta` is represented as an assistant message with empty `content`,
 *   plus metadata fields so clients can render an in-place preview.
 */
export const ACPEventTypes = [
  'steer_injected',
  'session_created',
  'iteration_start',
  'text_delta',
  'text_end',
  'thinking_start',
  'thinking_delta',
  'thinking_end',
  'tool_call_start',
  'tool_call_delta',
  'tool_call_end',
  'tool_output_start',
  'tool_output_delta',
  'tool_result',
  'tool_approval_requested',
  'requires_action',
  'tool_skipped',
  'iteration_end',
  'response_end',
  'completion_assessed',
  'done',
  'error',
  'compression_start',
  'compression_end',
  'usage_refresh',
  'hook_evaluation',
] as const

export type ACPEventType = (typeof ACPEventTypes)[number]

export type ACPToolCallDeltaMetadata = Readonly<{
  originalEventType: 'tool_call_delta'
  callId: string
  toolName?: string
  argsTextDelta?: string
}>

export type ACPDoneMetadata = Readonly<{
  originalEventType?: 'done'
  done: true
  stopReason?: string
  modelStopReason?: string
  usage?: unknown
  error?: unknown
}>

export function getOriginalEventType(msg: ACPMessage): ACPEventType | null {
  const t = (msg as any)?.metadata?.originalEventType
  return (ACPEventTypes as readonly string[]).includes(t) ? (t as any) : null
}
