#!/usr/bin/env node
import { F as versionForDisplay } from "./util-CsmUMRtR.js";
import { n as isDirectExecution } from "./entry-util-Bh1ksaYK.js";
import process from "node:process";
import { Command, CommanderError } from "commander";
//#region src/cli.ts
const commandLoaders = {
	build: async () => (await import("./build.runner-CNstCe42.js")).runBuildCli,
	create: async () => (await import("./create.runner-BuzKVM-O.js")).runCreateCli,
	init: async () => (await import("./init.runner-GuzeM0JD.js")).runInitCli,
	preview: async () => (await import("./preview.runner-BFuOuIpN.js")).runPreviewCli
};
async function runCommand(command, argv) {
	await (await commandLoaders[command]())(argv);
}
function setupProgram(argv) {
	const program = new Command().name("vivliostyle").version(versionForDisplay, "-v, --version").helpCommand(false).exitOverride();
	const addProxyCommand = (command, description) => {
		program.command(command).description(description).helpOption(false).allowUnknownOption().allowExcessArguments().action(async () => {
			const commandIndex = argv.indexOf(command);
			await runCommand(command, [
				"vivliostyle",
				command,
				...argv.slice(commandIndex + 1)
			]);
		});
	};
	addProxyCommand("create", "Scaffold a new Vivliostyle project");
	addProxyCommand("init", "Create a Vivliostyle configuration file");
	addProxyCommand("build", "Create PDF, EPUB, and other publication files");
	addProxyCommand("preview", "Open the preview page and interactively save PDFs");
	program.command("help [command]").description("display help for command").helpOption(false).allowUnknownOption().allowExcessArguments().action(async (command) => {
		if (!command) {
			program.help();
			return;
		}
		if (isCommandName(command)) {
			await runCommand(command, [
				"vivliostyle",
				command,
				"--help"
			]);
			return;
		}
		program.help({ error: true });
	});
	return program;
}
function isCommandName(command) {
	return Object.hasOwn(commandLoaders, command);
}
async function dispatchCli(argv) {
	const commandArgs = argv.slice(2);
	const rootProgram = setupProgram(commandArgs);
	try {
		await rootProgram.parseAsync(commandArgs, { from: "user" });
	} catch (error) {
		if (error instanceof CommanderError) {
			process.exitCode = error.exitCode;
			return;
		}
		throw error;
	}
}
if (isDirectExecution(import.meta.url)) await dispatchCli(process.argv);
//#endregion
export { dispatchCli };

//# sourceMappingURL=cli.js.map