import { buildCodeBasePracticeAuditMetadata, createCodeBaseCoreHandler, createCodeBaseToolDefinition, injectRuntimeInvocationMetadata, jsonSchema } from "../../_shared/baseToolAdapter.js";
import { anthropicCodeModifyPractice } from "./anthropic.js";
import { deepmindCodeModifyPractice } from "./deepmind.js";
import { codeModifyDependencyDeclarations } from "./dependencies.js";
import { openaiCodeModifyPractice } from "./openai.js";
import { codeModifyDescriptor, executeCodeModify as executeCodeModifyCore, planCodeModify, } from "./core.js";
export * from "./core.js";
export const codeModifyProviderPractices = [anthropicCodeModifyPractice, openaiCodeModifyPractice, deepmindCodeModifyPractice];
export const codeModifyBestPracticeDescriptor = {
    toolId: "code.modify",
    bestPractice: "storage-owned-bounded-code-modify-with-runtime-filesystem-support",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: codeModifyDependencyDeclarations,
};
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return codeModifyProviderPractices;
    return [...codeModifyProviderPractices.filter((practice) => practice.providerName === preferredProvider), ...codeModifyProviderPractices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectCodeModifyPractice(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-governed",
            notes: ["No injected or host filesystem edit provider is available; dry-run remains available."],
            createProvider: () => undefined,
        },
    };
}
function practiceAuditMetadata(selection) {
    return buildCodeBasePracticeAuditMetadata({
        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 executeCodeModify(request = {}) {
    const selection = selectCodeModifyPractice({
        executor: request.executor,
        provider: request.modifier ?? request.provider,
        preferredProvider: request.preferredProvider,
    });
    return executeCodeModifyCore({
        ...request,
        modifier: selection.provider,
        context: {
            ...request.context,
            auditMetadata: { ...(request.context?.auditMetadata ?? {}), ...practiceAuditMetadata(selection) },
        },
    });
}
const invocationContextSchema = {
    type: "object",
    additionalProperties: true,
    properties: {
        runtimeId: { type: "string" },
        sessionId: { type: "string" },
        invocationId: { type: "string" },
        dryRun: { type: "boolean" },
        workspaceRoot: { type: "string" },
        guard: { type: "object", additionalProperties: true },
    },
};
export const codeModifyBaseToolDefinition = createCodeBaseToolDefinition({
    toolId: "code.modify",
    title: "Code Modify",
    description: "Apply a governed bounded replacement inside an existing code file.",
    summary: "Use code.modify for bounded search/replace edits instead of shell text rewrites.",
    storageGroup: "edit",
    riskLevel: "risky",
    permissionHints: ["filesystem:read", "filesystem:write"],
    dependencies: codeModifyDependencyDeclarations,
    inputSchema: jsonSchema("code.modify.input", { type: "object", additionalProperties: true, properties: { workspaceRoot: { type: "string" }, targetPath: { type: "string" }, searchText: { type: "string" }, replacementText: { type: "string" }, occurrence: { enum: ["first", "all"] }, maxReplacements: { type: "integer", minimum: 1 }, dryRun: { type: "boolean" }, context: invocationContextSchema } }),
    outputSchema: jsonSchema("code.modify.output", { type: "object", additionalProperties: true }),
});
export const codeModifyHandler = createCodeBaseCoreHandler(codeModifyBaseToolDefinition, async (request) => {
    const selection = selectCodeModifyPractice({ ...request.input, executor: request.executor, provider: request.input.modifier ?? request.input.provider });
    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 executeCodeModifyCore({ ...request.input, modifier: selection.provider, context });
});
export { codeModifyDescriptor, planCodeModify };
