import "./paths-D4_LjrY7.js";
import "./utils-BnlaT0eO.js";
import { r as theme } from "./theme-CG-K7m8u.js";
import "./subsystem-ocBR131w.js";
import { n as defaultRuntime } from "./runtime-CRvfzclG.js";
import "./exec-CZr_EJHt.js";
import "./agent-scope-CVnlY0oG.js";
import "./model-selection-p6J3l4qF.js";
import "./github-copilot-token-CjSKohyA.js";
import "./emitter-oHo8y9ow.js";
import "./sqlite-wYNyDR1B.js";
import "./reliability-Dl7hbRzF.js";
import "./boolean-DGGu1sGA.js";
import "./env-CvUd308i.js";
import "./validation-p1IK70TM.js";
import { d as writeConfigFile, l as readConfigFileSnapshotForWrite, o as loadConfig } from "./config-DJ3A5x_1.js";
import "./manifest-registry-U2EDFVP4.js";
import "./redact-DcYCKU6u.js";
import "./errors-DbYSuCgv.js";
import { n as runCommandWithRuntime } from "./cli-utils-Cps3tWFt.js";
import { t as formatHelpExamples } from "./help-format-DukcHIxz.js";

//#region src/cli/program/register.channel.ts
function extractClientIdFromToken(token) {
	try {
		const base64 = token.split(".")[0];
		if (!base64) return;
		const decoded = Buffer.from(base64, "base64").toString("utf-8");
		return /^\d+$/.test(decoded) ? decoded : void 0;
	} catch {
		return;
	}
}
async function channelAddDiscord(opts) {
	const { token, accountName, agent } = opts;
	const runtime = defaultRuntime;
	const cfg = loadConfig();
	const { writeOptions } = await readConfigFileSnapshotForWrite();
	const accountPatch = {
		token,
		enabled: true
	};
	const next = {
		...cfg,
		channels: {
			...cfg.channels,
			discord: {
				...cfg.channels?.discord,
				enabled: true,
				accounts: {
					...cfg.channels?.discord?.accounts,
					[accountName]: {
						...cfg.channels?.discord?.accounts?.[accountName],
						...accountPatch
					}
				}
			}
		}
	};
	if (agent) {
		const bindings = [...next.bindings ?? []];
		bindings.push({
			agentId: agent,
			match: {
				channel: "discord",
				accountId: accountName
			}
		});
		next.bindings = bindings;
	}
	await writeConfigFile(next, writeOptions);
	runtime.log?.(`${theme.success("✓")} Added Discord account "${accountName}"`);
	if (agent) runtime.log?.(`  Bound to agent "${agent}"`);
	const clientId = extractClientIdFromToken(token);
	if (clientId) runtime.log?.(`  Invite URL: https://discord.com/oauth2/authorize?client_id=${clientId}&scope=bot&permissions=274877991936`);
}
function registerChannelCommands(program) {
	program.command("channel").description("Manage channel accounts and configuration").command("add").description("Add a channel account").command("discord").description("Add a Discord bot account").requiredOption("--token <token>", "Discord bot token").requiredOption("--account-name <name>", "Account ID key in config").option("--agent <agentId>", "Bind this account to an agent").addHelpText("after", () => `
${theme.heading("Examples:")}
${formatHelpExamples([["minion channel add discord --token \"Bot...\" --account-name my-bot", "Add a Discord bot account."], ["minion channel add discord --token \"Bot...\" --account-name my-bot --agent main", "Add and bind to an agent."]])}
`).action(async (opts) => {
		await runCommandWithRuntime(defaultRuntime, async () => {
			await channelAddDiscord({
				token: String(opts.token),
				accountName: String(opts.accountName),
				agent: opts.agent ? String(opts.agent) : void 0
			});
		});
	});
}

//#endregion
export { registerChannelCommands };