import { ImapFlow } from "imapflow";
import type { EmailConfig } from "./credentials.js";
export interface EmailMessage {
    uid: number;
    messageId: string | null;
    inReplyTo: string | null;
    from: string;
    subject: string;
    date: string;
    bodyPreview: string;
}
export interface PaginatedMessages {
    messages: EmailMessage[];
    /** Pass as before_uid to get the next page of older messages. Omitted when results are exhausted. */
    nextBeforeUid?: number;
}
export declare const MAX_LIMIT = 50;
/**
 * Resolve an IMAP folder name from a logical folder key.
 * - "inbox" → "INBOX"
 * - "sent" → discovered via \Sent special-use flag (RFC 6154)
 *
 * Throws if the requested folder cannot be found on this server.
 */
export declare function resolveFolderName(client: ImapFlow, folder: "inbox" | "sent"): Promise<string>;
/**
 * Strip HTML tags and decode common entities.
 * Used for extracting readable text from HTML-only email bodies.
 */
export declare function stripHtml(html: string): string;
/**
 * Decode a raw IMAP body part buffer according to its transfer encoding.
 * Returns a UTF-8 string. Falls back to raw UTF-8 for unknown encodings.
 */
export declare function decodeBodyPart(buf: Buffer, encoding?: string): string;
/**
 * Extract a text body preview from a fetched message's inline bodyParts + bodyStructure.
 *
 * Determines the content type and transfer encoding of part "1" from
 * bodyStructure (single-part: structure itself, multipart: first childNode).
 * Skips non-text parts and multipart containers. Strips HTML. Truncates
 * to BODY_PREVIEW_LENGTH characters.
 */
export declare function extractBodyFromFetch(msg: {
    bodyParts?: Map<string, Buffer>;
    bodyStructure?: {
        type?: string;
        encoding?: string;
        childNodes?: Array<{
            type?: string;
            encoding?: string;
        }>;
    };
}): string;
/**
 * Test IMAP connectivity and authentication.
 * Returns null on success, error message on failure.
 */
export declare function testConnection(config: EmailConfig, password: string): Promise<string | null>;
/**
 * Check whether an IMAP envelope's TO field contains the target address.
 * Case-insensitive comparison. Strict: if the server didn't return TO
 * addresses, the message is excluded — we cannot confirm it was addressed
 * to the target.
 */
export declare function matchesRecipient(envelope: {
    to?: Array<{
        name?: string;
        address?: string;
    }>;
}, targetAddress: string): boolean;
/**
 * Build IMAP SEARCH criteria for fetchRecent.
 * Never includes TO — that filter is applied client-side to avoid
 * full-mailbox header scans that timeout on large mailboxes.
 */
export declare function buildFetchRecentCriteria(options: {
    sender?: string;
    since?: Date;
    before?: Date;
    to?: string;
    limit?: number;
}): {
    criteria: Record<string, unknown>;
    clientSideTo: string | undefined;
    fetchLimit: number;
};
/**
 * Build IMAP SEARCH criteria for searchMessages.
 * Never includes TO — that filter is applied client-side.
 * When TO was the only filter (no other criteria provide bounding),
 * injects a since floor to prevent unbounded full-mailbox scans.
 */
export declare function buildSearchCriteria(options: {
    sender?: string;
    subject?: string;
    body?: string;
    since?: Date;
    before?: Date;
    to?: string;
}): {
    criteria: Record<string, unknown>;
    clientSideTo: string | undefined;
};
/**
 * Fetch recent messages from a mailbox folder.
 * Returns the most recent N messages with cursor-based pagination.
 *
 * TO filtering is applied client-side (not via IMAP SEARCH) to avoid
 * full-mailbox header scans that exceed socketTimeout on large mailboxes.
 */
export declare function fetchRecent(config: EmailConfig, password: string, options?: {
    limit?: number;
    sender?: string;
    subjectPattern?: string;
    since?: Date;
    before?: Date;
    to?: string;
    folder?: "inbox" | "sent";
    beforeUid?: number;
}): Promise<PaginatedMessages>;
/**
 * Search a mailbox folder by query criteria with cursor-based pagination.
 */
export declare function searchMessages(config: EmailConfig, password: string, query: {
    sender?: string;
    subject?: string;
    body?: string;
    since?: Date;
    before?: Date;
    limit?: number;
    to?: string;
    folder?: "inbox" | "sent";
    beforeUid?: number;
}): Promise<PaginatedMessages>;
/**
 * Result from an incremental email fetch for the polling cron.
 * Includes the raw envelope data needed for graph storage (not just display).
 */
export interface FetchedEmail {
    uid: number;
    messageId: string | null;
    inReplyTo: string | null;
    toAddresses: string[];
    ccAddresses: string[];
    fromAddress: string;
    fromName: string | null;
    subject: string;
    date: string;
    bodyPreview: string;
}
/**
 * Fetch emails from INBOX since a given UID (exclusive) for incremental polling.
 * Returns the UIDVALIDITY and all matching messages with UID > sinceUid.
 *
 * When sinceUid is 0 (first poll), fetches emails from the last 7 days
 * to avoid pulling the entire mailbox on large accounts.
 *
 * Client-side TO filtering is applied when recipientAddress differs
 * from the auth email (alias/catchall scenarios).
 */
export declare function fetchSinceUid(config: EmailConfig, password: string, sinceUid: number, recipientAddress: string): Promise<{
    uidValidity: number;
    emails: FetchedEmail[];
    maxUid: number;
}>;
/**
 * Get the inbox message count.
 */
export declare function getInboxCount(config: EmailConfig, password: string): Promise<number>;
export interface AutoRespondMessage {
    uid: number;
    messageId: string;
    from: string;
    to: string[];
    subject: string;
    date: string;
    inReplyTo: string | null;
    references: string | null;
    body: string;
    /** True if the message has auto-reply indicators and should be skipped. */
    isAutoReply: boolean;
}
/**
 * Parse raw IMAP header text into a map of lowercased header names → values.
 * Handles multi-line continuation (lines starting with whitespace).
 */
export declare function parseRawHeaders(raw: string): Map<string, string[]>;
/**
 * Detect whether a message is an auto-reply based on RFC 3834 headers.
 *
 * Checks:
 * - Auto-Submitted: any value other than "no"
 * - X-Auto-Response-Suppress: present
 * - Precedence: bulk, junk, or list
 * - List-Unsubscribe or List-Id: present (mailing list)
 */
export declare function detectAutoReply(headers: Map<string, string[]>): boolean;
/**
 * Fetch new messages for auto-respond processing.
 *
 * Returns messages with UIDs greater than `sinceUid`, filtered by the
 * agent's address (TO header). Includes envelope, body, and auto-reply
 * detection headers in a single IMAP FETCH.
 *
 * Unlike fetchRecent (optimised for tool output with pagination),
 * this function fetches everything needed for auto-respond in one pass:
 * envelope for threading, body for agent input, headers for loop guards.
 */
export declare function fetchForAutoRespond(config: EmailConfig, password: string, options: {
    sinceUid: number;
    agentAddress: string;
    limit?: number;
}): Promise<AutoRespondMessage[]>;
//# sourceMappingURL=imap.d.ts.map