import { a as require_lib, i as resolveConfig, n as BookifyEngine, r as loadConfig, t as makeOclifLogger } from "./oclif-logger.js";
import { t as ensureDirectoryExists } from "./fs.js";
import fsP from "node:fs/promises";
import path from "node:path";

//#region src/cli/pdf.ts
var import_lib = require_lib();
var Pdf = class Pdf extends import_lib.Command {
	static description = "Renders a set of markdown files into a publishable PDF";
	static examples = [
		"<%= config.bin %> <%= command.id %> -o result.pdf --css base.css --css theme.css -- foo.md bar.md",
		"<%= config.bin %> <%= command.id %> -o result.pdf --css base.css --watch -- foo.md bar.md",
		"<%= config.bin %> <%= command.id %> --renderer euro-pdf --api-key KEY -o result.pdf -- foo.md bar.md",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml -o result.pdf",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml -o result.pdf --watch"
	];
	static args = { files: import_lib.Args.string({
		description: "Markdown files to render (not allowed with --project)",
		name: "files",
		required: false
	}) };
	static flags = {
		"api-key": import_lib.Flags.string({
			description: "EuroPDF API key (required when using euro-pdf renderer)",
			env: "EURO_PDF_API_KEY",
			required: false
		}),
		css: import_lib.Flags.string({
			default: [],
			description: "Path to a CSS stylesheet (can be specified multiple times, not allowed with --project)",
			exclusive: ["project"],
			multiple: true
		}),
		output: import_lib.Flags.string({
			char: "o",
			description: "Output PDF file path",
			required: true
		}),
		project: import_lib.Flags.string({
			char: "p",
			description: "Path to a .bookify.yml project file",
			exclusive: ["css", "renderer"]
		}),
		renderer: import_lib.Flags.string({
			char: "r",
			default: "weasyprint",
			description: "PDF renderer to use",
			options: ["weasyprint", "euro-pdf"]
		}),
		watch: import_lib.Flags.boolean({
			char: "w",
			default: false,
			description: "Watch for changes and rebuild automatically"
		})
	};
	static strict = false;
	async run() {
		const { argv, flags } = await this.parse(Pdf);
		const outputPath = path.resolve(process.cwd(), flags.output);
		const renderer = flags.renderer;
		let project;
		if (flags.project) {
			if (argv.length > 0) this.error("Input files cannot be specified when using --project");
			project = await loadConfig(flags.project);
		} else {
			if (argv.length === 0) this.error("Either --project or input files must be specified");
			if (renderer === "euro-pdf" && !flags["api-key"]) this.error("--api-key is required when using euro-pdf renderer");
			const rendererOptions = renderer === "euro-pdf" ? {
				apiKey: flags["api-key"],
				test: "true"
			} : {};
			project = resolveConfig({
				css: flags.css,
				inputs: argv,
				pdf: {
					renderer,
					rendererOptions
				}
			});
		}
		const engine = new BookifyEngine({ logger: makeOclifLogger(this) });
		if (flags.watch && outputPath) await engine.watch(project, { pdfOutputPath: outputPath });
		else {
			const pdf = await engine.renderPdf(project);
			await ensureDirectoryExists(outputPath);
			await fsP.writeFile(outputPath, Buffer.from(pdf));
		}
	}
};

//#endregion
export { Pdf as t };
//# sourceMappingURL=pdf.js.map