import { anthropicSkillRipgrepPractice } from "./anthropic.js";
import { deepmindSkillRipgrepPractice } from "./deepmind.js";
import { openaiSkillRipgrepPractice } from "./openai.js";
import { buildSkillBasePracticeAuditMetadata, createSkillBaseCoreHandler, createSkillBaseToolDefinition, injectRuntimeInvocationMetadata, jsonSchema } from "../_shared/baseToolAdapter.js";
import { executeSkillRipgrep as executeSkillRipgrepCore } from "./core.js";
import { skillRipgrepDependencyDeclarations } from "./dependencies.js";
export { planSkillRipgrep, skillRipgrepDescriptor, executeSkillRipgrep as executeSkillRipgrepCore } from "./core.js";
const practices = [anthropicSkillRipgrepPractice, openaiSkillRipgrepPractice, deepmindSkillRipgrepPractice];
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function normalizeSkillRipgrepRequest(request) {
    if (request.target !== undefined || !isRecord(request))
        return request;
    const raw = request;
    const query = typeof raw.query === "string" && raw.query.trim().length > 0 ? raw.query.trim() : undefined;
    const registryRoot = typeof raw.registryRoot === "string" && raw.registryRoot.trim().length > 0
        ? raw.registryRoot.trim()
        : typeof raw.directoryPath === "string" && raw.directoryPath.trim().length > 0
            ? raw.directoryPath.trim()
            : typeof raw.skillRoot === "string" && raw.skillRoot.trim().length > 0
                ? raw.skillRoot.trim()
                : undefined;
    if (query === undefined || registryRoot === undefined)
        return request;
    return {
        ...request,
        target: {
            query,
            registryRoot,
            ...(typeof raw.skillId === "string" ? { skillId: raw.skillId } : {}),
            ...(typeof raw.fileGlob === "string" ? { fileGlob: raw.fileGlob } : {}),
            ...(typeof raw.maxResults === "number" ? { maxResults: raw.maxResults } : {}),
            ...(typeof raw.literal === "boolean" ? { literal: raw.literal } : {}),
            ...(typeof raw.caseSensitive === "boolean" ? { caseSensitive: raw.caseSensitive } : {}),
            ...(typeof raw.includeHidden === "boolean" ? { includeHidden: raw.includeHidden } : {}),
            ...(typeof raw.multiline === "boolean" ? { multiline: raw.multiline } : {}),
            ...(typeof raw.contextLines === "number" ? { contextLines: raw.contextLines } : {}),
        },
    };
}
function ordered(preferredProvider) {
    return preferredProvider === undefined ? practices : [...practices.filter((practice) => practice.providerName === preferredProvider), ...practices.filter((practice) => practice.providerName !== preferredProvider)];
}
export function selectSkillRipgrepPractice(dependencies = {}) {
    for (const practice of ordered(dependencies.preferredProvider)) {
        const provider = practice.createProvider({ executor: dependencies.executor, provider: dependencies.provider });
        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", path: undefined }, directCliSupport: false, sideEffectPolicy: "read-only", notes: ["No runtime ripgrep provider is available; dry-run remains available."], createProvider: () => undefined }, provider: undefined };
}
export async function executeSkillRipgrep(request = {}) {
    const normalized = normalizeSkillRipgrepRequest(request);
    const selection = selectSkillRipgrepPractice(request);
    return executeSkillRipgrepCore({ ...normalized, provider: selection.provider, context: { ...normalized.context, auditMetadata: { ...(normalized.context?.auditMetadata ?? {}), ...buildSkillBasePracticeAuditMetadata({ 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 const skillRipgrepBaseToolDefinition = createSkillBaseToolDefinition({
    toolId: "skill.ripgrep",
    title: "Skill Ripgrep",
    description: "Search local skill packages or an allowed skill-like context root through governed runtime ripgrep support. Prefer target.query plus target.registryRoot; directoryPath is accepted as a compatibility alias for registryRoot.",
    summary: "Use skill.ripgrep for precise local search across SKILL.md, references, scripts, assets, examples, or an allowed skill context root.",
    riskLevel: "normal",
    permissionHints: ["skill:read", "filesystem:read"],
    dependencies: skillRipgrepDependencyDeclarations,
    inputSchema: jsonSchema("skill.ripgrep.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    query: { type: "string", description: "Literal or regex query to search." },
                    registryRoot: { type: "string", description: "Skill registry or allowed local context root to search." },
                    directoryPath: { type: "string", description: "Compatibility alias for registryRoot." },
                    skillId: { type: "string", description: "Optional skill folder id below registryRoot." },
                    fileGlob: { type: "string", description: "Optional file glob." },
                    maxResults: { type: "integer", minimum: 1, maximum: 500 },
                    literal: { type: "boolean" },
                    caseSensitive: { type: "boolean" },
                    includeHidden: { type: "boolean" },
                    multiline: { type: "boolean" },
                    contextLines: { type: "integer", minimum: 0 },
                },
                required: ["query", "registryRoot"],
            },
            query: { type: "string", description: "Compatibility shorthand for target.query." },
            registryRoot: { type: "string", description: "Compatibility shorthand for target.registryRoot." },
            directoryPath: { type: "string", description: "Compatibility shorthand for target.registryRoot." },
        },
    }),
    outputSchema: jsonSchema("skill.ripgrep.output", { type: "object", additionalProperties: true }),
});
export const skillRipgrepHandler = createSkillBaseCoreHandler(skillRipgrepBaseToolDefinition, async (request) => {
    const normalized = normalizeSkillRipgrepRequest(request.input);
    const selection = selectSkillRipgrepPractice({ ...normalized, executor: request.executor });
    return executeSkillRipgrepCore({ ...normalized, provider: selection.provider, context: { ...normalized.context, runtimeId: normalized.context?.runtimeId ?? request.runtimeId, sessionId: normalized.context?.sessionId ?? request.sessionId, invocationId: normalized.context?.invocationId ?? request.toolCallId, auditMetadata: injectRuntimeInvocationMetadata(buildSkillBasePracticeAuditMetadata({ 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 }), normalized.context?.auditMetadata, request) } });
});
