import fs from "node:fs";
import path from "node:path";
import { parseRemoteCsv } from "../accessPolicy.js";
import { authRoot, listStoredAccounts } from "./openclawClient.js";
export function readWeChatOpenClawConfig(runtime) {
    return {
        enabled: readBoolean("DEEPSEEKCODE_WECHAT_OPENCLAW_ENABLED", true),
        accountId: readOptional("DEEPSEEKCODE_WECHAT_ACCOUNT_ID"),
        apiBaseUrl: readOptional("DEEPSEEKCODE_WECHAT_API_BASE_URL") ?? "https://ilinkai.weixin.qq.com",
        cdnBaseUrl: readOptional("DEEPSEEKCODE_WECHAT_CDN_BASE_URL") ?? "https://novac2c.cdn.weixin.qq.com/c2c",
        allowedUsers: parseRemoteCsv(process.env.DEEPSEEKCODE_WECHAT_ALLOWED_USERS),
        allowedGroups: parseRemoteCsv(process.env.DEEPSEEKCODE_WECHAT_ALLOWED_GROUPS),
        projectRoots: parseRemoteCsv(process.env.DEEPSEEKCODE_WECHAT_PROJECT_ROOTS),
        mentionNames: unique(parseRemoteCsv(process.env.DEEPSEEKCODE_WECHAT_MENTION_NAMES).concat([
            "DeepSeekCode",
            "deepseekcode",
        ])),
        qrPollIntervalMs: readPositiveInt("DEEPSEEKCODE_WECHAT_QR_POLL_INTERVAL_MS", 2000),
        longPollTimeoutMs: readPositiveInt("DEEPSEEKCODE_WECHAT_LONG_POLL_TIMEOUT_MS", 35000),
    };
}
export function formatWeChatOpenClawStatus(runtime) {
    const config = readWeChatOpenClawConfig(runtime);
    const accounts = listStoredAccounts(authRoot(runtime.dataDir));
    const selected = config.accountId ?? accounts.at(-1)?.accountId;
    const selectedAccount = selected ? accounts.find((account) => account.accountId === selected) : undefined;
    return [
        "WeChat OpenClaw remote control",
        `enabled=${config.enabled}`,
        `login=${selectedAccount ? "logged-in" : "needs login"}`,
        `account=${selectedAccount?.accountId ?? selected ?? "none"}`,
        `project=${runtime.projectPath}`,
        `apiBaseUrl=${config.apiBaseUrl}`,
        `cdnBaseUrl=${config.cdnBaseUrl}`,
        `allowedUsers=${config.allowedUsers.length || "all-private"}`,
        `allowedGroups=${config.allowedGroups.length || "mention-or-slash"}`,
        `projectRoots=${config.projectRoots.length ? config.projectRoots.join("; ") : runtime.projectPath}`,
    ].join("\n");
}
export function hasWeChatOpenClawLogin(runtime) {
    return listStoredAccounts(authRoot(runtime.dataDir)).length > 0;
}
function readOptional(name) {
    const value = process.env[name]?.trim();
    return value || undefined;
}
function readBoolean(name, fallback) {
    const value = readOptional(name);
    if (!value)
        return fallback;
    return !["0", "false", "off", "no"].includes(value.toLowerCase());
}
function readPositiveInt(name, fallback) {
    const value = Number(readOptional(name));
    return Number.isFinite(value) && value > 0 ? Math.floor(value) : fallback;
}
function unique(values) {
    return [...new Set(values.filter(Boolean))];
}
export function ensureWeChatAuthDir(runtime) {
    const root = authRoot(runtime.dataDir);
    fs.mkdirSync(path.join(root, "accounts"), { recursive: true });
    return root;
}
