import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicCameraStopRecordingPractice } from "./anthropic.js";
import { deepmindCameraStopRecordingPractice } from "./deepmind.js";
import { cameraStopRecordingDependencyDeclarations, } from "./dependencies.js";
import { openaiCameraStopRecordingPractice } from "./openai.js";
import { cameraStopRecordingDescriptor, executeCameraStopRecording as executeCameraStopRecordingCore, planCameraStopRecording, } from "./core.js";
export const cameraStopRecordingProviderPractices = [
    anthropicCameraStopRecordingPractice,
    openaiCameraStopRecordingPractice,
    deepmindCameraStopRecordingPractice,
];
export const cameraStopRecordingBestPracticeDescriptor = {
    toolId: "computeruse.cameraStopRecording",
    bestPractice: "storage-owned-runtime-computeruse-camera-stop-recording-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: cameraStopRecordingDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse camera recording stop 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 selectCameraStopRecordingPractice(dependencies = {}) {
    return selectComputerUseProviderPractice(cameraStopRecordingProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeCameraStopRecording(request = {}) {
    const selection = selectCameraStopRecordingPractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeCameraStopRecordingCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const cameraStopRecordingBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: cameraStopRecordingDescriptor.toolId,
    title: "Computer Use Camera Stop Recording",
    description: "Stop a camera recording session through governed runtime computer-use support and return a video artifact reference.",
    summary: "Use computeruse.cameraStopRecording to ask runtime to stop a camera recording session handle.",
    storageGroup: "cameraAccess",
    riskLevel: "risky",
    permissionHints: ["device:camera", "camera:stop-recording", "recording:session", "artifact:write"],
    dependencies: cameraStopRecordingDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.cameraStopRecording.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    recordingId: { type: "string" },
                    recordingRef: { type: "string" },
                    purpose: { type: "string" },
                    storageTarget: { type: "string" },
                    retentionPolicy: { type: "string", enum: ["ephemeral", "session-only", "session-scoped", "persistent"] },
                    destinationHint: { type: "string" },
                },
            },
            recordingId: { type: "string" },
            recordingRef: { type: "string" },
            purpose: { type: "string" },
            storageTarget: { type: "string" },
            retentionPolicy: { type: "string", enum: ["ephemeral", "session-only", "session-scoped", "persistent"] },
            destinationHint: { type: "string" },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.cameraStopRecording.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "artifactEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.cameraStopRecording" },
            target: { type: "object" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            artifactEnvelope: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: true,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
});
export const cameraStopRecordingHandler = createComputerUseCoreHandler(cameraStopRecordingBaseToolDefinition, async (request) => {
    const selection = selectCameraStopRecordingPractice({
        ...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 executeCameraStopRecordingCore({
        ...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 { cameraStopRecordingDescriptor, executeCameraStopRecordingCore, planCameraStopRecording, };
