import { EnhancedMessage, Middleware, Context } from 'cloudflare-email-kit';

interface EmailRouterConfig {
    name: string;
}
type EmailMatcher = RegExp | string | ((message: EnhancedMessage) => Promise<boolean> | boolean);
type EmailHandler = (message: EnhancedMessage) => Promise<void> | void;
interface EmailRouteMatcher {
    name: string;
    match: (message: EnhancedMessage) => Promise<boolean> | boolean;
}
type EmailRouteRule = EmailRouteMatcher & Middleware;
interface EmailRouteHandleResult {
    matched: EmailRouteRule | null;
}

declare class EmailRouter<In extends Context = Context> implements Middleware<In> {
    protected config: EmailRouterConfig;
    protected rules: EmailRouteRule[];
    constructor(config?: EmailRouterConfig);
    get name(): string;
    /**
     * Matches the given email message against the defined rules and returns the first matching rule.
     * @param message The email message to match against the rules.
     * @returns The first matching rule or null if no rule matches the message.
     */
    checkout(message: EnhancedMessage): Promise<EmailRouteRule | null>;
    /**
     * Processes the email message and returns the result of the matching route handle.
     * @param ctx - The email context object.
     * @param next - The next middleware function.
     * @returns An object containing the matched route handle or null if no match was found.
     */
    process(ctx: In, next?: (c: In) => Promise<void>): Promise<EmailRouteHandleResult>;
    handle(ctx: In, next?: (c: In) => Promise<void>): Promise<void>;
    /**
     * Adds a new route rule to the router's list of rules.
     * The first added rule will be checked first.
     * @param matcher The matcher to use for this rule. Can be a string, a regular expression, or a function.
     * @param handler The handler to use for this rule. Can be a function handler or a middleware.
     * @returns The router itself.
     */
    match(matcher: EmailMatcher, handler: EmailHandler | Middleware): this;
    match(matcher: EmailMatcher, subrouter: EmailRouter): this;
}

declare const CATCH_ALL: () => boolean;
declare function REJECT_ALL(reason?: string): [EmailMatcher, EmailHandler];

export { CATCH_ALL, type EmailHandler, type EmailMatcher, type EmailRouteHandleResult, type EmailRouteMatcher, type EmailRouteRule, EmailRouter, type EmailRouterConfig, REJECT_ALL };
