import "./paths-mmi2xb99.js";
import { I as isRich, L as theme, c as defaultRuntime, j as setVerbose } from "./subsystem-D4e4nOE7.js";
import "./utils-DLd4fFd2.js";
import "./boolean-Djq1Hgv8.js";
import { t as isTruthyEnvValue } from "./env-Bdq5Y-tD.js";
import "./chrome-CpAaM-7H.js";
import "./brew-Ds23pxcL.js";
import "./runtime-guard-DD5U3rmV.js";
import "./loader-nlcizznp.js";
import "./logging-BdXz5wtj.js";
import { c as getVerboseFlag, i as getCommandPath, u as hasHelpOrVersion } from "./register.subclis-D7CAc31a.js";
import { I as VERSION } from "./config-C9wUwpYY.js";
import { a as formatCliBannerLine, i as emitCliBanner, o as hasEmittedCliBanner, r as registerProgramCommands, t as ensureConfigReady } from "./config-guard-czS2xVVk.js";
import "./agent-scope-BcV171ci.js";
import "./exec-B_ziy0Fn.js";
import "./edwinpai-root-BaNzp4sP.js";
import "./model-selection-ChysOie6.js";
import "./github-copilot-token-D-EydSr2.js";
import { n as replaceCliName, r as resolveCliName } from "./command-format-XQlUKdf6.js";
import "./manifest-registry-DThGPzHr.js";
import "./internal-Ft2bMMnL.js";
import "./files-C_lcxjDi.js";
import "./client-FZxVEb3g.js";
import "./tailnet-CsQPCZBE.js";
import "./call-Dm_M_SfO.js";
import "./message-channel-eeeqF-J8.js";
import "./ws-ld1ms-Ht.js";
import "./sandbox-B_7wnPz0.js";
import "./deliver-Ce4muAXi.js";
import "./plugins-D_f06tiR.js";
import "./accounts-Blm-CSIN.js";
import "./dispatcher-kTQZ3t7n.js";
import "./pi-model-discovery-DZFI_e0Z.js";
import "./routes-CHh8zZx-.js";
import "./pi-embedded-helpers-Hus0BP0L.js";
import "./skills-B9WXNmn5.js";
import "./server-context-CdZ_-Xj5.js";
import "./ports-SUg8rnQx.js";
import "./paths-1OcM9YBo.js";
import "./image-yOVg03mb.js";
import "./exec-approvals-K3ABdHGm.js";
import "./nodes-screen-24Fv30p2.js";
import "./redact-DbMu650y.js";
import "./tool-display-D8xZqYsu.js";
import "./control-service-DYqjNgPG.js";
import "./restart-sentinel-DkbwzVUl.js";
import "./channel-selection-BXORBOHN.js";
import "./channel-summary-xp5M-jlo.js";
import "./commands-Bl1f7vmP.js";
import "./pairing-store-F36xrrvf.js";
import "./login-qr-DWYOkTgz.js";
import { t as formatDocsLink } from "./links-DlcX3SnL.js";
import "./progress-D4tnjzku.js";
import { n as ensurePluginRegistryLoaded } from "./command-options-By5yFv1a.js";
import "./helpers-XH2J9KRX.js";
import "./onboard-helpers-BArU1Ks-.js";
import "./note-BmwttIJ8.js";
import "./clack-prompter-CAZu6UVa.js";
import "./onboarding-CcpOoI2d.js";
import "./onboard-skills-BqrGuDv7.js";
import "./onboard-channels-B_O2u1kZ.js";
import "./shared-DsNlV7x3.js";
import "./wsl-BxK-Spqg.js";
import "./plugin-auto-enable-CAYA2jY0.js";
import "./archive-UFoXv0sh.js";
import "./installs-C0iwT6hr.js";
import "./health-format-DTWwS6zi.js";
import "./update-runner-BCjl92Go.js";
import "./tailscale-jmgId34Y.js";
import "./net-V2OLvHbo.js";
import "./auth-CySi9sC4.js";
import "./audit-DcTWNaLK.js";
import "./table-CTqUON-t.js";
import "./skills-status-CjdifZyI.js";
import "./service-BXV5WdCs.js";
import "./systemd-B3oVOU-l.js";
import "./service-audit-taH0ebK-.js";
import "./node-service-DnDJ49mK.js";
import "./channels-status-issues-DgPHxqCU.js";
import "./status.update-D9eAu2de.js";
import "./gateway-rpc-Dw0OsDV7.js";
import "./help-format-ph1z7eHo.js";
import "./deps-BygaPoRm.js";
import "./agent-yboWdGCD.js";
import "./configure-Trjv6X0t.js";
import "./daemon-runtime-C0L3ZZ7l.js";
import "./service-env-BwW73jih.js";
import "./systemd-linger-CeRc1hOc.js";
import "./widearea-dns-BvuXu7AL.js";
import "./auth-health-D7TxGNjl.js";
import "./doctor-DvQlQAjz.js";
import "./completion-cli-nlw31mEs.js";
import "./systemd-hints-uRWaROC9.js";
import "./hooks-status-BnBA7R7I.js";
import "./tui-BeUMXiYC.js";
import "./auth-token-DMp3C907.js";
import { n as resolveCliChannelOptions } from "./channel-options-DcXxOpf7.js";
import { t as forceFreePort } from "./ports-B0jLeVue.js";
import { Command } from "commander";

//#region ../../src/cli/program/context.ts
function createProgramContext() {
	const channelOptions = resolveCliChannelOptions();
	return {
		programVersion: VERSION,
		channelOptions,
		messageChannelOptions: channelOptions.join("|"),
		agentChannelOptions: ["last", ...channelOptions].join("|")
	};
}

//#endregion
//#region ../../src/cli/program/help.ts
const CLI_NAME = resolveCliName();
const EXAMPLES = [
	["edwinpai channels login --verbose", "Link personal WhatsApp Web and show QR + connection logs."],
	["edwinpai message send --target +15555550123 --message \"Hi\" --json", "Send via your web session and print JSON result."],
	["edwinpai gateway --port 18789", "Run the WebSocket Gateway locally."],
	["edwinpai --dev gateway", "Run a dev Gateway (isolated state/config) on ws://127.0.0.1:19001."],
	["edwinpai gateway --force", "Kill anything bound to the default gateway port, then start it."],
	["edwinpai gateway ...", "Gateway control via WebSocket."],
	["edwinpai agent --to +15555550123 --message \"Run summary\" --deliver", "Talk directly to the agent using the Gateway; optionally send the WhatsApp reply."],
	["edwinpai message send --channel telegram --target @mychat --message \"Hi\"", "Send via your Telegram bot."]
];
function configureProgramHelp(program, ctx) {
	program.name(CLI_NAME).description("").version(ctx.programVersion).option("--dev", "Dev profile: isolate state under ~/.edwinpai-dev, default gateway port 19001, and shift derived ports (browser/canvas)").option("--profile <name>", "Use a named profile (isolates EDWINPAI_STATE_DIR/EDWINPAI_CONFIG_PATH under ~/.edwinpai-<name>)");
	program.option("--no-color", "Disable ANSI colors", false);
	program.configureHelp({
		optionTerm: (option) => theme.option(option.flags),
		subcommandTerm: (cmd) => theme.command(cmd.name())
	});
	program.configureOutput({
		writeOut: (str) => {
			const colored = str.replace(/^Usage:/gm, theme.heading("Usage:")).replace(/^Options:/gm, theme.heading("Options:")).replace(/^Commands:/gm, theme.heading("Commands:"));
			process.stdout.write(colored);
		},
		writeErr: (str) => process.stderr.write(str),
		outputError: (str, write) => write(theme.error(str))
	});
	if (process.argv.includes("-V") || process.argv.includes("--version") || process.argv.includes("-v")) {
		console.log(ctx.programVersion);
		process.exit(0);
	}
	program.addHelpText("beforeAll", () => {
		if (hasEmittedCliBanner()) return "";
		const rich = isRich();
		return `\n${formatCliBannerLine(ctx.programVersion, { richTty: rich })}\n`;
	});
	const fmtExamples = EXAMPLES.map(([cmd, desc]) => `  ${theme.command(replaceCliName(cmd, CLI_NAME))}\n    ${theme.muted(desc)}`).join("\n");
	program.addHelpText("afterAll", ({ command }) => {
		if (command !== program) return "";
		const docs = formatDocsLink("/cli", "docs.edwinpai.com/cli");
		return `\n${theme.heading("Examples:")}\n${fmtExamples}\n\n${theme.muted("Docs:")} ${docs}\n`;
	});
}

//#endregion
//#region ../../src/cli/program/preaction.ts
function setProcessTitleForCommand(actionCommand) {
	let current = actionCommand;
	while (current.parent && current.parent.parent) current = current.parent;
	const name = current.name();
	const cliName = resolveCliName();
	if (!name || name === cliName) return;
	process.title = `${cliName}-${name}`;
}
const PLUGIN_REQUIRED_COMMANDS = new Set([
	"message",
	"channels",
	"directory"
]);
function registerPreActionHooks(program, programVersion) {
	program.hook("preAction", async (_thisCommand, actionCommand) => {
		setProcessTitleForCommand(actionCommand);
		const argv = process.argv;
		if (hasHelpOrVersion(argv)) return;
		const commandPath = getCommandPath(argv, 2);
		if (!(isTruthyEnvValue(process.env.EDWINPAI_HIDE_BANNER) || commandPath[0] === "update" || commandPath[0] === "completion" || commandPath[0] === "plugins" && commandPath[1] === "update")) emitCliBanner(programVersion);
		const verbose = getVerboseFlag(argv, { includeDebug: true });
		setVerbose(verbose);
		if (!verbose) process.env.NODE_NO_WARNINGS ??= "1";
		if (commandPath[0] === "doctor" || commandPath[0] === "completion") return;
		await ensureConfigReady({
			runtime: defaultRuntime,
			commandPath
		});
		if (PLUGIN_REQUIRED_COMMANDS.has(commandPath[0])) ensurePluginRegistryLoaded();
	});
}

//#endregion
//#region ../../src/cli/program/build-program.ts
function buildProgram() {
	const program = new Command();
	const ctx = createProgramContext();
	const argv = process.argv;
	configureProgramHelp(program, ctx);
	registerPreActionHooks(program, ctx.programVersion);
	registerProgramCommands(program, ctx, argv);
	return program;
}

//#endregion
export { buildProgram };