import { buildComputerUsePracticeAuditMetadata, createComputerUseBaseToolDefinition, createComputerUseCoreHandler, injectRuntimeInvocationMetadata, jsonSchema, selectComputerUseProviderPractice, } from "../../_shared/baseToolAdapter.js";
import { anthropicMicrophoneSelectPractice } from "./anthropic.js";
import { deepmindMicrophoneSelectPractice } from "./deepmind.js";
import { microphoneSelectDependencyDeclarations, } from "./dependencies.js";
import { openaiMicrophoneSelectPractice } from "./openai.js";
import { executeMicrophoneSelect as executeMicrophoneSelectCore, microphoneSelectDescriptor, planMicrophoneSelect, } from "./core.js";
export const microphoneSelectProviderPractices = [
    anthropicMicrophoneSelectPractice,
    openaiMicrophoneSelectPractice,
    deepmindMicrophoneSelectPractice,
];
export const microphoneSelectBestPracticeDescriptor = {
    toolId: "computeruse.microphoneSelect",
    bestPractice: "storage-owned-runtime-computeruse-microphone-select-provider-practice-evidence",
    sourcePriority: ["cli", "agent-sdk", "api-sdk", "praxis-native"],
    providerOrder: ["anthropic", "openai", "deepmind"],
    dependencies: microphoneSelectDependencyDeclarations,
};
const praxisNativeFallbackPractice = {
    providerName: "praxis-native",
    source: {
        kind: "praxis-native",
        label: "Praxis dry-run fallback",
    },
    directCliSupport: false,
    sideEffectPolicy: "runtime-governed",
    notes: ["No runtime computeruse microphone select 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 selectMicrophoneSelectPractice(dependencies = {}) {
    return selectComputerUseProviderPractice(microphoneSelectProviderPractices, dependencies, praxisNativeFallbackPractice);
}
export async function executeMicrophoneSelect(request = {}) {
    const selection = selectMicrophoneSelectPractice({
        executor: request.executor,
        provider: request.provider,
        preferredProvider: request.preferredProvider,
    });
    const auditMetadata = buildComputerUsePracticeAuditMetadata(selection);
    return executeMicrophoneSelectCore({
        ...request,
        provider: selection.provider,
        context: mergeContextWithAuditMetadata(request.context, auditMetadata),
    });
}
export const microphoneSelectBaseToolDefinition = createComputerUseBaseToolDefinition({
    toolId: microphoneSelectDescriptor.toolId,
    title: "Computer Use Microphone Select",
    description: "Select a microphone device through governed runtime computer-use support.",
    summary: "Use computeruse.microphoneSelect to ask runtime to select a microphone device for a session or app target.",
    storageGroup: "microphoneAccess",
    riskLevel: "risky",
    permissionHints: ["device:microphone", "microphone:select"],
    dependencies: microphoneSelectDependencyDeclarations,
    inputSchema: jsonSchema("computeruse.microphoneSelect.input", {
        type: "object",
        additionalProperties: true,
        properties: {
            target: {
                type: "object",
                additionalProperties: true,
                properties: {
                    deviceId: { type: "string" },
                    targetApplication: { type: "string" },
                    permissionLeaseId: { type: "string" },
                    selectionReason: { type: "string" },
                    availableDevices: { type: "array", items: { type: "object", additionalProperties: true } },
                },
            },
            deviceId: { type: "string" },
            targetApplication: { type: "string" },
            permissionLeaseId: { type: "string" },
            selectionReason: { type: "string" },
            availableDevices: { type: "array", items: { type: "object", additionalProperties: true } },
            context: { type: "object", additionalProperties: true },
            preferredProvider: { type: "string", enum: ["anthropic", "openai", "deepmind", "praxis-native"] },
        },
    }),
    outputSchema: jsonSchema("computeruse.microphoneSelect.output", {
        type: "object",
        additionalProperties: true,
        required: ["kind", "target", "dispatch", "dryRun", "providerCalled", "runtimeEntry", "selectionEnvelope"],
        properties: {
            kind: { const: "agentCore.basicTool.computeruse.microphoneSelect" },
            target: { type: "object" },
            dispatch: { type: "string", enum: ["dry-run", "runtime-computeruse"] },
            dryRun: { type: "boolean" },
            providerCalled: { type: "boolean" },
            runtimeEntry: { type: "object" },
            selectionEnvelope: { type: "object" },
        },
    }),
    storagePolicy: {
        storesMaterial: false,
        storesResult: true,
        storesAudit: true,
        reusable: false,
    },
});
export const microphoneSelectHandler = createComputerUseCoreHandler(microphoneSelectBaseToolDefinition, async (request) => {
    const selection = selectMicrophoneSelectPractice({
        ...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 executeMicrophoneSelectCore({
        ...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 { executeMicrophoneSelectCore, microphoneSelectDescriptor, planMicrophoneSelect, };
