import { anthropicCodeSearchRipgrepPractice } from "./anthropic.js";
import { deepmindCodeSearchRipgrepPractice } from "./deepmind.js";
import { openaiCodeSearchRipgrepPractice } from "./openai.js";
import { buildCodeBasePracticeAuditMetadata, createCodeBaseCoreHandler, createCodeBaseToolDefinition, injectRuntimeInvocationMetadata, jsonSchema, } from "../../_shared/baseToolAdapter.js";
import { executeCodeSearchRipgrep as executeCodeSearchRipgrepCore, } from "./core.js";
import { codeSearchRipgrepDependencyDeclarations, } from "./dependencies.js";
export * from "./core.js";
export const codeSearchRipgrepProviderPractices = [
    anthropicCodeSearchRipgrepPractice,
    openaiCodeSearchRipgrepPractice,
    deepmindCodeSearchRipgrepPractice,
];
export const codeSearchRipgrepBestPracticeDescriptor = {
    toolId: "code.search_Ripgrep",
    bestPractice: "storage-owned-code-search-with-runtime-ripgrep-support",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: codeSearchRipgrepDependencyDeclarations,
};
function orderedPractices(preferredProvider) {
    if (preferredProvider === undefined)
        return codeSearchRipgrepProviderPractices;
    return [
        ...codeSearchRipgrepProviderPractices.filter((practice) => practice.providerName === preferredProvider),
        ...codeSearchRipgrepProviderPractices.filter((practice) => practice.providerName !== preferredProvider),
    ];
}
export function selectCodeSearchRipgrepPractice(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: "read-only",
            notes: ["No injected or host ripgrep-style search 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 executeCodeSearchRipgrep(request = {}) {
    const selection = selectCodeSearchRipgrepPractice({
        executor: request.executorPort,
        provider: request.executor ?? request.provider,
        preferredProvider: request.preferredProvider,
    });
    return executeCodeSearchRipgrepCore({
        ...request,
        executor: 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" },
        allowedRoots: { type: "array", items: { type: "string" } },
        guard: { type: "object", additionalProperties: true },
    },
};
export const codeSearchRipgrepBaseToolDefinition = createCodeBaseToolDefinition({
    toolId: "code.search_Ripgrep",
    title: "Code Search Ripgrep",
    description: "Search code text through a governed ripgrep-style runtime provider.",
    summary: "Use code.search_Ripgrep for precise text search without shell.",
    storageGroup: "explore",
    riskLevel: "normal",
    permissionHints: ["filesystem:read"],
    dependencies: codeSearchRipgrepDependencyDeclarations,
    inputSchema: jsonSchema("code.search_Ripgrep.input", {
        type: "object",
        additionalProperties: true,
        required: ["query", "directoryPath"],
        properties: {
            query: { type: "string" },
            pattern: { type: "string" },
            directoryPath: { type: "string" },
            fileGlob: { type: "string" },
            maxMatches: { type: "integer", minimum: 1 },
            literal: { type: "boolean" },
            caseSensitive: { type: "boolean" },
            includeHidden: { type: "boolean" },
            multiline: { type: "boolean" },
            contextLines: { type: "integer", minimum: 0, maximum: 20 },
            context: invocationContextSchema,
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("code.search_Ripgrep.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "matches", "exitCode", "truncated"],
        properties: {
            kind: { const: "agentCore.basicTool.code.search_Ripgrep.output" },
            matches: { type: "array" },
            exitCode: { type: "integer" },
            stderr: { type: "string" },
            truncated: { type: "boolean" },
        },
    }),
});
export const codeSearchRipgrepHandler = createCodeBaseCoreHandler(codeSearchRipgrepBaseToolDefinition, async (request) => {
    const selection = selectCodeSearchRipgrepPractice({
        ...request.input,
        executor: request.executor,
        provider: request.input.executor ?? 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 executeCodeSearchRipgrepCore({
        ...request.input,
        executor: selection.provider,
        context,
    });
});
