/**
 * Standalone WeChat QR login command.
 *
 * Calls the transport-layer auth functions from `@tencent-weixin/openclaw-weixin`
 * directly, bypassing the OpenClaw plugin runtime. On success, persists the bot
 * token so the WeixinAdapter can pick it up on the next start.
 *
 * Usage:  visionclaw weixin-login [--base-url <url>]
 */
import { saveWeixinAccount, registerWeixinAccountId, DEFAULT_BASE_URL, } from "@tencent-weixin/openclaw-weixin/src/auth/accounts.js";
import { startWeixinLoginWithQr, waitForWeixinLogin, } from "@tencent-weixin/openclaw-weixin/src/auth/login-qr.js";
import { normalizeAccountId } from "openclaw/plugin-sdk";
import { configExists, loadConfig, saveConfig, getConfigDir } from "../config/index.js";
export async function runWeixinLogin(args) {
    const baseUrlIdx = args.indexOf("--base-url");
    const apiBaseUrl = baseUrlIdx !== -1 && baseUrlIdx + 1 < args.length
        ? args[baseUrlIdx + 1]
        : DEFAULT_BASE_URL;
    console.log(`\nWeChat (Weixin) QR Login`);
    console.log(`API base URL: ${apiBaseUrl}\n`);
    const startResult = await startWeixinLoginWithQr({
        apiBaseUrl,
        verbose: true,
        force: true,
    });
    if (!startResult.qrcodeUrl) {
        console.error(`Failed to start login: ${startResult.message}`);
        process.exit(1);
    }
    console.log(startResult.message);
    try {
        const qrterm = (await import("qrcode-terminal"));
        qrterm.default.generate(startResult.qrcodeUrl, { small: true });
    }
    catch {
        console.log(`QR Code URL: ${startResult.qrcodeUrl}`);
    }
    console.log("\nWaiting for scan...");
    const result = await waitForWeixinLogin({
        sessionKey: startResult.sessionKey,
        apiBaseUrl,
        verbose: true,
        timeoutMs: 480_000,
    });
    console.log(`\n${result.message}`);
    if (!result.connected || !result.accountId) {
        process.exit(1);
    }
    const accountId = normalizeAccountId(result.accountId);
    saveWeixinAccount(accountId, {
        token: result.botToken,
        baseUrl: result.baseUrl,
        userId: result.userId,
    });
    registerWeixinAccountId(accountId);
    // Enable weixin channel in VisionClaw config
    if (configExists()) {
        const config = loadConfig();
        config.channels.weixin = {
            enabled: true,
            accountId,
            baseUrl: apiBaseUrl,
            cdnBaseUrl: config.channels.weixin?.cdnBaseUrl ?? "https://novac2c.cdn.weixin.qq.com/c2c",
        };
        saveConfig(config);
        console.log(`\nWeixin channel enabled in VisionClaw config.`);
    }
    else {
        console.log(`\nNo VisionClaw config found. Enable the channel after running setup:`);
        console.log(`  Set channels.weixin.enabled = true in your config.`);
    }
    console.log(`Account saved: ${accountId}`);
    console.log(`Credentials stored in ${getConfigDir()}/openclaw-weixin/accounts/${accountId}.json`);
    console.log(`\nWeChat channel is ready. Send /restart in Telegram or restart the agent to activate.`);
}
//# sourceMappingURL=weixin-login.js.map