import { pdfjs } from "react-pdf";

let workerInitialized = false;

/**
 * 初始化 PDF.js Worker，确保只初始化一次
 */
export const initPdfWorker = () => {
  if (workerInitialized) {
    window.electronAPI?.log("info", "[PDF Worker] 已初始化，跳过重复设置");
    return;
  }

  const getWorkerSrc = () => {
    if (window.location.protocol === "file:") {
      return new URL("./pdf.worker.min.js", window.location.href).href;
    }
    return "/pdf.worker.min.js";
  };

  const workerSrc = getWorkerSrc();
  pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
  workerInitialized = true;
  
  window.electronAPI?.log("info", `[PDF Worker] 初始化完成，路径: ${workerSrc}`);
};

