import { Document, Packer, Paragraph } from "docx";
import { saveAs } from "file-saver";
import * as mammoth from "mammoth";

export function exportWord(content: string) {
  console.log(content);
  // 简单类型检测示例
  const isHTML = (text: string): boolean => {
    return (
      /<html[\s>]/.test(text) &&
      /<\/html>/.test(text) &&
      /<head>/.test(text) &&
      /<body>/.test(text)
    );
  };
  if (isHTML(content)) {
    exportHtmlToWord(content);
  } else {
    exportMarkdownToWord(content);
  }
}

export function exportHtmlToWord(content: string) {
  let cleanedContent = content.startsWith("```html")
    ? content.substring(8)
    : content;
  if (cleanedContent.endsWith("```")) {
    cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
  }
  const blob = new Blob([cleanedContent], { type: "application/msword" });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = "htmldemo.docx";
  // 触发点击事件,开始下载
  document.body.appendChild(a);
  a.click();
  // 下载完成后移除临时链接元素
  document.body.removeChild(a);
  // 释放 Blob URL 对象
  URL.revokeObjectURL(url);
}

function exportMarkdownToWord(content: string) {
  // 按换行符拆分内容
  const lines = content.split(/\r?\n/);
  const paragraphs: Paragraph[] = [];
  for (const line of lines) {
    // 去除 Markdown 标记(#、*等)
    const cleanedLine = line
      .replace(/^#+\s*/, "")
      .replace(/^\*\*\s*|\*\s*/g, "")
      .trim();
    // 处理空行
    if (cleanedLine === "") {
      paragraphs.push(new Paragraph(""));
      continue;
    }
    // 添加文本段落
    paragraphs.push(new Paragraph(cleanedLine));
  }

  // 创建 Word 文档对象
  const doc = new Document({
    sections: [
      {
        children: paragraphs,
      },
    ],
  });

  // 转换为 Blob 并下载
  Packer.toBlob(doc)
    .then((blob) => {
      saveAs(blob, "demo.docx");
    })
    .catch((error) => {
      console.error("导出 Word 失败:", error);
    });
}

export async function getWordData(file: File) {
  try {
    const arrayBuffer = await file.arrayBuffer();
    const { value, messages } = await mammoth.extractRawText({ arrayBuffer });
    return value;
  } catch (error) {
    console.error("Error extracting Word content:", error);
    throw error;
  }
}