import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpUpdateToolPractice } from "./anthropic.js";
import { deepmindMcpUpdateToolPractice } from "./deepmind.js";
import { mcpUpdateToolDependencyDeclarations } from "./dependencies.js";
import { openaiMcpUpdateToolPractice } from "./openai.js";
import { executeMcpToolUpdate as executeMcpToolUpdateCore, planMcpToolUpdate } from "./core.js";
export * from "./core.js";
export const mcpUpdateToolProviderPractices = [anthropicMcpUpdateToolPractice, openaiMcpUpdateToolPractice, deepmindMcpUpdateToolPractice];
export const mcpUpdateToolBestPracticeDescriptor = { toolId: "mcp.updateTool", bestPractice: "storage-owned-mcp-update-tool-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpUpdateToolDependencyDeclarations };
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return mcpUpdateToolProviderPractices;
    return [...mcpUpdateToolProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpUpdateToolProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpUpdateToolPractice(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 updateTool 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 executeMcpToolUpdate(request = {}, providerOverride) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpUpdateToolPractice({ executor: requestRecord.executor, provider: providerOverride ?? requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpToolUpdateCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpUpdateToolBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.updateTool",
    title: "MCP Update Tool",
    description: "Request runtime to update a registered MCP tool definition.",
    summary: "Use mcp.updateTool for governed runtime-owned MCP tool registry patching.",
    storageGroup: "tool",
    riskLevel: "risky",
    permissionHints: ["mcp:access", "mcp:tool:read", "mcp:tool:write"],
    dependencies: mcpUpdateToolDependencyDeclarations,
    inputSchema: jsonSchema("mcp.updateTool.input", { type: "object", additionalProperties: true }),
    outputSchema: jsonSchema("mcp.updateTool.output", { type: "object", additionalProperties: true }),
});
export const mcpUpdateToolHandler = createMcpCoreHandler(mcpUpdateToolBaseToolDefinition, async (request) => {
    const selection = selectMcpUpdateToolPractice({ 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 executeMcpToolUpdateCore({ ...request.input, context }, selection.provider);
});
export { planMcpToolUpdate };
