import type { JSX } from "react";
import type { InspectorResponse, OpenAIResponse, RequestFormat } from "../../../proxy/schemas";
import { StructuredResponseViewAnthropic } from "./anthropic/ResponseView";
import { OpenAIResponseView } from "./openai/ResponseView";

export { StructuredResponseViewAnthropic } from "./anthropic/ResponseView";
export { OpenAIResponseView } from "./openai/ResponseView";
export { ResponseContentBlockRenderer } from "./anthropic/ContentBlocks";

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function isOpenAIResponse(response: unknown): response is OpenAIResponse {
  return isRecord(response) && response.object === "chat.completion";
}

function isAnthropicResponse(response: unknown): response is InspectorResponse {
  return isRecord(response) && response.type === "message" && Array.isArray(response.content);
}

export function formatViewFor(
  apiFormat: RequestFormat | undefined,
  response: unknown,
): JSX.Element | null {
  if (apiFormat === "openai" && isOpenAIResponse(response)) {
    return <OpenAIResponseView response={response} />;
  }
  if (apiFormat === "anthropic" && isAnthropicResponse(response)) {
    return <StructuredResponseViewAnthropic response={response} />;
  }
  return null;
}
