import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicFullscreenScreenshotPractice } from "./anthropic.js";
import { deepmindFullscreenScreenshotPractice } from "./deepmind.js";
import { fullscreenScreenshotDependencyDeclarations, } from "./dependencies.js";
import { openaiFullscreenScreenshotPractice } from "./openai.js";
import { executeFullscreenScreenshot as executeFullscreenScreenshotCore, fullscreenScreenshotDescriptor, planFullscreenScreenshot, } from "./core.js";
export const fullscreenScreenshotProviderPractices = [
    anthropicFullscreenScreenshotPractice,
    openaiFullscreenScreenshotPractice,
    deepmindFullscreenScreenshotPractice,
];
export const fullscreenScreenshotBestPracticeDescriptor = {
    toolId: "computeruse.fullscreenScreenshot",
    bestPractice: "storage-owned-runtime-computeruse-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: fullscreenScreenshotDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse 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 selectFullscreenScreenshotPractice(dependencies = {}) {
    return selectComputerUseProviderPractice(fullscreenScreenshotProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeFullscreenScreenshot(request = {}) {
    const selection = selectFullscreenScreenshotPractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeFullscreenScreenshotCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const fullscreenScreenshotBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: fullscreenScreenshotDescriptor.toolId,
    title: "Computer Use Fullscreen Screenshot",
    description: "Capture a fullscreen screenshot through governed runtime computer-use support.",
    summary: "Use computeruse.fullscreenScreenshot to request a runtime-owned fullscreen screenshot artifact.",
    storageGroup: "screenshot",
    riskLevel: "risky",
    permissionHints: ["device:screen", "screen:read", "display:capture"],
    dependencies: fullscreenScreenshotDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.fullscreenScreenshot.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    displayId: { type: "string" },
                    outputFormat: { type: "string", enum: ["image/png", "image/jpeg", "image/webp"] },
                },
            },
            displayId: { type: "string" },
            outputFormat: { type: "string", enum: ["image/png", "image/jpeg", "image/webp"] },
            purpose: { type: "string" },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.fullscreenScreenshot.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "purpose", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "captureEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.fullscreenScreenshot" },
            target: { type: "object" },
            purpose: { type: "string" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            captureEnvelope: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: true,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
    metadata: {
        invocationEntryPath: "src/agentCore/agent_executionEngine/basic_toolLayer/baseTools/computeruseBase/screenshot/computeruse.fullscreenScreenshot.ts",
        runtimeProviderDependencies: {
            linux: ["runtime.desktop.screenshotProvider.linux"],
        },
    },
});
export const fullscreenScreenshotHandler = createComputerUseCoreHandler(fullscreenScreenshotBaseToolDefinition, async (request) => {
    const rawInput = request.input;
    if (!isRecord(rawInput)) {
        return executeFullscreenScreenshotCore(rawInput);
    }
    const selection = selectFullscreenScreenshotPractice({
        ...rawInput,
        executor: request.executor,
    });
    const auditMetadata = injectRuntimeInvocationMetadata(buildComputerUsePracticeAuditMetadata(selection), isRecord(rawInput.context) && isRecord(rawInput.context.auditMetadata)
        ? rawInput.context.auditMetadata
        : undefined, request);
    return executeFullscreenScreenshotCore({
        ...rawInput,
        provider: selection.provider,
        context: {
            ...(isRecord(rawInput.context) ? rawInput.context : {}),
            runtimeId: isRecord(rawInput.context) && typeof rawInput.context.runtimeId === "string"
                ? rawInput.context.runtimeId
                : request.runtimeId,
            sessionId: isRecord(rawInput.context) && typeof rawInput.context.sessionId === "string"
                ? rawInput.context.sessionId
                : request.sessionId,
            invocationId: isRecord(rawInput.context) && typeof rawInput.context.invocationId === "string"
                ? rawInput.context.invocationId
                : request.toolCallId,
            auditMetadata,
        },
    });
});
export { executeFullscreenScreenshotCore, fullscreenScreenshotDescriptor, planFullscreenScreenshot };
