/**
 * Configuration schema for mailpouch
 * Covers connection settings and per-tool agentic access permissions.
 */
// ─── Tool Registry ─────────────────────────────────────────────────────────────
export const ALL_TOOLS = [
    // Sending
    "send_email", "reply_to_email", "forward_email", "send_test_email",
    // Drafts & scheduling
    "save_draft", "schedule_email", "list_scheduled_emails", "cancel_scheduled_email", "list_proton_scheduled",
    "remind_if_no_reply", "list_pending_reminders", "cancel_reminder", "check_reminders",
    // Reading
    "get_emails", "get_email_by_id", "search_emails", "get_unread_count",
    "list_labels", "get_emails_by_label", "download_attachment",
    "get_thread", "get_correspondence_profile",
    "fts_search", "fts_rebuild", "fts_status",
    "extract_action_items", "extract_meeting",
    // Folder management
    "get_folders", "sync_folders", "create_folder", "delete_folder", "rename_folder",
    // Email actions
    "mark_email_read", "star_email", "mark_answered", "mark_forwarded", "move_email", "archive_email",
    "move_to_trash", "move_to_spam", "move_to_folder",
    "bulk_mark_read", "bulk_star", "bulk_move_emails",
    "move_to_label", "bulk_move_to_label",
    "remove_label", "bulk_remove_label",
    // Deletion
    "delete_email", "bulk_delete_emails", "bulk_delete", "empty_trash",
    // Analytics
    "get_email_stats", "get_email_analytics", "get_contacts", "get_volume_trends",
    // System
    "get_connection_status", "sync_emails", "clear_cache", "get_logs", "get_server_version",
    // Bridge & server control
    "start_bridge", "shutdown_server", "restart_server",
    // SimpleLogin aliases (Proton-owned; optional — requires API key)
    "alias_list", "alias_create_random", "alias_create_custom",
    "alias_toggle", "alias_delete", "alias_get_activity",
    // Proton Pass (optional — requires pass-cli and a Personal Access Token)
    "pass_list", "pass_search", "pass_get",
];
export const TOOL_CATEGORIES = {
    sending: {
        label: "Sending",
        description: "Compose and send outbound email",
        tools: ["send_email", "reply_to_email", "forward_email", "send_test_email"],
        risk: "moderate",
    },
    drafts: {
        label: "Drafts & Scheduling",
        description: "Save drafts and schedule emails for future delivery",
        tools: [
            "save_draft", "schedule_email", "list_scheduled_emails", "cancel_scheduled_email", "list_proton_scheduled",
            "remind_if_no_reply", "list_pending_reminders", "cancel_reminder", "check_reminders",
        ],
        risk: "moderate",
    },
    reading: {
        label: "Reading",
        description: "Fetch, search, preview email content, and download attachments",
        tools: [
            "get_emails", "get_email_by_id", "search_emails", "get_unread_count",
            "list_labels", "get_emails_by_label", "download_attachment",
            "get_thread", "get_correspondence_profile",
            "fts_search", "fts_rebuild", "fts_status",
            "extract_action_items", "extract_meeting",
        ],
        risk: "safe",
    },
    folders: {
        label: "Folder Management",
        description: "List, create, rename, and delete folders",
        tools: ["get_folders", "sync_folders", "create_folder", "delete_folder", "rename_folder"],
        risk: "moderate",
    },
    actions: {
        label: "Email Actions",
        description: "Mark read/unread, star, move, label, and bulk operations",
        tools: [
            "mark_email_read", "star_email", "mark_answered", "mark_forwarded", "move_email", "archive_email",
            "move_to_trash", "move_to_spam", "move_to_folder",
            "bulk_mark_read", "bulk_star", "bulk_move_emails",
            "move_to_label", "bulk_move_to_label",
            "remove_label", "bulk_remove_label",
        ],
        risk: "moderate",
    },
    deletion: {
        label: "Deletion",
        description: "Delete emails by moving them to Trash (recoverable). Includes empty_trash, which PERMANENTLY purges the Trash mailbox — the one unrecoverable, confirm-gated exception.",
        tools: ["delete_email", "bulk_delete_emails", "bulk_delete", "empty_trash"],
        risk: "destructive",
    },
    analytics: {
        label: "Analytics",
        description: "Email statistics, volume trends, and contact insights",
        tools: ["get_email_stats", "get_email_analytics", "get_contacts", "get_volume_trends"],
        risk: "safe",
    },
    system: {
        label: "System",
        description: "Connection status, cache control, server logs, and version info",
        tools: ["get_connection_status", "sync_emails", "clear_cache", "get_logs", "get_server_version"],
        risk: "safe",
    },
    bridge_control: {
        label: "Bridge & Server Control",
        description: "Start Proton Bridge, shut down, or restart the MCP server",
        tools: ["start_bridge", "shutdown_server", "restart_server"],
        risk: "destructive",
    },
    aliases: {
        label: "SimpleLogin Aliases",
        description: "Create and manage SimpleLogin aliases (Proton-owned alias service; requires API key)",
        tools: [
            "alias_list", "alias_create_random", "alias_create_custom",
            "alias_toggle", "alias_delete", "alias_get_activity",
        ],
        risk: "moderate",
    },
    pass: {
        label: "Proton Pass",
        description: "Retrieve credentials from Proton Pass via pass-cli (requires a Personal Access Token).",
        tools: ["pass_list", "pass_search", "pass_get"],
        risk: "moderate",
    },
};
export const PERMISSION_PRESETS = ["full", "read_only", "supervised", "send_only", "custom"];
/**
 * Minimum Proton Bridge version the MCP server targets.
 * Bumped when Proton ships security-relevant Bridge changes (e.g. v3.21.2
 * strict TLS validation, v3.22.0 FIDO2 + 50 MB import cap). Detected at
 * startup via the IMAP ID command; running an older Bridge logs a warning
 * but does not block connection.
 */
export const BRIDGE_MIN_VERSION = "3.22.0";
export const DEFAULT_RESPONSE_LIMITS = {
    maxResponseBytes: 900 * 1024, // 900 KB
    maxEmailBodyChars: 500_000,
    maxEmailListResults: 50,
    maxAttachmentBytes: 600_000, // ~440 KB raw → ~600 KB base64
    warnOnLargeResponse: true,
};
// ─── Top-Level Config ──────────────────────────────────────────────────────────
/**
 * Config schema version.
 *   v1 → pre-2026-04 — no explicit insecure-Bridge opt-in (TLS validation was
 *        silently disabled when no cert was configured).
 *   v2 → 2026-04 hardening — allowInsecureBridge is required to keep the legacy
 *        behavior. v1 configs are grandfathered in the loader with a warning.
 *   v3 → 2026-04 multi-account — adds accounts[] + activeAccountId. Legacy
 *        configs auto-migrate: the top-level connection fields are promoted
 *        into a "primary" account on first read and mirrored back on each
 *        save so single-account consumers keep working during the transition.
 */
export const CONFIG_VERSION = 3;
/** Tools that mutate or destroy Proton-side state and require { confirmed: true }. */
export const DESTRUCTIVE_TOOLS = new Set([
    "delete_email",
    "bulk_delete",
    "bulk_delete_emails",
    "empty_trash",
    "delete_folder",
    "move_to_trash",
    "move_to_spam",
    "alias_delete",
    "pass_get",
    "shutdown_server",
    "restart_server",
]);
/**
 * Tool-name aliases. PERM-003 (audit 2026-05-28): `bulk_delete` and
 * `bulk_delete_emails` resolve to the same handler in src/tools/deletion.ts,
 * but the permission gate keys rate buckets by raw tool name — letting an
 * agent double the destruction throughput the operator configured by
 * alternating between the two names. Canonicalize at every gate
 * (rate-limit bucket, destructive-confirm, per-tool enabled flag) so the
 * operator's intent applies regardless of which alias the caller used.
 *
 * Keys are aliases; values are the canonical name. Stays small — only add
 * an entry when two tool names truly share a handler.
 */
export const TOOL_ALIASES = Object.freeze({
    bulk_delete: "bulk_delete_emails",
});
/** Resolve a tool name through TOOL_ALIASES; returns the canonical name. */
export function canonicalToolName(name) {
    return TOOL_ALIASES[name] ?? name;
}
/**
 * Move-style tools that, when their target folder is destructive (Trash or
 * Spam), should be gated by the destructive-confirm flow the same way
 * `move_to_trash` / `move_to_spam` already are. PERM-004 (audit 2026-05-28):
 * without this, calling `move_email { targetFolder: "Trash" }` bypassed
 * destructive-confirm even though `move_to_trash` did not.
 */
export const MOVE_TOOLS_WITH_DESTRUCTIVE_TARGET = new Set([
    "move_email",
    "bulk_move_emails",
    "move_to_folder",
]);
/** Destination folder names that count as destructive. Case-insensitive comparison. */
export const DESTRUCTIVE_DESTINATIONS = new Set([
    "trash",
    "spam",
]);
/**
 * Where each category surfaces first.
 *
 * Actual tool counts per tier (cumulative, including the 3 always-available
 * meta-tools: setup_status + request_permission_escalation + check_escalation_status):
 *   core     — 27 categorized (reading 14 + sending 4 + analytics 4 + system 5)            + 3 meta = 30 visible
 *   extended — 66 categorized (core 27 + drafts 9 + folders 5 + actions 16 + aliases 6 + pass 3) + 3 = 69 visible
 *   complete — 73 categorized (extended 66 + deletion 4 + bridge_control 3)                       + 3 = 76 visible
 */
export const TOOL_CATEGORY_TIER = {
    reading: "core", // reading is the 80 % use case
    sending: "core", // sending needs to be available in core too — common ask
    analytics: "core", // analytics is read-only and small
    system: "core", // connection status, cache, logs
    drafts: "extended",
    folders: "extended",
    actions: "extended",
    aliases: "extended", // SimpleLogin; optional (requires API key), moderate risk
    pass: "extended", // Proton Pass; optional (requires PAT + pass-cli), moderate risk
    deletion: "complete", // destructive + rarely needed by casual agents
    bridge_control: "complete", // server lifecycle
};
/**
 * Always-available meta-tools — they bypass the permission gate, sit outside
 * the category registry, and ignore the tiering system (visible at every tier):
 *   setup_status                  — read-only install/connect diagnostic (CALL FIRST)
 *   request_permission_escalation — ask a human for a higher preset
 *   check_escalation_status       — poll an escalation request
 * None can GRANT access; they only report or request.
 */
export const ALWAYS_AVAILABLE_TOOLS = new Set([
    "setup_status",
    "request_permission_escalation",
    "check_escalation_status",
]);
/** Resolve the set of tools that should be exposed by ListTools for a given tier. */
export function toolsForTier(tier) {
    const tiersIncluded = tier === "core" ? ["core"] :
        tier === "extended" ? ["core", "extended"] :
            ["core", "extended", "complete"];
    const result = new Set();
    for (const [cat, catTier] of Object.entries(TOOL_CATEGORY_TIER)) {
        if (tiersIncluded.includes(catTier)) {
            const def = TOOL_CATEGORIES[cat];
            if (def)
                for (const tool of def.tools)
                    result.add(tool);
        }
    }
    // Always-available tools are added regardless of tier.
    for (const tool of ALWAYS_AVAILABLE_TOOLS)
        result.add(tool);
    return result;
}
/** Parse a value into a ToolTier, defaulting to "complete" on anything else. */
export function parseToolTier(value) {
    if (value === "core" || value === "extended" || value === "complete")
        return value;
    return "complete";
}
//# sourceMappingURL=schema.js.map