import { getChildLogger } from "../logging.js";
const logger = getChildLogger({ module: "feishu-probe" });
const FEISHU_API_BASES = {
    feishu: "https://open.feishu.cn/open-apis",
    lark: "https://open.larksuite.com/open-apis",
};
async function fetchWithTimeout(url, options, timeoutMs) {
    const controller = new AbortController();
    const timer = setTimeout(() => controller.abort(), timeoutMs);
    try {
        return await fetch(url, { ...options, signal: controller.signal });
    }
    finally {
        clearTimeout(timer);
    }
}
export async function probeFeishu(appId, appSecret, timeoutMs = 5000, domain) {
    const apiBase = FEISHU_API_BASES[domain ?? "feishu"];
    const started = Date.now();
    const result = {
        ok: false,
        error: null,
        elapsedMs: 0,
    };
    try {
        // Step 1: Get tenant_access_token
        const tokenRes = await fetchWithTimeout(`${apiBase}/auth/v3/tenant_access_token/internal`, {
            method: "POST",
            headers: { "Content-Type": "application/json; charset=utf-8" },
            body: JSON.stringify({ app_id: appId, app_secret: appSecret }),
        }, timeoutMs);
        const tokenJson = (await tokenRes.json());
        if (tokenJson.code !== 0 || !tokenJson.tenant_access_token) {
            result.error = tokenJson.msg || `Failed to get access token: code ${tokenJson.code}`;
            result.elapsedMs = Date.now() - started;
            return result;
        }
        const accessToken = tokenJson.tenant_access_token;
        // Step 2: Get bot info
        const botRes = await fetchWithTimeout(`${apiBase}/bot/v3/info`, {
            method: "GET",
            headers: {
                Authorization: `Bearer ${accessToken}`,
            },
        }, timeoutMs);
        const botJson = (await botRes.json());
        if (botJson.code !== 0) {
            result.error = botJson.msg || `Failed to get bot info: code ${botJson.code}`;
            result.elapsedMs = Date.now() - started;
            return result;
        }
        result.ok = true;
        result.bot = {
            appId: appId,
            appName: botJson.bot?.app_name ?? null,
            avatarUrl: botJson.bot?.avatar_url ?? null,
            openId: botJson.bot?.open_id ?? null,
        };
        result.elapsedMs = Date.now() - started;
        return result;
    }
    catch (err) {
        logger.debug?.(`Feishu probe failed: ${String(err)}`);
        return {
            ...result,
            error: err instanceof Error ? err.message : String(err),
            elapsedMs: Date.now() - started,
        };
    }
}
