import { EContentType, TAnyMessage } from "@/types/bot";
import Taro from "@tarojs/taro";

export function formatMessageItem(item: TAnyMessage){
  if (item.contentType === EContentType.AiseekQA) {
    try {
      const contentJson = JSON.parse(item.content as string);
      item.content = contentJson.answer.text;
      // 把消息详情放入统一 body 中
      item.body = {
        ...item,
        content: contentJson,
        contentType: EContentType.AiseekQA,
      };
    } catch (e) {
      // console.error(e)
    }
  }
  return item;
}

export const handleCopy = (e: any, textStr: string) => {
  e.stopPropagation();
  // 手动复制并 toast 提示
  if (textStr) {
    Taro.setClipboardData({
      data: textStr,
      success() {
        Taro.showToast({
          title: "复制成功",
          icon: "none",
        });
      },
      fail(res) {
        console.log(res);
        Taro.showToast({
          title: "复制失败",
          icon: "none",
        });
      },
    });
  }
};