import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpCreateResourcePractice } from "./anthropic.js";
import { deepmindMcpCreateResourcePractice } from "./deepmind.js";
import { mcpCreateResourceDependencyDeclarations } from "./dependencies.js";
import { openaiMcpCreateResourcePractice } from "./openai.js";
import { executeMcpCreateResource as executeMcpCreateResourceCore, mcpCreateResourceDescriptor, planMcpCreateResource } from "./core.js";
export * from "./core.js";
export const mcpCreateResourceProviderPractices = [anthropicMcpCreateResourcePractice, openaiMcpCreateResourcePractice, deepmindMcpCreateResourcePractice];
export const mcpCreateResourceBestPracticeDescriptor = { toolId: "mcp.createResource", bestPractice: "storage-owned-mcp-create-resource-with-runtime-owned-client", providerOrder: ["anthropic", "openai", "deepmind"], dependencies: mcpCreateResourceDependencyDeclarations };
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return mcpCreateResourceProviderPractices;
    return [...mcpCreateResourceProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpCreateResourceProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpCreateResourcePractice(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 createResource 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 executeMcpCreateResource(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpCreateResourcePractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpCreateResourceCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
export const mcpCreateResourceBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.createResource",
    title: "MCP Create Resource",
    description: "Request runtime to create a resource on a configured MCP server.",
    summary: "Use mcp.createResource for governed runtime-owned MCP resource creation.",
    storageGroup: "resource",
    riskLevel: "risky",
    permissionHints: ["mcp:access", "mcp:connection:read", "mcp:resource:create"],
    dependencies: mcpCreateResourceDependencyDeclarations,
    inputSchema: jsonSchema("mcp.createResource.input", { type: "object", additionalProperties: true }),
    outputSchema: jsonSchema("mcp.createResource.output", { type: "object", additionalProperties: true }),
});
export const mcpCreateResourceHandler = createMcpCoreHandler(mcpCreateResourceBaseToolDefinition, async (request) => {
    const selection = selectMcpCreateResourcePractice({ 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 executeMcpCreateResourceCore({ ...request.input, context }, selection.provider);
});
export { mcpCreateResourceDescriptor, planMcpCreateResource };
