const publicSafeProviderFailurePrefix = "PUBLIC_SAFE_PROVIDER_FAILURE:";
export const mouseEmulationDependencyDeclarations = [
    {
        dependencyId: "runtime.execEngine.computeruse.locateCursor",
        kind: "runtime",
        required: true,
        description: "Runtime-owned cursor observation support exposed through BaseToolExecutorPort.computeruse.locateCursor.",
    },
    {
        dependencyId: "runtime.execEngine.computeruse.pointerAction",
        kind: "runtime",
        required: true,
        description: "Runtime-owned pointer action support exposed through BaseToolExecutorPort.computeruse.pointerAction.",
    },
    {
        dependencyId: "runtime.governancePlane.toolInvocationGrant",
        kind: "permission",
        required: true,
        description: "dryRun:false requires an affirmative runtime guard before pointer sequence dispatch.",
    },
    {
        dependencyId: "runtime.inputController.pointer",
        kind: "runtime",
        required: true,
        description: "Runtime owns OS pointer events, cursor reads, coordinate translation, focus, cleanup, and input policy.",
    },
];
export function createRuntimeMouseEmulationProvider(executor) {
    const locateCursor = executor?.computeruse?.locateCursor;
    const pointerAction = executor?.computeruse?.pointerAction;
    if (locateCursor === undefined || pointerAction === undefined)
        return undefined;
    return async (request) => {
        const stepResults = [];
        for (const [index, step] of request.steps.entries()) {
            if (step.kind === "locate") {
                const result = await locateCursor({
                    coordinateSpace: step.coordinateSpace,
                    metadata: {
                        runtimeId: request.context.runtimeId,
                        sessionId: request.context.sessionId,
                        invocationId: request.context.invocationId,
                        purpose: request.purpose,
                        stepIndex: index,
                        runtimeGuardAccepted: true,
                        displayId: step.displayId,
                        auditMetadata: request.context.auditMetadata,
                    },
                });
                if (!result.ok) {
                    throw new Error(`${publicSafeProviderFailurePrefix}${result.error.message}`);
                }
                stepResults.push({
                    index,
                    kind: "locate",
                    position: {
                        x: result.output.x,
                        y: result.output.y,
                        coordinateSpace: result.output.coordinateSpace,
                        displayId: step.displayId,
                    },
                    metadata: result.metadata,
                });
                continue;
            }
            const result = await pointerAction({
                action: step.kind,
                target: pointerTargetForStep(step),
                metadata: {
                    runtimeId: request.context.runtimeId,
                    sessionId: request.context.sessionId,
                    invocationId: request.context.invocationId,
                    purpose: request.purpose,
                    stepIndex: index,
                    runtimeGuardAccepted: true,
                    auditMetadata: request.context.auditMetadata,
                },
            });
            if (!result.ok) {
                throw new Error(`${publicSafeProviderFailurePrefix}${result.error.message}`);
            }
            stepResults.push({
                index,
                kind: step.kind,
                actionId: result.output.actionId,
                metadata: {
                    ...(result.output.metadata ?? {}),
                    ...(result.metadata ?? {}),
                },
            });
        }
        return {
            stepResults,
            metadata: {
                stepCount: request.steps.length,
            },
        };
    };
}
function pointerTargetForStep(step) {
    if (step.kind === "move") {
        return {
            x: step.target.x,
            y: step.target.y,
            coordinateSpace: step.coordinateSpace,
            displayId: step.displayId,
            windowId: step.windowId,
            durationMs: step.durationMs,
        };
    }
    return {
        button: step.button,
        clickCount: step.clickCount,
        at: step.at,
        coordinateSpace: step.coordinateSpace,
        displayId: step.displayId,
        windowId: step.windowId,
        usesCurrentCursor: step.usesCurrentCursor,
    };
}
