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

//#region src/cli/serve.ts
var import_lib = require_lib();
var Serve = class Serve extends import_lib.Command {
	static description = "Watches files and serves generated HTML and PDF files via a web server with live reload";
	static examples = [
		"<%= config.bin %> <%= command.id %> --project .bookify.yml",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml --port 8080",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml --html",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml --pdf",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml --html --pdf",
		"<%= config.bin %> <%= command.id %> --css styles.css one.md two.md"
	];
	static args = { files: import_lib.Args.string({
		description: "Markdown files to assemble (not allowed with --project)",
		name: "files",
		required: false
	}) };
	static flags = {
		"api-key": import_lib.Flags.string({
			description: "EuroPDF API key (required for PDF generation with 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
		}),
		html: import_lib.Flags.boolean({ description: "Serve HTML output (if neither --html nor --pdf specified, both are served)" }),
		pdf: import_lib.Flags.boolean({ description: "Serve PDF output (if neither --html nor --pdf specified, both are served)" }),
		port: import_lib.Flags.integer({
			default: 3e3,
			description: "Port number for the web server"
		}),
		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"]
		})
	};
	static strict = false;
	async run() {
		const { argv, flags } = await this.parse(Serve);
		const outputConfig = this.determineOutputConfig(flags);
		const project = await this.loadOrCreateProject(flags, argv, outputConfig);
		const tmpDir = await this.createTempDirectory();
		outputConfig.htmlPath = outputConfig.serveHtml ? path.join(tmpDir, "output.html") : void 0;
		outputConfig.pdfPath = outputConfig.servePdf ? path.join(tmpDir, "output.pdf") : void 0;
		const server = new PreviewServer(flags.port, outputConfig, (msg) => {
			this.log(msg);
		});
		const engine = this.createEngine();
		const watchOptions = this.createWatchOptions(outputConfig, server);
		await server.start();
		await engine.watch(project, watchOptions);
		await server.stop();
	}
	determineOutputConfig(flags) {
		const serveHtml = flags.html || flags.pdf ? flags.html ?? false : true;
		const servePdf = flags.html || flags.pdf ? flags.pdf ?? false : true;
		if (!serveHtml && !servePdf) this.error("At least one of --html or --pdf must be specified");
		return {
			serveHtml,
			servePdf
		};
	}
	async loadOrCreateProject(flags, argv, outputConfig) {
		if (flags.project) {
			if (argv.length > 0) this.error("Input files cannot be specified when using --project");
			return loadConfig(flags.project);
		}
		if (argv.length === 0) this.error("Either --project or input files must be specified");
		const renderer = flags.renderer;
		if (outputConfig.servePdf && renderer === "euro-pdf" && !flags["api-key"]) this.error("--api-key is required for PDF generation with euro-pdf renderer");
		const rendererOptions = renderer === "euro-pdf" && flags["api-key"] ? {
			apiKey: flags["api-key"],
			test: "true"
		} : {};
		return resolveConfig({
			css: flags.css,
			inputs: argv,
			pdf: outputConfig.servePdf ? {
				renderer,
				rendererOptions
			} : void 0
		});
	}
	async createTempDirectory() {
		const tmpDir = path.join(process.cwd(), ".bookify-tmp");
		await fsP.mkdir(tmpDir, { recursive: true });
		return tmpDir;
	}
	createEngine() {
		return new BookifyEngine({ logger: makeOclifLogger(this) });
	}
	createWatchOptions(config, server) {
		return {
			htmlOutputPath: config.htmlPath,
			pdfOutputPath: config.pdfPath,
			onChangeCompleted: () => {
				server.loadContent();
			}
		};
	}
};

//#endregion
export { Serve as default };
//# sourceMappingURL=serve.js.map