import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicFullscreenScreenRecordingPractice } from "./anthropic.js";
import { deepmindFullscreenScreenRecordingPractice } from "./deepmind.js";
import { fullscreenScreenRecordingDependencyDeclarations, } from "./dependencies.js";
import { openaiFullscreenScreenRecordingPractice } from "./openai.js";
import { executeFullscreenScreenRecording as executeFullscreenScreenRecordingCore, fullscreenScreenRecordingDescriptor, planFullscreenScreenRecording, } from "./core.js";
export const fullscreenScreenRecordingProviderPractices = [
    anthropicFullscreenScreenRecordingPractice,
    openaiFullscreenScreenRecordingPractice,
    deepmindFullscreenScreenRecordingPractice,
];
export const fullscreenScreenRecordingBestPracticeDescriptor = {
    toolId: "computeruse.fullscreenScreenRecording",
    bestPractice: "storage-owned-runtime-computeruse-recording-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: fullscreenScreenRecordingDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse recording 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 selectFullscreenScreenRecordingPractice(dependencies = {}) {
    return selectComputerUseProviderPractice(fullscreenScreenRecordingProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeFullscreenScreenRecording(request = {}) {
    const selection = selectFullscreenScreenRecordingPractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeFullscreenScreenRecordingCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const fullscreenScreenRecordingBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: fullscreenScreenRecordingDescriptor.toolId,
    title: "Computer Use Fullscreen Screen Recording",
    description: "Start a fullscreen screen recording session through governed runtime computer-use support.",
    summary: "Use computeruse.fullscreenScreenRecording to request a runtime-owned fullscreen recording session handle.",
    storageGroup: "screenRecording",
    riskLevel: "risky",
    permissionHints: ["device:screen", "screen:record", "display:capture"],
    dependencies: fullscreenScreenRecordingDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.fullscreenScreenRecording.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    displayId: { type: "string" },
                    maxDurationMs: { type: "integer", minimum: 1, maximum: fullscreenScreenRecordingDescriptor.maxDurationMs },
                    includeCursor: { type: "boolean" },
                    includeAudio: { type: "boolean" },
                    outputFormat: { type: "string", enum: ["video/webm", "video/mp4", "video/quicktime"] },
                    destinationHint: { type: "string" },
                },
            },
            displayId: { type: "string" },
            maxDurationMs: { type: "integer", minimum: 1, maximum: fullscreenScreenRecordingDescriptor.maxDurationMs },
            includeCursor: { type: "boolean" },
            includeAudio: { type: "boolean" },
            outputFormat: { type: "string", enum: ["video/webm", "video/mp4", "video/quicktime"] },
            destinationHint: { type: "string" },
            purpose: { type: "string" },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.fullscreenScreenRecording.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "purpose", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "recordingEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.fullscreenScreenRecording" },
            target: { type: "object" },
            purpose: { type: "string" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            recordingEnvelope: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: true,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
});
export const fullscreenScreenRecordingHandler = createComputerUseCoreHandler(fullscreenScreenRecordingBaseToolDefinition, async (request) => {
    const selection = selectFullscreenScreenRecordingPractice({
        ...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 executeFullscreenScreenRecordingCore({
        ...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 { executeFullscreenScreenRecordingCore, fullscreenScreenRecordingDescriptor, planFullscreenScreenRecording };
