import { normalizeAccountId } from "../routing/session-key.js";
export function listFeishuAccountIds(cfg) {
    const feishuCfg = cfg.channels?.feishu;
    const accounts = feishuCfg?.accounts;
    if (!accounts) {
        if (process.env.FEISHU_APP_ID)
            return ["default"];
        return [];
    }
    return Object.keys(accounts);
}
export function resolveDefaultFeishuAccountId(cfg) {
    const ids = listFeishuAccountIds(cfg);
    return ids[0] ?? "default";
}
export function resolveFeishuAccount(params) {
    const { cfg, accountId } = params;
    const feishuCfg = cfg.channels?.feishu;
    const resolvedId = accountId ? normalizeAccountId(accountId) : resolveDefaultFeishuAccountId(cfg);
    let config = { appId: "", appSecret: "" };
    let tokenSource = "none";
    let enabled = true;
    let name;
    if (feishuCfg?.accounts?.[resolvedId]) {
        config = feishuCfg.accounts[resolvedId];
        tokenSource = "config";
        name = config.botName;
    }
    else if (resolvedId === "default" &&
        process.env.FEISHU_APP_ID &&
        process.env.FEISHU_APP_SECRET) {
        const envDomain = process.env.FEISHU_DOMAIN?.toLowerCase();
        config = {
            appId: process.env.FEISHU_APP_ID,
            appSecret: process.env.FEISHU_APP_SECRET,
            domain: envDomain === "lark" ? "lark" : undefined,
        };
        tokenSource = "env";
    }
    if (config.appId && config.appSecret) {
        // ok
    }
    else {
        tokenSource = "none";
    }
    return {
        accountId: resolvedId,
        config,
        tokenSource,
        name,
        enabled,
    };
}
