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 ensureDirectoryExists } from "../fs.js";
import fsP from "node:fs/promises";
import path from "node:path";

//#region src/cli/html.ts
var import_lib = require_lib();
var Transform = class Transform extends import_lib.Command {
	static description = "Assembles markdown input files and transforms them to a single HTML file with assets embedded";
	static examples = [
		"<%= config.bin %> <%= command.id %> one.md two.md three.md > result.html",
		"<%= config.bin %> <%= command.id %> --format html one.md two.md three.md > result.html",
		"<%= config.bin %> <%= command.id %> --css styles.css --css theme.css one.md two.md > result.html",
		"<%= config.bin %> <%= command.id %> -o result.html --css styles.css one.md two.md",
		"<%= config.bin %> <%= command.id %> -o result.html --css styles.css --watch one.md two.md",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml -o result.html",
		"<%= config.bin %> <%= command.id %> --project .bookify.yml -o result.html --watch"
	];
	static args = { files: import_lib.Args.string({
		description: "Markdown files to assemble (not allowed with --project)",
		name: "files",
		required: false
	}) };
	static flags = {
		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 HTML file path (required when using --watch)"
		}),
		project: import_lib.Flags.string({
			char: "p",
			description: "Path to a .bookify.yml project file",
			exclusive: ["css"]
		}),
		watch: import_lib.Flags.boolean({
			char: "w",
			dependsOn: ["output"],
			description: "Watch for changes and rebuild automatically"
		})
	};
	static strict = false;
	async run() {
		const { argv, flags } = await this.parse(Transform);
		const outputPath = flags.output ? path.resolve(process.cwd(), flags.output) : null;
		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");
			project = resolveConfig({
				css: flags.css,
				inputs: argv
			});
		}
		const engine = new BookifyEngine({ logger: makeOclifLogger(this) });
		if (flags.watch && outputPath) await engine.watch(project, { htmlOutputPath: outputPath });
		else {
			const html = await engine.renderHtml(project);
			if (outputPath) {
				await ensureDirectoryExists(outputPath);
				await fsP.writeFile(outputPath, html, "utf-8");
			} else this.log(html);
		}
	}
};

//#endregion
export { Transform as default };
//# sourceMappingURL=html.js.map