import { confirm, input, select } from "@inquirer/prompts";
//#region extensions/telegram/src/adapters/onboard-cli.ts
/**
* Telegram interactive onboarding (CLI onboard) — {@link ChannelOnboardAdapter}.
*/
function isTelegramConfigured(config) {
	const telegram = config.channels?.telegram;
	if (!telegram) return false;
	const accounts = telegram.accounts;
	if (accounts) for (const account of Object.values(accounts)) {
		if (account?.enabled === false) continue;
		if (typeof account.botToken === "string" && account.botToken.trim()) return true;
	}
	return false;
}
function detectEnvToken() {
	return process.env.TELEGRAM_BOT_TOKEN?.trim() || null;
}
async function promptBotToken(config) {
	const envToken = detectEnvToken();
	const existing = ((config.channels?.telegram)?.accounts)?.default?.botToken?.trim();
	if (envToken && !existing) {
		if (await confirm({
			message: "TELEGRAM_BOT_TOKEN is set in the environment. Use it?",
			default: true
		})) return envToken;
	}
	if (typeof existing === "string" && existing) {
		if (await confirm({
			message: "A Telegram bot token is already configured. Keep it?",
			default: true
		})) return existing;
	}
	console.log("\n📝 Telegram Bot Token:");
	console.log("   1. Open Telegram and chat with @BotFather");
	console.log("   2. Send /newbot and follow the prompts");
	console.log("   3. Copy the token BotFather gives you\n");
	return (await input({
		message: "Enter bot token:",
		validate: (v) => v.trim().length > 0 || "Token cannot be empty"
	})).trim();
}
async function promptDmPolicy() {
	return select({
		message: "DM (private chat) policy:",
		choices: [
			{
				value: "pairing",
				name: "pairing  [recommended] verify new users with /pair",
				description: "New users must send /pair before chatting"
			},
			{
				value: "allowlist",
				name: "allowlist   allowlisted users only",
				description: "Only listed users can message the bot"
			},
			{
				value: "open",
				name: "open        anyone can DM (not recommended)",
				description: "Any user can start a conversation"
			},
			{
				value: "disabled",
				name: "disabled    DMs disabled",
				description: "The bot does not respond to private messages"
			}
		],
		default: "pairing"
	});
}
async function promptAllowlist(message) {
	const value = await input({
		message,
		default: ""
	});
	if (!value.trim()) return [];
	return value.split(/[,\s\n]+/).map((s) => s.trim()).filter(Boolean).map((e) => {
		const num = parseInt(e, 10);
		return !isNaN(num) && String(num) === e ? num : e;
	});
}
async function promptGroupPolicy() {
	return select({
		message: "Group chat policy:",
		choices: [
			{
				value: "open",
				name: "open       all groups allowed",
				description: "The bot can be used in any group"
			},
			{
				value: "disabled",
				name: "disabled   groups disabled",
				description: "The bot does not respond in groups"
			},
			{
				value: "allowlist",
				name: "allowlist  allowlisted groups only",
				description: "Only listed group chats can use the bot"
			}
		],
		default: "open"
	});
}
async function configureTelegram(config) {
	console.log(`\n${"=".repeat(50)}`);
	console.log("📱 Telegram setup");
	console.log(`${"=".repeat(50)}\n`);
	const botToken = await promptBotToken(config);
	if (!botToken) {
		console.log("⚠️ No bot token; skipping Telegram setup.");
		return config;
	}
	const dmPolicy = await promptDmPolicy();
	let allowFrom;
	if (dmPolicy === "allowlist") allowFrom = await promptAllowlist("User IDs or usernames allowed for DMs (comma-separated):");
	const groupPolicy = await promptGroupPolicy();
	let groupAllowFrom;
	if (groupPolicy === "allowlist") groupAllowFrom = await promptAllowlist("Allowed group chat IDs (comma-separated):");
	const prev = config.channels?.telegram;
	const prevAccounts = prev?.accounts && typeof prev.accounts === "object" && !Array.isArray(prev.accounts) ? { ...prev.accounts } : {};
	const prevDefault = prevAccounts.default && typeof prevAccounts.default === "object" && !Array.isArray(prevAccounts.default) ? { ...prevAccounts.default } : {};
	const telegramConfig = {
		...prev ?? {},
		enabled: true,
		dmPolicy,
		groupPolicy,
		allowFrom: allowFrom ?? prev?.allowFrom ?? [],
		groupAllowFrom: groupAllowFrom ?? prev?.groupAllowFrom ?? [],
		accounts: {
			...prevAccounts,
			default: {
				...prevDefault,
				accountId: "default",
				enabled: true,
				botToken,
				dmPolicy,
				groupPolicy,
				allowFrom: allowFrom ?? prevDefault.allowFrom ?? [],
				groupAllowFrom: groupAllowFrom ?? prevDefault.groupAllowFrom ?? []
			}
		}
	};
	const newConfig = {
		...config,
		channels: {
			...config.channels,
			telegram: telegramConfig
		}
	};
	console.log("\n✅ Telegram configuration complete\n");
	return newConfig;
}
const telegramOnboardAdapter = {
	isConfigured: isTelegramConfigured,
	configure: configureTelegram
};
/** Optional entry: confirm before running the full Telegram configure flow. */
async function setupTelegramOnboard(config, options = {}) {
	if (!await confirm({
		message: options.confirmMessage || "Enable Telegram channel?",
		default: options.confirmDefault ?? true
	})) {
		console.log("ℹ️ Telegram skipped.");
		return config;
	}
	return configureTelegram(config);
}
//#endregion
export { setupTelegramOnboard, telegramOnboardAdapter };

//# sourceMappingURL=onboard-cli.js.map