import { buildMcpPracticeAuditMetadata, createMcpBaseToolDefinition, createMcpCoreHandler, injectRuntimeInvocationMetadata, isJsonObject, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicMcpListToolsPractice } from "./anthropic.js";
import { deepmindMcpListToolsPractice } from "./deepmind.js";
import { mcpListToolsDependencyDeclarations } from "./dependencies.js";
import { openaiMcpListToolsPractice } from "./openai.js";
import { executeMcpToolsList as executeMcpToolsListCore, mcpListToolsDescriptor, planMcpToolsList } from "./core.js";
export * from "./core.js";
export const mcpListToolsProviderPractices = [anthropicMcpListToolsPractice, openaiMcpListToolsPractice, deepmindMcpListToolsPractice];
export const mcpListToolsBestPracticeDescriptor = {
    toolId: "mcp.listTools",
    bestPractice: "storage-owned-mcp-list-tools-with-runtime-owned-client",
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: mcpListToolsDependencyDeclarations,
};
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return mcpListToolsProviderPractices;
    return [...mcpListToolsProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...mcpListToolsProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectMcpListToolsPractice(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 listTools 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 executeMcpToolsList(request = {}) {
    const requestRecord = isJsonObject(request) ? request : {};
    const selection = selectMcpListToolsPractice({ executor: requestRecord.executor, provider: requestRecord.provider, preferredProvider: requestRecord.preferredProvider });
    const context = isJsonObject(requestRecord.context) ? requestRecord.context : undefined;
    return executeMcpToolsListCore(isJsonObject(request) ? { ...requestRecord, context: { ...context, auditMetadata: { ...(context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) } } } : request, selection.provider);
}
const invocationContextSchema = { type: "object", additionalProperties: true };
export const mcpListToolsBaseToolDefinition = createMcpBaseToolDefinition({
    toolId: "mcp.listTools",
    title: "MCP List Tools",
    description: "List tools from a runtime-owned MCP server client.",
    summary: "Use mcp.listTools to discover tools from a mounted MCP server without owning the client in baseTool.",
    storageGroup: "tool",
    riskLevel: "normal",
    permissionHints: ["mcp:access", "mcp:tool:read"],
    dependencies: mcpListToolsDependencyDeclarations,
    inputSchema: jsonSchema("mcp.listTools.input", { type: "object", additionalProperties: true, properties: { target: { type: "object", additionalProperties: true }, context: invocationContextSchema } }),
    outputSchema: jsonSchema("mcp.listTools.output", { type: "object", additionalProperties: true }),
});
export const mcpListToolsHandler = createMcpCoreHandler(mcpListToolsBaseToolDefinition, async (request) => {
    const selection = selectMcpListToolsPractice({ 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 executeMcpToolsListCore({ ...request.input, context }, selection.provider);
});
export { mcpListToolsDescriptor, planMcpToolsList };
