import { init_paths, resolveConfigPath } from "../../../../src/config/paths.js";
import { listWeixinAccountIds, resolveWeixinAccount } from "../auth/accounts.js";
import { mergeWeixinConfigAfterLogin, runWeixinQrLoginCli } from "../cli/qr-login.js";
import { confirm } from "@inquirer/prompts";
//#region extensions/weixin/src/adapters/onboard-cli.ts
/**
* Weixin interactive onboarding — {@link ChannelOnboardAdapter} (QR scan).
*/
init_paths();
function isWeixinOnboardConfigured(config) {
	const ids = listWeixinAccountIds(config);
	for (const id of ids) try {
		if (resolveWeixinAccount(config, id).configured) return true;
	} catch {}
	return false;
}
function resolveCliConfigPath() {
	return process.env.XOPC_CONFIG_PATH?.trim() || process.env.XOPC_CONFIG?.trim() || resolveConfigPath();
}
async function configureWeixin(config) {
	console.log(`\n${"=".repeat(50)}`);
	console.log("💬 Weixin (WeChat ilink)");
	console.log(`${"=".repeat(50)}\n`);
	if (isWeixinOnboardConfigured(config)) {
		if (!await confirm({
			message: "Weixin appears configured. Scan QR again to replace login?",
			default: false
		})) {
			console.log("ℹ️  Weixin onboarding skipped.");
			return config;
		}
	} else if (!await confirm({
		message: "Log in with WeChat using a QR scan?",
		default: true
	})) {
		console.log("ℹ️  Weixin onboarding skipped.");
		console.log("   Configure later with: xopc channels login --channel weixin\n");
		return config;
	}
	const result = await runWeixinQrLoginCli({
		configPath: resolveCliConfigPath(),
		writeConfig: true,
		existingConfig: config
	});
	if (!result.ok || !result.accountId) {
		console.log(`\n⚠️  ${result.message || "Weixin login did not complete."}`);
		console.log("   You can retry from channel setup or run: xopc channels login --channel weixin\n");
		return config;
	}
	const merged = mergeWeixinConfigAfterLogin(config, result.accountId);
	console.log("\n✅ Weixin onboarding complete\n");
	return merged;
}
const weixinOnboardAdapter = {
	isConfigured: isWeixinOnboardConfigured,
	configure: configureWeixin
};
//#endregion
export { isWeixinOnboardConfigured, weixinOnboardAdapter };

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