import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpReadResourcePractice } from "./anthropic.js";
import { deepmindMcpReadResourcePractice } from "./deepmind.js";
import { mcpReadResourceDependencyDeclarations } from "./dependencies.js";
import { openaiMcpReadResourcePractice } from "./openai.js";
import { executeMcpResourceRead as executeMcpResourceReadCore, mcpReadResourceDescriptor, planMcpResourceRead } from "./core.js";
export * from "./core.js";
export const mcpReadResourceProviderPractices = [anthropicMcpReadResourcePractice, openaiMcpReadResourcePractice, deepmindMcpReadResourcePractice];
export const mcpReadResourceBestPracticeDescriptor = { toolId: "mcp.readResource", bestPractice: "storage-owned-mcp-read-resource-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpReadResourceDependencyDeclarations };
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return mcpReadResourceProviderPractices;
    return [...mcpReadResourceProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpReadResourceProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpReadResourcePractice(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 readResource 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 executeMcpResourceRead(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpReadResourcePractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpResourceReadCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpReadResourceBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.readResource",
    title: "MCP Read Resource",
    description: "Read a resource through a runtime-owned MCP server client.",
    summary: "Use mcp.readResource to fetch MCP resource contents while runtime owns the MCP session.",
    storageGroup: "resource",
    riskLevel: "normal",
    permissionHints: ["mcp:access", "mcp:resource:read"],
    dependencies: mcpReadResourceDependencyDeclarations,
    inputSchema: jsonSchema("mcp.readResource.input", { type: "object", additionalProperties: true }),
    outputSchema: jsonSchema("mcp.readResource.output", { type: "object", additionalProperties: true }),
});
export const mcpReadResourceHandler = createMcpCoreHandler(mcpReadResourceBaseToolDefinition, async (request) => {
    const selection = selectMcpReadResourcePractice({ 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 executeMcpResourceReadCore({ ...request.input, context }, selection.provider);
});
export { mcpReadResourceDescriptor, planMcpResourceRead };
