import { Typography } from "antd";
import { Markdown } from "markdown-to-jsx/react";

import type { PartProps } from "./types";

import { CodeBlock } from "./CodeBlock";
import { MarkdownTable } from "./MarkdownTable";

interface TextPartProps extends PartProps {
  isStreaming: boolean;
  role: string;
}

export function TextPart({ isStreaming, part, role }: TextPartProps) {
  const text = typeof part["text"] === "string" ? part["text"] : "";

  return (
    <div className="part-body">
      {role === "user" ? (
        <Typography.Paragraph className="message-body-text">{text}</Typography.Paragraph>
      ) : (
        <Markdown
          options={{
            optimizeForStreaming: isStreaming,
            overrides: {
              pre: { component: CodeBlock, props: { isStreaming } },
              table: MarkdownTable,
            },
          }}
        >
          {text}
        </Markdown>
      )}
    </div>
  );
}
