import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicCursorLocatePractice } from "./anthropic.js";
import { deepmindCursorLocatePractice } from "./deepmind.js";
import { cursorLocateDependencyDeclarations, } from "./dependencies.js";
import { openaiCursorLocatePractice } from "./openai.js";
import { cursorLocateDescriptor, executeCursorLocate as executeCursorLocateCore, planCursorLocate, } from "./core.js";
export const cursorLocateProviderPractices = [
    anthropicCursorLocatePractice,
    openaiCursorLocatePractice,
    deepmindCursorLocatePractice,
];
export const cursorLocateBestPracticeDescriptor = {
    toolId: "computeruse.cursorLocate",
    bestPractice: "storage-owned-runtime-computeruse-cursor-locate-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: cursorLocateDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse cursor provider is available; dry-run remains available."],
    createProvider: () => undefined,
};
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function mergeContextWithAuditMetadata(context, auditMetadata) {
    if (!isRecord(context)) {
        return { auditMetadata };
    }
    return {
        ...context,
        auditMetadata: {
            ...(isRecord(context.auditMetadata) ? context.auditMetadata : {}),
            ...auditMetadata,
        },
    };
}
export function selectCursorLocatePractice(dependencies = {}) {
    return selectComputerUseProviderPractice(cursorLocateProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeCursorLocate(request = {}) {
    const selection = selectCursorLocatePractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeCursorLocateCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const cursorLocateBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: cursorLocateDescriptor.toolId,
    title: "Computer Use Cursor Locate",
    description: "Locate the current cursor through governed runtime computer-use support.",
    summary: "Use computeruse.cursorLocate to request a runtime-owned cursor position observation.",
    storageGroup: "mouseEmulation",
    riskLevel: "normal",
    permissionHints: ["device:pointer", "pointer:read", "ui:observe"],
    dependencies: cursorLocateDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.cursorLocate.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    coordinateSpace: { type: "string", enum: ["screen", "window", "normalized"] },
                    displayId: { type: "string" },
                },
            },
            coordinateSpace: { type: "string", enum: ["screen", "window", "normalized"] },
            displayId: { type: "string" },
            purpose: { type: "string" },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.cursorLocate.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "purpose", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "observationEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.cursorLocate" },
            target: { type: "object" },
            purpose: { type: "string" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            observationEnvelope: { type: "object" },
            position: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: false,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
});
export const cursorLocateHandler = createComputerUseCoreHandler(cursorLocateBaseToolDefinition, async (request) => {
    const selection = selectCursorLocatePractice({
        ...request.input,
        executor: request.executor,
    });
    const auditMetadata = injectRuntimeInvocationMetadata(buildComputerUsePracticeAuditMetadata(selection), isRecord(request.input.context) && isRecord(request.input.context.auditMetadata)
        ? request.input.context.auditMetadata
        : undefined, request);
    return executeCursorLocateCore({
        ...request.input,
        provider: selection.provider,
        context: {
            ...(isRecord(request.input.context) ? request.input.context : {}),
            runtimeId: isRecord(request.input.context) && typeof request.input.context.runtimeId === "string"
                ? request.input.context.runtimeId
                : request.runtimeId,
            sessionId: isRecord(request.input.context) && typeof request.input.context.sessionId === "string"
                ? request.input.context.sessionId
                : request.sessionId,
            invocationId: isRecord(request.input.context) && typeof request.input.context.invocationId === "string"
                ? request.input.context.invocationId
                : request.toolCallId,
            auditMetadata,
        },
    });
});
export { cursorLocateDescriptor, executeCursorLocateCore, planCursorLocate };
