import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpHealthCheckPractice } from "./anthropic.js";
import { deepmindMcpHealthCheckPractice } from "./deepmind.js";
import { mcpHealthCheckDependencyDeclarations } from "./dependencies.js";
import { openaiMcpHealthCheckPractice } from "./openai.js";
import { executeMcpHealthCheck as executeMcpHealthCheckCore, mcpHealthCheckDescriptor, planMcpHealthCheck } from "./core.js";
export * from "./core.js";
export const mcpHealthCheckProviderPractices = [anthropicMcpHealthCheckPractice, openaiMcpHealthCheckPractice, deepmindMcpHealthCheckPractice];
export const mcpHealthCheckBestPracticeDescriptor = { toolId: "mcp.healthCheck", bestPractice: "storage-owned-mcp-health-check-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpHealthCheckDependencyDeclarations };
function orderedPractices(preferredProvider) { return preferredProvider === undefined ? mcpHealthCheckProviderPractices : [...mcpHealthCheckProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpHealthCheckProviderPractices.filter((practice) => practice.providerName !== preferredProvider)]; }
export function selectMcpHealthCheckPractice(dependencies = {}) {
    for (const practice of orderedPractices(dependencies.preferredProvider)) {
        const provider = practice.createProvider(dependencies);
        if (provider !== undefined)
            return { providerName: practice.providerName, practice, provider };
    }
    return { providerName: "praxis-native", practice: { providerName: "praxis-native", source: { kind: "praxis-native", label: "Praxis dry-run fallback" }, directCliSupport: false, sideEffectPolicy: "runtime-owned-client", notes: ["No runtime MCP healthCheck provider is available; dry-run remains available."], createProvider: () => undefined } };
}
function practiceAuditMetadata(selection) { return buildMcpPracticeAuditMetadata({ providerName: selection.providerName, sourceLabel: selection.practice.source.label, sourceKind: selection.practice.source.kind, sourcePath: selection.practice.source.path, directCliSupport: selection.practice.directCliSupport, sideEffectPolicy: selection.practice.sideEffectPolicy, notes: selection.practice.notes }); }
export async function executeMcpHealthCheck(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpHealthCheckPractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpHealthCheckCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpHealthCheckBaseToolDefinition = createMcpBaseToolDefinition({ toolId: "mcp.healthCheck", title: "MCP Health Check", description: "Check health through a runtime-owned MCP server client.", summary: "Use mcp.healthCheck to inspect MCP server health while runtime owns the session.", storageGroup: "monitoring", riskLevel: "normal", permissionHints: ["mcp:access", "mcp:monitor:read"], dependencies: mcpHealthCheckDependencyDeclarations, inputSchema: jsonSchema("mcp.healthCheck.input", { type: "object", additionalProperties: true }), outputSchema: jsonSchema("mcp.healthCheck.output", { type: "object", additionalProperties: true }) });
export const mcpHealthCheckHandler = createMcpCoreHandler(mcpHealthCheckBaseToolDefinition, async (request) => {
    const selection = selectMcpHealthCheckPractice({ executor: request.executor, provider: request.input.provider, preferredProvider: request.input.preferredProvider });
    const inputContext = request.input.context ?? {};
    const context = { ...inputContext, runtimeId: inputContext.runtimeId ?? request.runtimeId, sessionId: inputContext.sessionId ?? request.sessionId, invocationId: inputContext.invocationId ?? request.toolCallId, auditMetadata: injectRuntimeInvocationMetadata({ ...practiceAuditMetadata(selection), ...(request.metadata ?? {}) }, inputContext.auditMetadata, request) };
    return executeMcpHealthCheckCore({ ...request.input, context }, selection.provider);
});
export { mcpHealthCheckDescriptor, planMcpHealthCheck };
