import { buildPracticeAuditMetadata, createLspBaseToolDefinition, createLspCoreHandler, injectInvocationAudit, jsonSchema, lspCommonSchemaFragments, normalizeLspDependencyDeclarations, preferAnthropicExecutor, } from "../_shared/baseToolAdapter.js";
import { suggestLspCodeActions as suggestLspCodeActionsCore, } from "./core.js";
import { anthropicLspSuggestCodeActionsPractice } from "./anthropic.js";
import { lspSuggestCodeActionsDependencyDeclarations } from "./dependencies.js";
import { deepmindLspSuggestCodeActionsPractice } from "./deepmind.js";
import { openaiLspSuggestCodeActionsPractice } from "./openai.js";
export * from "./core.js";
export const lspSuggestCodeActionsProviderPractices = [
    anthropicLspSuggestCodeActionsPractice,
    openaiLspSuggestCodeActionsPractice,
    deepmindLspSuggestCodeActionsPractice,
];
export const lspSuggestCodeActionsBestPracticeDescriptor = {
    toolId: "code.lsp_suggestCodeActions",
    bestPractice: "anthropic-host-executor-or-shared-stdio-lsp-runtime",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerPractices: lspSuggestCodeActionsProviderPractices,
    dependencies: lspSuggestCodeActionsDependencyDeclarations,
};
function createExecutorProvider(executor) {
    const suggestCodeActions = executor?.lsp?.suggestCodeActions;
    if (suggestCodeActions === undefined) {
        return undefined;
    }
    return async (target, context, diagnostics, only) => {
        const result = await suggestCodeActions({
            target,
            diagnostics: diagnostics
                .filter((diagnostic) => diagnostic.range !== undefined)
                .map((diagnostic) => ({
                ...diagnostic,
                range: diagnostic.range,
            })),
            only,
            context: {
                invocationId: context.invocationId,
                workspaceRoot: context.workspaceRoot,
                auditMetadata: context.auditMetadata,
            },
        });
        if (!result.ok)
            throw new Error(result.error.message);
        return result.output.actions;
    };
}
export const lspSuggestCodeActionsBaseToolDefinition = createLspBaseToolDefinition({
    toolId: "code.lsp_suggestCodeActions",
    title: "Code LSP Suggest Code Actions",
    description: "Suggest available code actions through a governed LSP provider.",
    summary: "Use code.lsp_suggestCodeActions when the agent needs fix-it or refactor suggestions without applying edits.",
    permissionHints: ["workspace:read", "lsp:read"],
    dependencies: normalizeLspDependencyDeclarations(lspSuggestCodeActionsDependencyDeclarations),
    inputSchema: jsonSchema("code.lsp_suggestCodeActions.input", {
        type: "object",
        additionalProperties: true,
        required: ["target"],
        properties: {
            target: {
                type: "object",
                additionalProperties: false,
                required: ["filePath", "range"],
                properties: {
                    filePath: { type: "string", minLength: 1 },
                    range: lspCommonSchemaFragments.lspRange,
                    languageId: { type: "string" },
                },
            },
            only: { type: "array", items: { type: "string" } },
            diagnostics: { type: "array", items: { type: "object", additionalProperties: true } },
            context: lspCommonSchemaFragments.invocationContext,
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("code.lsp_suggestCodeActions.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "actions", "dryRun", "providerCalled"],
        properties: {
            kind: { const: "agentCore.basicTool.lsp.suggestCodeActions" },
            target: { type: "object", additionalProperties: true },
            actions: { type: "array", items: { type: "object", additionalProperties: true } },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
        },
    }),
});
export const lspSuggestCodeActionsHandler = createLspCoreHandler(lspSuggestCodeActionsBaseToolDefinition, async (request) => {
    const selection = preferAnthropicExecutor(request.executor, (executor) => executor.lsp?.suggestCodeActions !== undefined, lspSuggestCodeActionsProviderPractices, request.input.preferredProvider);
    return await suggestLspCodeActionsCore({
        ...request.input,
        provider: request.input.provider ?? createExecutorProvider(request.executor),
        context: {
            ...request.input.context,
            invocationId: request.input.context?.invocationId ?? request.toolCallId,
            auditMetadata: injectInvocationAudit({
                ...buildPracticeAuditMetadata(selection),
                ...(request.metadata ?? {}),
            }, request.input.context?.auditMetadata, request),
        },
    });
});
