import fs from "fs/promises";
import path from "path";
import { createRequire } from "module";
const moduleRequire = createRequire(path.join(process.cwd(), "package.json"));
const pdfJsPackageRoot = path.dirname(moduleRequire.resolve("pdfjs-dist/package.json"));
const PDF_CMAP_URL = `${path.join(pdfJsPackageRoot, "cmaps")}${path.sep}`;
const PDF_STANDARD_FONT_DATA_URL = `${path.join(pdfJsPackageRoot, "standard_fonts")}${path.sep}`;
function isImage(mimeType, filePath) {
  const normalized = (mimeType || "").toLowerCase();
  if (normalized.startsWith("image/")) return true;
  if (filePath) {
    const ext = path.extname(filePath).toLowerCase();
    return [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".tiff"].includes(ext);
  }
  return false;
}
function isPlainText(mimeType, ext) {
  return mimeType.startsWith("text/") || ext === ".txt" || ext === ".md" || ext === ".csv" || ext === ".log";
}
function isPdf(mimeType, ext) {
  return mimeType === "application/pdf" || ext === ".pdf";
}
function isDocx(mimeType, ext) {
  return mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || ext === ".docx";
}
async function extractPlainText(filePath) {
  try {
    const text = await fs.readFile(filePath, "utf8");
    const trimmed = text.trim();
    return trimmed.length > 0 ? trimmed : null;
  } catch {
    return null;
  }
}
async function extractPdfText(filePath) {
  try {
    const { getDocument } = await import("pdfjs-dist/legacy/build/pdf.mjs");
    const data = new Uint8Array(await fs.readFile(filePath));
    const loadingTask = getDocument({
      data,
      cMapUrl: PDF_CMAP_URL,
      cMapPacked: true,
      standardFontDataUrl: PDF_STANDARD_FONT_DATA_URL
    });
    const pdfDocument = await loadingTask.promise;
    const textParts = [];
    try {
      for (let pageNumber = 1; pageNumber <= pdfDocument.numPages; pageNumber += 1) {
        const page = await pdfDocument.getPage(pageNumber);
        try {
          const textContent = await page.getTextContent();
          const items = textContent.items;
          if (Array.isArray(items)) {
            const pageText = items.map((item) => typeof item?.str === "string" ? item.str : "").filter((s) => s.length > 0).join(" ");
            if (pageText.trim()) textParts.push(pageText.trim());
          }
        } finally {
          page.cleanup();
        }
      }
    } finally {
      await loadingTask.destroy();
    }
    const result = textParts.join("\n").trim();
    return result.length > 0 ? result : null;
  } catch {
    return null;
  }
}
async function extractDocxText(filePath) {
  try {
    const mammoth = await import("mammoth");
    const result = await mammoth.extractRawText({ path: filePath });
    const trimmed = result.value.trim();
    return trimmed.length > 0 ? trimmed : null;
  } catch {
    return null;
  }
}
async function extractAttachmentContent(params) {
  const { filePath, mimeType } = params;
  if (!filePath) return null;
  if (isImage(mimeType, filePath)) return null;
  const normalized = (mimeType || "").toLowerCase();
  const ext = path.extname(filePath).toLowerCase();
  if (isPlainText(normalized, ext)) {
    return extractPlainText(filePath);
  }
  if (isPdf(normalized, ext)) {
    return extractPdfText(filePath);
  }
  if (isDocx(normalized, ext)) {
    return extractDocxText(filePath);
  }
  return null;
}
export {
  extractAttachmentContent
};
//# sourceMappingURL=textExtraction.js.map
