import { parseCommentText } from "../utils";

// 渲染评论文本（包含表情）
export function CommentText({ text }: { text: string }) {
  const parts = parseCommentText(text);

  return (
    <>
      {parts.map((part, idx: number) => {
        if (typeof part === "string") {
          return <span key={idx}>{part}</span>;
        }
        return (
          <img
            key={idx}
            src={`/emojis/${part.name}.webp`}
            alt={part.name}
            className="inline-block w-5 h-5 align-text-bottom mx-0.5"
            onError={(e) => {
              // 如果图片加载失败，显示原始文本
              e.currentTarget.style.display = "none";
              const textNode = document.createTextNode(`[${part.name}]`);
              e.currentTarget.parentNode?.insertBefore(textNode, e.currentTarget);
            }}
          />
        );
      })}
    </>
  );
}
