/**
 * @fileoverview MCP Engine for caching MCP server, connection, and tool data
 *
 * Provides centralized caching for MCP (Model Context Protocol) entities.
 * Execution logs are NOT cached here - they should be loaded on-demand via RunView.
 *
 * @module @memberjunction/core-entities/MCPEngine
 */
import { BaseEngine } from "@memberjunction/core";
import { UUIDsEqual } from "@memberjunction/global";
/**
 * MCPEngine provides centralized caching for MCP-related entities.
 *
 * Cached entities:
 * - MCP Servers: Server definitions with transport, auth, and rate limit config
 * - MCP Server Connections: Connection instances for each server
 * - MCP Server Tools: Tools discovered from MCP servers
 *
 * NOT cached (load on-demand):
 * - MCP Tool Execution Logs: Historical data that should be queried as needed
 *
 * @example
 * ```typescript
 * // Initialize the engine
 * await MCPEngine.Instance.Config(false, contextUser);
 *
 * // Access cached data
 * const servers = MCPEngine.Instance.Servers;
 * const connections = MCPEngine.Instance.Connections;
 * const tools = MCPEngine.Instance.Tools;
 *
 * // Get tools for a specific server
 * const serverTools = MCPEngine.Instance.GetToolsByServer(serverId);
 *
 * // Force refresh
 * await MCPEngine.Instance.Config(true, contextUser);
 * ```
 */
export class MCPEngine extends BaseEngine {
    constructor() {
        super(...arguments);
        // ========================================
        // Private Storage
        // ========================================
        this._Servers = [];
        this._Connections = [];
        this._Tools = [];
        this._Favorites = [];
    }
    /**
     * Configures and loads the MCP engine data.
     *
     * @param forceRefresh - If true, forces a refresh of cached data
     * @param contextUser - User context for data loading (required for server-side)
     * @param provider - Optional metadata provider
     */
    async Config(forceRefresh, contextUser, provider) {
        const configs = [
            {
                Type: 'entity',
                EntityName: 'MJ: MCP Servers',
                PropertyName: '_Servers',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: MCP Server Connections',
                PropertyName: '_Connections',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: MCP Server Tools',
                PropertyName: '_Tools',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: MCP Tool Favorites',
                PropertyName: '_Favorites',
                CacheLocal: true
            }
        ];
        await this.Load(configs, provider, forceRefresh, contextUser);
    }
    /**
     * Gets the singleton instance of MCPEngine
     */
    static get Instance() {
        return super.getInstance();
    }
    // ========================================
    // Public Getters
    // ========================================
    /**
     * Gets all cached MCP servers
     */
    get Servers() {
        return this.GetConfigData('_Servers');
    }
    /**
     * Gets all cached MCP server connections
     */
    get Connections() {
        return this.GetConfigData('_Connections');
    }
    /**
     * Gets all cached MCP server tools
     */
    get Tools() {
        return this.GetConfigData('_Tools');
    }
    /**
     * Gets all cached MCP tool favorites (across all users).
     * For current-user-only access, use {@link GetFavoritesByUser}.
     */
    get Favorites() {
        return this.GetConfigData('_Favorites');
    }
    /**
     * Gets all MCP tool favorites for a specific user.
     *
     * @param userId - The user ID
     */
    GetFavoritesByUser(userId) {
        return this._Favorites.filter(f => UUIDsEqual(f.UserID, userId));
    }
    /**
     * Gets a single MCP tool favorite for a user/tool pair.
     *
     * @param userId - The user ID
     * @param toolId - The MCP server tool ID
     */
    GetFavoriteByUserAndTool(userId, toolId) {
        return this._Favorites.find(f => UUIDsEqual(f.UserID, userId) && UUIDsEqual(f.MCPServerToolID, toolId));
    }
    // ========================================
    // Helper Methods
    // ========================================
    /**
     * Gets a server by ID
     *
     * @param serverId - The server ID
     * @returns The server entity or undefined if not found
     */
    GetServerById(serverId) {
        return this._Servers.find(s => UUIDsEqual(s.ID, serverId));
    }
    /**
     * Gets a connection by ID
     *
     * @param connectionId - The connection ID
     * @returns The connection entity or undefined if not found
     */
    GetConnectionById(connectionId) {
        return this._Connections.find(c => UUIDsEqual(c.ID, connectionId));
    }
    /**
     * Gets a tool by ID
     *
     * @param toolId - The tool ID
     * @returns The tool entity or undefined if not found
     */
    GetToolById(toolId) {
        return this._Tools.find(t => UUIDsEqual(t.ID, toolId));
    }
    /**
     * Gets all connections for a specific server
     *
     * @param serverId - The server ID
     * @returns Array of connections for the server
     */
    GetConnectionsByServer(serverId) {
        return this._Connections.filter(c => UUIDsEqual(c.MCPServerID, serverId));
    }
    /**
     * Gets all tools for a specific server
     *
     * @param serverId - The server ID
     * @returns Array of tools for the server
     */
    GetToolsByServer(serverId) {
        return this._Tools.filter(t => UUIDsEqual(t.MCPServerID, serverId));
    }
    /**
     * Gets active servers only
     *
     * @returns Array of servers with Status = 'Active'
     */
    get ActiveServers() {
        return this.GetConfigData('_Servers').filter(s => s.Status === 'Active');
    }
    /**
     * Gets active connections only
     *
     * @returns Array of connections with Status = 'Active'
     */
    get ActiveConnections() {
        return this.GetConfigData('_Connections').filter(c => c.Status === 'Active');
    }
    /**
     * Gets active tools only
     *
     * @returns Array of tools with Status = 'Active'
     */
    get ActiveTools() {
        return this.GetConfigData('_Tools').filter(t => t.Status === 'Active');
    }
    /**
     * Gets active connections for a specific server
     *
     * @param serverId - The server ID
     * @returns Array of active connections for the server
     */
    GetActiveConnectionsByServer(serverId) {
        return this._Connections.filter(c => UUIDsEqual(c.MCPServerID, serverId) && c.Status === 'Active');
    }
    /**
     * Gets active tools for a specific server
     *
     * @param serverId - The server ID
     * @returns Array of active tools for the server
     */
    GetActiveToolsByServer(serverId) {
        return this._Tools.filter(t => UUIDsEqual(t.MCPServerID, serverId) && t.Status === 'Active');
    }
    /**
     * Gets the server name for a given server ID
     *
     * @param serverId - The server ID
     * @returns The server name or 'Unknown' if not found
     */
    GetServerName(serverId) {
        const server = this.GetServerById(serverId);
        return server?.Name ?? 'Unknown';
    }
    /**
     * Gets the connection name for a given connection ID
     *
     * @param connectionId - The connection ID
     * @returns The connection name or 'Unknown' if not found
     */
    GetConnectionName(connectionId) {
        const connection = this.GetConnectionById(connectionId);
        return connection?.Name ?? 'Unknown';
    }
}
//# sourceMappingURL=MCPEngine.js.map