import { t as isTruthyEnvValue } from "./env-CIj0loAy.js";
import { a as formatUncaughtError, i as formatErrorMessage } from "./errors-BaENkGSr.js";
import { n as resolveCliName } from "./cli-name-BBcw9o6c.js";
import { t as formatCliCommand } from "./command-format-BdP42cV0.js";
//#region src/cli/failure-output.ts
function hasDebugArg(argv) {
	return Boolean(argv?.some((arg) => arg === "--debug" || arg === "--verbose"));
}
function shouldShowStack(argv, env) {
	return hasDebugArg(argv) || isTruthyEnvValue(env.KLAW_DEBUG);
}
function pushPrefixed(out, value, prefix) {
	for (const line of value.split("\n")) if (line.trim().length > 0) out.push(`[${prefix}] ${line}`);
}
function formatCliFailureLines(options) {
	const env = options.env ?? process.env;
	const cli = resolveCliName(options.argv);
	const lines = [`[${cli}] ${options.title}`, `[${cli}] Reason: ${formatErrorMessage(options.error)}`];
	if (shouldShowStack(options.argv, env)) {
		lines.push(`[${cli}] Stack:`);
		pushPrefixed(lines, formatUncaughtError(options.error), cli);
	} else lines.push(`[${cli}] Debug: set KLAW_DEBUG=1 to include the stack trace.`);
	if (options.includeDoctorHint !== false) lines.push(`[${cli}] Try: ${formatCliCommand(`${cli} doctor`, env)}`);
	lines.push(`[${cli}] Help: ${formatCliCommand(`${cli} --help`, env)}`);
	return lines;
}
//#endregion
export { formatCliFailureLines as t };
