// src/index.ts
import { spawn } from "child_process";
import * as path from "path";
import * as fs from "fs";
import { fileURLToPath } from "url";

// src/convert-options.generated.ts
function buildArgs(options) {
  const args = [];
  if (options.outputDir) {
    args.push("--output-dir", options.outputDir);
  }
  if (options.password) {
    args.push("--password", options.password);
  }
  if (options.format) {
    if (Array.isArray(options.format)) {
      if (options.format.length > 0) {
        args.push("--format", options.format.join(","));
      }
    } else {
      args.push("--format", options.format);
    }
  }
  if (options.quiet) {
    args.push("--quiet");
  }
  if (options.contentSafetyOff) {
    if (Array.isArray(options.contentSafetyOff)) {
      if (options.contentSafetyOff.length > 0) {
        args.push("--content-safety-off", options.contentSafetyOff.join(","));
      }
    } else {
      args.push("--content-safety-off", options.contentSafetyOff);
    }
  }
  if (options.keepLineBreaks) {
    args.push("--keep-line-breaks");
  }
  if (options.replaceInvalidChars) {
    args.push("--replace-invalid-chars", options.replaceInvalidChars);
  }
  if (options.useStructTree) {
    args.push("--use-struct-tree");
  }
  if (options.tableMethod) {
    args.push("--table-method", options.tableMethod);
  }
  if (options.readingOrder) {
    args.push("--reading-order", options.readingOrder);
  }
  if (options.markdownPageSeparator) {
    args.push("--markdown-page-separator", options.markdownPageSeparator);
  }
  if (options.textPageSeparator) {
    args.push("--text-page-separator", options.textPageSeparator);
  }
  if (options.htmlPageSeparator) {
    args.push("--html-page-separator", options.htmlPageSeparator);
  }
  if (options.imageOutput) {
    args.push("--image-output", options.imageOutput);
  }
  if (options.imageFormat) {
    args.push("--image-format", options.imageFormat);
  }
  if (options.imageDir) {
    args.push("--image-dir", options.imageDir);
  }
  if (options.pages) {
    args.push("--pages", options.pages);
  }
  if (options.hybrid) {
    args.push("--hybrid", options.hybrid);
  }
  if (options.hybridMode) {
    args.push("--hybrid-mode", options.hybridMode);
  }
  if (options.hybridUrl) {
    args.push("--hybrid-url", options.hybridUrl);
  }
  if (options.hybridTimeout) {
    args.push("--hybrid-timeout", options.hybridTimeout);
  }
  if (options.hybridFallback) {
    args.push("--hybrid-fallback");
  }
  return args;
}

// src/index.ts
var __filename = fileURLToPath(import.meta.url);
var __dirname = path.dirname(__filename);
var JAR_NAME = "opendataloader-pdf-cli.jar";
function executeJar(args, executionOptions = {}) {
  const { streamOutput = false } = executionOptions;
  return new Promise((resolve, reject) => {
    const jarPath = path.join(__dirname, "..", "lib", JAR_NAME);
    if (!fs.existsSync(jarPath)) {
      return reject(
        new Error(`JAR file not found at ${jarPath}. Please run the build script first.`)
      );
    }
    const command = "java";
    const commandArgs = ["-jar", jarPath, ...args];
    const javaProcess = spawn(command, commandArgs);
    let stdout = "";
    let stderr = "";
    javaProcess.stdout.on("data", (data) => {
      const chunk = data.toString();
      if (streamOutput) {
        process.stdout.write(chunk);
      }
      stdout += chunk;
    });
    javaProcess.stderr.on("data", (data) => {
      const chunk = data.toString();
      if (streamOutput) {
        process.stderr.write(chunk);
      }
      stderr += chunk;
    });
    javaProcess.on("close", (code) => {
      if (code === 0) {
        resolve(stdout);
      } else {
        const errorOutput = stderr || stdout;
        const error = new Error(
          `The opendataloader-pdf CLI exited with code ${code}.

${errorOutput}`
        );
        reject(error);
      }
    });
    javaProcess.on("error", (err) => {
      if (err.message.includes("ENOENT")) {
        reject(
          new Error(
            "'java' command not found. Please ensure Java is installed and in your system's PATH."
          )
        );
      } else {
        reject(err);
      }
    });
  });
}
function convert(inputPaths, options = {}) {
  const inputList = Array.isArray(inputPaths) ? inputPaths : [inputPaths];
  if (inputList.length === 0) {
    return Promise.reject(new Error("At least one input path must be provided."));
  }
  for (const input of inputList) {
    if (!fs.existsSync(input)) {
      return Promise.reject(new Error(`Input file or folder not found: ${input}`));
    }
  }
  const args = [...inputList, ...buildArgs(options)];
  return executeJar(args, {
    streamOutput: !options.quiet
  });
}
function run(inputPath, options = {}) {
  console.warn(
    "Warning: run() is deprecated and will be removed in a future version. Use convert() instead."
  );
  const formats = [];
  if (!options.noJson) {
    formats.push("json");
  }
  if (options.generateMarkdown) {
    if (options.addImageToMarkdown) {
      formats.push("markdown-with-images");
    } else if (options.htmlInMarkdown) {
      formats.push("markdown-with-html");
    } else {
      formats.push("markdown");
    }
  }
  if (options.generateHtml) {
    formats.push("html");
  }
  if (options.generateAnnotatedPdf) {
    formats.push("pdf");
  }
  return convert(inputPath, {
    outputDir: options.outputFolder,
    password: options.password,
    replaceInvalidChars: options.replaceInvalidChars,
    keepLineBreaks: options.keepLineBreaks,
    contentSafetyOff: options.contentSafetyOff,
    useStructTree: options.useStructTree,
    format: formats.length > 0 ? formats : void 0,
    quiet: !options.debug
  });
}
export {
  buildArgs,
  convert,
  run
};
//# sourceMappingURL=index.js.map