import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicCameraPermissionReleasePractice } from "./anthropic.js";
import { deepmindCameraPermissionReleasePractice } from "./deepmind.js";
import { cameraPermissionReleaseDependencyDeclarations, } from "./dependencies.js";
import { openaiCameraPermissionReleasePractice } from "./openai.js";
import { cameraPermissionReleaseDescriptor, executeCameraPermissionRelease as executeCameraPermissionReleaseCore, planCameraPermissionRelease, } from "./core.js";
export const cameraPermissionReleaseProviderPractices = [
    anthropicCameraPermissionReleasePractice,
    openaiCameraPermissionReleasePractice,
    deepmindCameraPermissionReleasePractice,
];
export const cameraPermissionReleaseBestPracticeDescriptor = {
    toolId: "computeruse.cameraPermissionRelease",
    bestPractice: "storage-owned-runtime-computeruse-camera-permission-release-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: cameraPermissionReleaseDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse camera permission release 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 selectCameraPermissionReleasePractice(dependencies = {}) {
    return selectComputerUseProviderPractice(cameraPermissionReleaseProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeCameraPermissionRelease(request = {}) {
    const selection = selectCameraPermissionReleasePractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeCameraPermissionReleaseCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const cameraPermissionReleaseBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: cameraPermissionReleaseDescriptor.toolId,
    title: "Computer Use Camera Permission Release",
    description: "Release a camera permission lease through governed runtime computer-use support.",
    summary: "Use computeruse.cameraPermissionRelease to ask runtime to release a camera permission lease.",
    storageGroup: "cameraAccess",
    riskLevel: "risky",
    permissionHints: ["device:camera", "camera:permission-release"],
    dependencies: cameraPermissionReleaseDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.cameraPermissionRelease.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    leaseId: { type: "string" },
                    permissionToken: { type: "string" },
                    deviceId: { type: "string" },
                    reason: { type: "string" },
                },
            },
            leaseId: { type: "string" },
            permissionToken: { type: "string" },
            deviceId: { type: "string" },
            reason: { type: "string" },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.cameraPermissionRelease.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "permissionEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.cameraPermissionRelease" },
            target: { type: "object" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            permissionEnvelope: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: false,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
});
export const cameraPermissionReleaseHandler = createComputerUseCoreHandler(cameraPermissionReleaseBaseToolDefinition, async (request) => {
    const selection = selectCameraPermissionReleasePractice({
        ...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 executeCameraPermissionReleaseCore({
        ...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 { cameraPermissionReleaseDescriptor, executeCameraPermissionReleaseCore, planCameraPermissionRelease, };
