// ILoveMD Extension High-Precision Multi-Format Conversion Engine
// Delegates 100% of conversion to the bundled website engine (ILoveMDConverter)

async function convertFileToMarkdownInExtension(file) {
  const ext = file.name.split(".").pop().toLowerCase();
  const converter = window.ILoveMDConverter || globalThis.ILoveMDConverter;

  if (converter) {
    switch (ext) {
      case "pdf":
        if (typeof converter.convertPdfToMarkdown === "function") {
          return await converter.convertPdfToMarkdown(file);
        }
        break;
      case "docx":
      case "doc":
        if (typeof converter.convertDocxToMarkdown === "function") {
          return await converter.convertDocxToMarkdown(file);
        }
        break;
      case "xlsx":
      case "xls":
        if (typeof converter.convertExcelToMarkdown === "function") {
          return await converter.convertExcelToMarkdown(file);
        }
        break;
      case "csv":
        if (typeof converter.convertCsvToMarkdown === "function") {
          return await converter.convertCsvToMarkdown(file);
        }
        break;
      case "json":
        if (typeof converter.convertJsonToMarkdown === "function") {
          return await converter.convertJsonToMarkdown(file);
        }
        break;
      case "xml":
        if (typeof converter.convertXmlToMarkdown === "function") {
          return await converter.convertXmlToMarkdown(file);
        }
        break;
      case "html":
      case "htm":
        if (typeof converter.convertHtmlToMarkdown === "function") {
          return await converter.convertHtmlToMarkdown(file);
        }
        break;
      case "png":
      case "jpg":
      case "jpeg":
      case "webp":
      case "gif":
      case "svg":
        if (typeof converter.convertImageToMarkdown === "function") {
          return await converter.convertImageToMarkdown(file);
        }
        break;
    }
  }

  // Fallback text reader if converter engine is initializing
  const text = await file.text();
  return `# File: ${file.name}\n\n${text}`;
}
