import { readConfig, loadCredentials } from "../lib/credentials.js";
import * as imap from "../lib/imap.js";
import * as smtp from "../lib/smtp.js";
export async function emailStatus(params) {
    const config = await readConfig(params.accountId);
    if (!config) {
        return "Email: not configured. Run email-setup to set up the agent's email account.";
    }
    const credResult = await loadCredentials(params.accountId);
    if ("error" in credResult) {
        return `Email: ${config.email} — ${credResult.error}`;
    }
    const { password } = credResult.credentials;
    // Test IMAP
    const imapError = await imap.testConnection(config, password);
    const imapStatus = imapError ? `FAIL — ${imapError}` : "OK";
    // Test SMTP
    const smtpError = await smtp.testConnection(config, password);
    const smtpStatus = smtpError ? `FAIL — ${smtpError}` : "OK";
    // Get inbox count if IMAP is healthy
    let inboxInfo = "";
    if (!imapError) {
        try {
            const count = await imap.getInboxCount(config, password);
            inboxInfo = `\nInbox: ${count} messages`;
        }
        catch {
            inboxInfo = "\nInbox: could not retrieve count";
        }
    }
    const aliasLine = config.agentAddress !== config.email
        ? `Agent address: ${config.agentAddress} (auth: ${config.email})`
        : `Email: ${config.email}`;
    return [
        aliasLine,
        `IMAP: ${config.imapHost}:${config.imapPort} (${config.imapSecurity}) — ${imapStatus}`,
        `SMTP: ${config.smtpHost}:${config.smtpPort} (${config.smtpSecurity}) — ${smtpStatus}`,
        inboxInfo,
    ]
        .filter(Boolean)
        .join("\n");
}
//# sourceMappingURL=email-status.js.map