import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpDisconnectPractice } from "./anthropic.js";
import { deepmindMcpDisconnectPractice } from "./deepmind.js";
import { mcpDisconnectDependencyDeclarations } from "./dependencies.js";
import { openaiMcpDisconnectPractice } from "./openai.js";
import { executeMcpDisconnect as executeMcpDisconnectCore, mcpDisconnectDescriptor, planMcpDisconnect } from "./core.js";
export * from "./core.js";
export const mcpDisconnectProviderPractices = [anthropicMcpDisconnectPractice, openaiMcpDisconnectPractice, deepmindMcpDisconnectPractice];
export const mcpDisconnectBestPracticeDescriptor = { toolId: "mcp.disconnect", bestPractice: "storage-owned-mcp-disconnect-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpDisconnectDependencyDeclarations };
function orderedPractices(preferredProvider) {
    return preferredProvider === undefined ? mcpDisconnectProviderPractices : [...mcpDisconnectProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpDisconnectProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpDisconnectPractice(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 disconnect 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 executeMcpDisconnect(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpDisconnectPractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpDisconnectCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpDisconnectBaseToolDefinition = createMcpBaseToolDefinition({ toolId: "mcp.disconnect", title: "MCP Disconnect", description: "Request runtime to disconnect a managed MCP server connection.", summary: "Use mcp.disconnect to ask the runtime-owned MCP manager to close or mark an MCP connection.", storageGroup: "connection", riskLevel: "risky", permissionHints: ["mcp:access", "mcp:disconnect"], dependencies: mcpDisconnectDependencyDeclarations, inputSchema: jsonSchema("mcp.disconnect.input", { type: "object", additionalProperties: true }), outputSchema: jsonSchema("mcp.disconnect.output", { type: "object", additionalProperties: true }) });
export const mcpDisconnectHandler = createMcpCoreHandler(mcpDisconnectBaseToolDefinition, async (request) => {
    const selection = selectMcpDisconnectPractice({ 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 executeMcpDisconnectCore({ ...request.input, context }, selection.provider);
});
export { mcpDisconnectDescriptor, planMcpDisconnect };
