import type { ChatBlock, ToolResultBlock } from "@runtime/types";
import { BlockAuthorizationRequested } from "./BlockAuthorizationRequested";
import { BlockSetupRequested } from "./BlockSetupRequested";
import { BlockAssistantText } from "./BlockAssistantText";
import { BlockPhase } from "./BlockPhase";
import { BlockSystemNote } from "./BlockSystemNote";
import { BlockToolCall } from "./BlockToolCall";
import { BlockUserText } from "./BlockUserText";

// Dispatcher for the typed ChatBlock union. The switch is exhaustive on
// `block.kind` — adding a new block kind requires a new case here, and
// the `never` guard in the default branch makes the compiler enforce it.
//
// Tool results don't render as standalone rows — they're shown inline
// when the user expands their parent tool_call. The page resolves the
// callId → tool_result mapping and passes the matching result here.
export function BlockRenderer({
  block,
  toolResult,
  agent
}: {
  block: ChatBlock;
  toolResult?: ToolResultBlock;
  agent?: { id: string; name: string };
}) {
  switch (block.kind) {
    case "user_text":
      return <BlockUserText block={block} />;
    case "assistant_text":
      return <BlockAssistantText block={block} agent={agent} />;
    case "tool_call":
      return <BlockToolCall block={block} result={toolResult} />;
    case "tool_result":
      return null;
    case "phase":
      return <BlockPhase block={block} />;
    case "authorization_requested":
      return <BlockAuthorizationRequested block={block} />;
    case "setup_requested":
      return <BlockSetupRequested block={block} />;
    case "system_note":
      return <BlockSystemNote block={block} />;
    default: {
      const exhaustive: never = block;
      throw new Error(`Unknown chat block kind: ${JSON.stringify(exhaustive)}`);
    }
  }
}
