import "./paths-mmi2xb99.js";
import { d as isRich, f as theme, o as setVerbose } from "./globals-D5pM_phR.js";
import "./utils-3Lue6wVm.js";
import { c as defaultRuntime } from "./subsystem-_lp1q2Mi.js";
import "./boolean-BhvV-zDA.js";
import { t as isTruthyEnvValue } from "./env-aEqmsXHj.js";
import "./chrome-7Zn7OYzP.js";
import "./brew-B4JBRY5K.js";
import "./runtime-guard-taA7D64B.js";
import "./loader-364nXh92.js";
import "./logging-D4aavsm_.js";
import { c as getVerboseFlag, i as getCommandPath, u as hasHelpOrVersion } from "./register.subclis-Dbev_opC.js";
import { I as VERSION } from "./config-VoJVjSgT.js";
import { a as formatCliBannerLine, i as emitCliBanner, o as hasEmittedCliBanner, r as registerProgramCommands, t as ensureConfigReady } from "./config-guard-DnQ7spuS.js";
import "./agent-scope-u760_XKU.js";
import "./exec-NeyMP-Tl.js";
import "./edwinpai-root-fGTEvnPp.js";
import "./model-selection-CMHF17-l.js";
import "./github-copilot-token-Bu-iLSUh.js";
import { n as replaceCliName, r as resolveCliName } from "./command-format-D8drL8c2.js";
import "./manifest-registry-Cobl4jxs.js";
import "./access-policy-Bz60GPHK.js";
import "./backend-config-CnwghGiS.js";
import "./public-policy-BcsEQrNF.js";
import "./internal-C9ERe0oQ.js";
import "./files-C9aOMd12.js";
import "./client-BdG39O7o.js";
import "./tailnet-D1VxJifZ.js";
import "./call-BuuyfSRn.js";
import "./message-channel-B4I_vLJb.js";
import "./ws-CvzJ5P1j.js";
import "./sandbox-Cnho92pr.js";
import "./deliver-CDl0wl5Y.js";
import "./plugins-B3jBOs5H.js";
import "./accounts-DmCFbO1K.js";
import "./dispatcher-Bo-53pRv.js";
import "./pi-model-discovery-DU5m2tTc.js";
import "./routes--IwjIOyE.js";
import "./pi-embedded-helpers-bcXOORS6.js";
import "./skills-DMXslih8.js";
import "./server-context-CysGziRI.js";
import "./ports-CgQyR_CW.js";
import "./paths-CzSw2xi4.js";
import "./image-Behi5-Q5.js";
import "./exec-approvals-HMeMyfFd.js";
import "./nodes-screen-BCmLkGEF.js";
import "./redact-DKKIzZKH.js";
import "./tool-display-CEyvKqx1.js";
import "./control-service-BCE6PKvq.js";
import "./restart-sentinel-V09ETbcM.js";
import "./channel-selection-nnjP4eS9.js";
import "./channel-summary-b-OepsoT.js";
import "./commands-CYbojHvd.js";
import "./pairing-store-BHWTx_J7.js";
import "./login-qr-C0aXNPXR.js";
import { t as formatDocsLink } from "./links-BKmnIRTU.js";
import "./progress-DIssriZ5.js";
import { n as ensurePluginRegistryLoaded } from "./command-options-r41gHMe0.js";
import "./helpers-C7AMac6B.js";
import "./onboard-helpers-CSsQgmZN.js";
import "./note-B0WhCXlw.js";
import "./clack-prompter-CwHL0EGc.js";
import "./agents.config-BoatZ0qi.js";
import "./onboard-skills-CoK0dkQn.js";
import "./onboard-channels-Del21pjv.js";
import "./shared-BKEXeTXq.js";
import "./wsl-BFoXHNBM.js";
import "./plugin-auto-enable-oOj_liKU.js";
import "./archive-wBMTPd1d.js";
import "./installs-BC5L3cvh.js";
import "./health-format-C1q5vlEI.js";
import "./update-runner-DOoc5PX2.js";
import "./onboarding-bCbVdsAe.js";
import "./tailscale-9brshfui.js";
import "./net-BImkhihx.js";
import "./auth-DQvzz4Ac.js";
import "./audit-C9c7n4Gt.js";
import "./table-tCr5riS6.js";
import "./skills-status-BdfabRbZ.js";
import "./service-BssQxmRt.js";
import "./systemd-pzbs8ZOA.js";
import "./service-audit-Dvc0hKtz.js";
import "./node-service-DanQ7J9k.js";
import "./channels-status-issues-CbhkLB9h.js";
import "./status.update-fpLgJ3ZG.js";
import "./gateway-rpc-DDaEENUI.js";
import "./help-format-BhVuFSZT.js";
import "./disciplines-9fVGWq-P.js";
import "./deps-BDkDEJDb.js";
import "./agent-AE36cAfG.js";
import "./configure-ix33PxpS.js";
import "./daemon-runtime-CEk_30Dc.js";
import "./service-env-BnMzE8Cz.js";
import "./systemd-linger-B_hlqS-Z.js";
import "./widearea-dns-BnBAD9pa.js";
import "./auth-health-953re2h8.js";
import "./doctor-BkGVznVe.js";
import "./completion-cli-B_rAu7gA.js";
import "./systemd-hints-CzgT_W5U.js";
import "./response-EhRrUCse.js";
import "./shared-CdxZFobZ.js";
import "./runners-tzpWaOR5.js";
import "./status-J7E8eYs2.js";
import "./hooks-status-TRRghkID.js";
import "./tui-N6eeHe7P.js";
import "./auth-token-DdM6zbgB.js";
import { n as resolveCliChannelOptions } from "./channel-options-CpWsJeZ8.js";
import { t as forceFreePort } from "./ports-B6g6tY2d.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 };