import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpPingPractice } from "./anthropic.js";
import { deepmindMcpPingPractice } from "./deepmind.js";
import { mcpPingDependencyDeclarations } from "./dependencies.js";
import { openaiMcpPingPractice } from "./openai.js";
import { executeMcpPing as executeMcpPingCore, mcpPingDescriptor, planMcpPing } from "./core.js";
export * from "./core.js";
export const mcpPingProviderPractices = [anthropicMcpPingPractice, openaiMcpPingPractice, deepmindMcpPingPractice];
export const mcpPingBestPracticeDescriptor = { toolId: "mcp.ping", bestPractice: "storage-owned-mcp-ping-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpPingDependencyDeclarations };
function orderedPractices(preferredProvider) { return preferredProvider === undefined ? mcpPingProviderPractices : [...mcpPingProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpPingProviderPractices.filter((practice) => practice.providerName !== preferredProvider)]; }
export function selectMcpPingPractice(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 ping 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 executeMcpPing(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpPingPractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpPingCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpPingBaseToolDefinition = createMcpBaseToolDefinition({ toolId: "mcp.ping", title: "MCP Ping", description: "Ping a runtime-owned MCP server connection.", summary: "Use mcp.ping to probe MCP liveness without owning the MCP client in baseTool.", storageGroup: "connection", riskLevel: "normal", permissionHints: ["mcp:access", "mcp:ping"], dependencies: mcpPingDependencyDeclarations, inputSchema: jsonSchema("mcp.ping.input", { type: "object", additionalProperties: true }), outputSchema: jsonSchema("mcp.ping.output", { type: "object", additionalProperties: true }) });
export const mcpPingHandler = createMcpCoreHandler(mcpPingBaseToolDefinition, async (request) => {
    const selection = selectMcpPingPractice({ 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 executeMcpPingCore({ ...request.input, context }, selection.provider);
});
export { mcpPingDescriptor, planMcpPing };
