import { callGraph } from "../lib/graph-client.js";
import { log } from "../lib/log.js";
/**
 * Health probe: answers "did account X auth?" + "is the token in the
 * refresh window?" without grepping logs. If the account isn't registered,
 * returns `registered: false` rather than throwing.
 */
export async function runMailboxInfo(config) {
    const blob = config.tokenStore.read();
    const startMs = Date.now();
    if (!blob) {
        log({
            event: "mailbox-info",
            account: config.accountId,
            tokenWithinRefreshWindow: false,
            folderCount: 0,
        });
        return {
            registered: false,
            graphUserId: null,
            scopes: [],
            tokenExpSec: null,
            refreshTokenExpSec: null,
            tokenWithinRefreshWindow: false,
            folderCount: null,
        };
    }
    let folderCount = null;
    try {
        const result = await callGraph(config, async (client) => {
            return (await client.api("/me/mailFolders").top(50).select("id").get());
        }, { tool: "mailbox-info" });
        folderCount = result.value.length;
    }
    catch {
        // Don't throw — operator should still see the auth state even if Graph
        // is unreachable. folderCount stays null.
        folderCount = null;
    }
    const tokenWithinRefreshWindow = config.tokenStore.needsRefresh();
    log({
        event: "mailbox-info",
        account: config.accountId,
        tokenWithinRefreshWindow,
        folderCount: folderCount ?? -1,
        elapsedMs: Date.now() - startMs,
    });
    return {
        registered: true,
        graphUserId: blob.graphUserId,
        scopes: blob.scopes,
        tokenExpSec: Math.floor(blob.accessTokenExpiry / 1000),
        refreshTokenExpSec: Math.floor(blob.refreshTokenExpiry / 1000),
        tokenWithinRefreshWindow,
        folderCount,
    };
}
//# sourceMappingURL=mailbox-info.js.map