import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpInvalidateCachePractice } from "./anthropic.js";
import { deepmindMcpInvalidateCachePractice } from "./deepmind.js";
import { mcpInvalidateCacheDependencyDeclarations } from "./dependencies.js";
import { openaiMcpInvalidateCachePractice } from "./openai.js";
import { executeMcpCacheInvalidation as executeMcpCacheInvalidationCore, mcpInvalidateCacheDescriptor, planMcpCacheInvalidation } from "./core.js";
export * from "./core.js";
export const mcpInvalidateCacheProviderPractices = [anthropicMcpInvalidateCachePractice, openaiMcpInvalidateCachePractice, deepmindMcpInvalidateCachePractice];
export const mcpInvalidateCacheBestPracticeDescriptor = { toolId: "mcp.invalidateCache", bestPractice: "storage-owned-mcp-cache-invalidation-with-runtime-owned-cache-store", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpInvalidateCacheDependencyDeclarations };
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return mcpInvalidateCacheProviderPractices;
    return [...mcpInvalidateCacheProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpInvalidateCacheProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpInvalidateCachePractice(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 cache invalidation 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 executeMcpCacheInvalidation(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpInvalidateCachePractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpCacheInvalidationCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpInvalidateCacheBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.invalidateCache",
    title: "MCP Invalidate Cache",
    description: "Request runtime to invalidate cache material for a configured MCP server.",
    summary: "Use mcp.invalidateCache for governed runtime-owned MCP cache eviction.",
    storageGroup: "cache",
    riskLevel: "risky",
    permissionHints: ["mcp:access", "mcp:cache:invalidate"],
    dependencies: mcpInvalidateCacheDependencyDeclarations,
    inputSchema: jsonSchema("mcp.invalidateCache.input", { type: "object", additionalProperties: true }),
    outputSchema: jsonSchema("mcp.invalidateCache.output", { type: "object", additionalProperties: true }),
});
export const mcpInvalidateCacheHandler = createMcpCoreHandler(mcpInvalidateCacheBaseToolDefinition, async (request) => {
    const selection = selectMcpInvalidateCachePractice({ 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 executeMcpCacheInvalidationCore({ ...request.input, context }, selection.provider);
});
export { mcpInvalidateCacheDescriptor, planMcpCacheInvalidation };
