export const checkboxConfirmDescriptor = {
    toolId: "computeruse.checkboxConfirm",
    capability: "confirm-mouse-checkbox",
    route: "agent_executionEngine.basic_toolLayer.baseTools.computeruseBase.mouseEmulation",
    defaultDryRun: true,
    defaultExpectedState: "checked",
    defaultClickMode: "single-click",
    defaultCoordinateSpace: "screen",
    maxCoordinate: 100_000,
    tapOwnsApproval: true,
    runtimeEntry: "BaseToolExecutorPort.computeruse.pointerAction",
    permissionsRequired: ["pointer:write", "ui:action"],
    unsafeSideEffects: true,
};
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cleanString(value) {
    return typeof value === "string" && value.trim().length > 0 && !value.includes("\0") ? value.trim() : undefined;
}
function cleanStringList(value) {
    if (value === undefined)
        return undefined;
    if (!Array.isArray(value))
        return undefined;
    const cleaned = [];
    for (const item of value) {
        const text = cleanString(item);
        if (text === undefined)
            return undefined;
        if (!cleaned.includes(text))
            cleaned.push(text);
    }
    return cleaned;
}
function cleanGate(value) {
    if (value === undefined)
        return undefined;
    if (!isRecord(value))
        return undefined;
    const gate = {};
    if (typeof value.accepted === "boolean")
        gate.accepted = value.accepted;
    if (typeof value.allowed === "boolean")
        gate.allowed = value.allowed;
    const reason = cleanString(value.reason);
    if (reason !== undefined)
        gate.reason = reason;
    return gate;
}
function cleanAuditMetadata(value) {
    return isRecord(value) ? value : undefined;
}
function cleanState(value, fallback) {
    if (value === undefined)
        return fallback;
    return value === "checked" || value === "unchecked" ? value : undefined;
}
function cleanClickMode(value) {
    if (value === undefined)
        return checkboxConfirmDescriptor.defaultClickMode;
    return value === "single-click" || value === "double-click" ? value : undefined;
}
function cleanCoordinateSpace(value) {
    if (value === undefined)
        return checkboxConfirmDescriptor.defaultCoordinateSpace;
    return value === "screen" || value === "window" || value === "normalized" ? value : undefined;
}
function cleanCoordinate(value) {
    return typeof value === "number" &&
        Number.isFinite(value) &&
        value >= 0 &&
        value <= checkboxConfirmDescriptor.maxCoordinate
        ? Math.round(value)
        : undefined;
}
function auditEvent(type, context, metadata) {
    return {
        type,
        toolId: checkboxConfirmDescriptor.toolId,
        invocationId: context?.invocationId ?? "computeruse.checkboxConfirm:dry-run",
        dryRun: context?.dryRun !== false,
        metadata: {
            ...(context?.auditMetadata ?? {}),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context) {
    return {
        ok: false,
        toolId: checkboxConfirmDescriptor.toolId,
        error: {
            code,
            message,
            boundary,
            publicSafe: true,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        audit: [auditEvent("agentCore.basicTool.computeruse.checkboxConfirm.rejected", context, { code })],
        events: ["basicTool.computeruse.checkboxConfirm.rejected"],
    };
}
function normalizeContext(value) {
    if (value === undefined)
        return {};
    if (!isRecord(value))
        return failure("INVALID_CONTEXT", "computeruse.checkboxConfirm context must be an object", "input", undefined);
    const requestedScopes = cleanStringList(value.requestedScopes);
    const allowedScopes = cleanStringList(value.allowedScopes);
    const guard = cleanGate(value.guard);
    const contract = cleanGate(value.contract);
    const governance = cleanGate(value.governance);
    if ((value.requestedScopes !== undefined && requestedScopes === undefined) ||
        (value.allowedScopes !== undefined && allowedScopes === undefined) ||
        (value.guard !== undefined && guard === undefined) ||
        (value.contract !== undefined && contract === undefined) ||
        (value.governance !== undefined && governance === undefined) ||
        (value.dryRun !== undefined && typeof value.dryRun !== "boolean")) {
        return failure("INVALID_CONTEXT", "computeruse.checkboxConfirm context contains malformed guard, governance, or scope fields", "input", undefined);
    }
    return {
        runtimeId: cleanString(value.runtimeId),
        sessionId: cleanString(value.sessionId),
        invocationId: cleanString(value.invocationId),
        dryRun: typeof value.dryRun === "boolean" ? value.dryRun : undefined,
        guard,
        contract,
        governance,
        requestedScopes,
        allowedScopes,
        auditMetadata: cleanAuditMetadata(value.auditMetadata),
    };
}
function normalizePoint(value, context) {
    if (value === undefined)
        return undefined;
    if (!isRecord(value))
        return failure("INVALID_POINT", "computeruse.checkboxConfirm point must be an object when provided", "input", context);
    const x = cleanCoordinate(value.x);
    const y = cleanCoordinate(value.y);
    if (x === undefined || y === undefined) {
        return failure("INVALID_POINT", "computeruse.checkboxConfirm point must use finite non-negative coordinates", "input", context);
    }
    return { x, y };
}
function normalizeTarget(request, context) {
    const targetValue = request.target;
    if (targetValue !== undefined && !isRecord(targetValue)) {
        return failure("INVALID_TARGET", "computeruse.checkboxConfirm target must be an object when provided", "input", context);
    }
    const target = isRecord(targetValue) ? targetValue : {};
    const label = cleanString(target.label ?? request.label);
    if ((target.label ?? request.label) !== undefined && label === undefined) {
        return failure("INVALID_TARGET", "computeruse.checkboxConfirm label must be a safe string", "input", context);
    }
    const selectorHint = cleanString(target.selectorHint ?? request.selectorHint);
    if ((target.selectorHint ?? request.selectorHint) !== undefined && selectorHint === undefined) {
        return failure("INVALID_TARGET", "computeruse.checkboxConfirm selectorHint must be a safe string", "input", context);
    }
    const point = normalizePoint(target.point ?? request.point, context);
    if (point !== undefined && "ok" in point)
        return point;
    if (label === undefined && selectorHint === undefined && point === undefined) {
        return failure("MISSING_TARGET", "computeruse.checkboxConfirm requires label, selectorHint, or point", "input", context);
    }
    const expectedState = cleanState(target.expectedState ?? request.expectedState, checkboxConfirmDescriptor.defaultExpectedState);
    if (expectedState === undefined) {
        return failure("INVALID_STATE", "computeruse.checkboxConfirm expectedState must be checked or unchecked", "input", context);
    }
    const currentState = cleanState(target.currentState ?? request.currentState);
    if ((target.currentState ?? request.currentState) !== undefined && currentState === undefined) {
        return failure("INVALID_STATE", "computeruse.checkboxConfirm currentState must be checked or unchecked", "input", context);
    }
    const clickMode = cleanClickMode(target.clickMode ?? request.clickMode);
    if (clickMode === undefined) {
        return failure("INVALID_CLICK_MODE", "computeruse.checkboxConfirm clickMode must be single-click or double-click", "input", context);
    }
    const coordinateSpace = cleanCoordinateSpace(target.coordinateSpace ?? request.coordinateSpace);
    if (coordinateSpace === undefined) {
        return failure("INVALID_COORDINATE_SPACE", "computeruse.checkboxConfirm coordinateSpace must be screen, window, or normalized", "input", context);
    }
    const displayId = cleanString(target.displayId ?? request.displayId);
    if ((target.displayId ?? request.displayId) !== undefined && displayId === undefined) {
        return failure("INVALID_DISPLAY_ID", "computeruse.checkboxConfirm displayId must be a safe string", "input", context);
    }
    const windowId = cleanString(target.windowId ?? request.windowId);
    if ((target.windowId ?? request.windowId) !== undefined && windowId === undefined) {
        return failure("INVALID_WINDOW_ID", "computeruse.checkboxConfirm windowId must be a safe string", "input", context);
    }
    return {
        expectedState,
        currentState,
        label,
        selectorHint,
        point,
        coordinateSpace,
        displayId,
        windowId,
        clickMode,
        clickCount: clickMode === "single-click" ? 1 : 2,
        wouldToggle: currentState === undefined || currentState !== expectedState,
    };
}
function ensureScopes(context) {
    const requested = context.requestedScopes ?? [];
    const allowed = context.allowedScopes ?? [];
    if (requested.length === 0)
        return undefined;
    const denied = requested.filter((scope) => !allowed.includes(scope));
    if (denied.length === 0)
        return undefined;
    return failure("SCOPE_DENIED", `computeruse.checkboxConfirm scope ${denied[0]} is outside runtime governance`, "scope", context);
}
function ensureStaticGates(context) {
    if (context.contract?.accepted === false || context.contract?.allowed === false) {
        return failure("CONTRACT_REJECTED", context.contract.reason ?? "computeruse.checkboxConfirm was rejected by runtime contract surface", "contract", context);
    }
    if (context.governance?.accepted === false || context.governance?.allowed === false) {
        return failure("GOVERNANCE_REJECTED", context.governance.reason ?? "computeruse.checkboxConfirm was rejected by runtime governance", "governance", context);
    }
    return undefined;
}
function ensureRealExecutionGuard(context) {
    if (context.dryRun !== false)
        return undefined;
    if (context.guard?.accepted === true || context.guard?.allowed === true)
        return undefined;
    return failure("GOVERNANCE_REJECTED", context.guard?.reason ?? "computeruse.checkboxConfirm dryRun:false requires an affirmative runtime guard", "governance", context);
}
function baseOutput(target, purpose, acceptedScopes, dryRun, providerCalled) {
    return {
        kind: "agentCore.basicTool.computeruse.checkboxConfirm",
        target,
        purpose,
        dispatch: dryRun ? "dry-run" : "runtime-computeruse",
        dryRun,
        providerCalled,
        executionBlocked: dryRun,
        unsafeSideEffects: true,
        permissionsRequired: checkboxConfirmDescriptor.permissionsRequired,
        requiresTapApproval: true,
        acceptedScopes,
        runtimeEntry: {
            port: "BaseToolExecutorPort.computeruse.pointerAction",
            operation: "computeruse.checkboxConfirm.pointerAction",
            runtimeOwnsPointerEvents: true,
            runtimeOwnsInputPolicy: true,
            baseToolOwnsTapStrategy: false,
        },
    };
}
function normalizeProviderResult(value, context) {
    if (!isRecord(value) || cleanString(value.actionId) === undefined) {
        return failure("PROVIDER_FAILURE", "computeruse.checkboxConfirm runtime provider returned a malformed public-safe action envelope", "provider", context);
    }
    const finalState = cleanState(value.finalState);
    if (value.finalState !== undefined && finalState === undefined) {
        return failure("PROVIDER_FAILURE", "computeruse.checkboxConfirm runtime provider returned an invalid final state", "provider", context);
    }
    return {
        actionId: cleanString(value.actionId) ?? "",
        finalState,
        metadata: cleanAuditMetadata(value.metadata),
    };
}
const publicSafeProviderFailurePrefix = "PUBLIC_SAFE_PROVIDER_FAILURE:";
function publicSafeProviderFailureMessage(error) {
    const message = error instanceof Error && error.message.trim().length > 0 ? error.message.trim() : undefined;
    if (message === undefined || !message.startsWith(publicSafeProviderFailurePrefix))
        return undefined;
    const publicSafeMessage = message.slice(publicSafeProviderFailurePrefix.length).trim();
    return publicSafeMessage.length > 0 ? publicSafeMessage : undefined;
}
function normalizeRequest(request) {
    if (request === undefined)
        request = {};
    if (!isRecord(request))
        return failure("INVALID_REQUEST", "computeruse.checkboxConfirm request must be an object", "input", undefined);
    const context = normalizeContext(request.context);
    if ("ok" in context)
        return context;
    const target = normalizeTarget(request, context);
    if ("ok" in target)
        return target;
    const purpose = cleanString(request.purpose);
    if (purpose === undefined) {
        return failure("MISSING_PURPOSE", "computeruse.checkboxConfirm requires an explicit purpose", "input", context);
    }
    if (context.runtimeId === undefined) {
        return failure("MISSING_RUNTIME_ID", "computeruse.checkboxConfirm requires context.runtimeId for audit", "input", context);
    }
    const scopes = ensureScopes(context);
    if (scopes !== undefined)
        return scopes;
    const staticGates = ensureStaticGates(context);
    if (staticGates !== undefined)
        return staticGates;
    const realGuard = ensureRealExecutionGuard(context);
    if (realGuard !== undefined)
        return realGuard;
    return {
        target,
        context,
        purpose,
        metadata: cleanAuditMetadata(request.metadata),
        provider: typeof request.provider === "function" ? request.provider : undefined,
    };
}
export async function executeCheckboxConfirm(request = {}) {
    const normalized = normalizeRequest(request);
    if ("ok" in normalized)
        return normalized;
    const { target, context, purpose, metadata, provider } = normalized;
    const acceptedScopes = context.requestedScopes ?? [];
    const dryRun = context.dryRun !== false;
    if (dryRun) {
        return {
            ok: true,
            toolId: checkboxConfirmDescriptor.toolId,
            output: {
                ...baseOutput(target, purpose, acceptedScopes, true, false),
                actionEnvelope: {
                    resource: "pointer",
                    action: "confirm",
                    executed: false,
                    metadataOnly: true,
                },
            },
            audit: [auditEvent("agentCore.basicTool.computeruse.checkboxConfirm.dryRun", context, metadata)],
            events: ["basicTool.computeruse.checkboxConfirm.dryRun"],
        };
    }
    if (provider === undefined) {
        return failure("PROVIDER_UNAVAILABLE", "computeruse.checkboxConfirm requires runtime executor.computeruse.pointerAction for dryRun:false", "provider", context);
    }
    let providerResult;
    try {
        const result = await provider({
            operation: "computeruse.checkboxConfirm.pointerAction",
            target,
            purpose,
            context: {
                runtimeId: context.runtimeId,
                sessionId: context.sessionId,
                invocationId: context.invocationId,
                auditMetadata: {
                    ...(context.auditMetadata ?? {}),
                    ...(metadata ?? {}),
                },
            },
        });
        const normalizedResult = normalizeProviderResult(result, context);
        if ("ok" in normalizedResult)
            return normalizedResult;
        providerResult = normalizedResult;
    }
    catch (error) {
        const providerMessage = publicSafeProviderFailureMessage(error);
        return failure("PROVIDER_FAILURE", providerMessage === undefined
            ? "computeruse.checkboxConfirm runtime provider failed without exposing private details"
            : `computeruse.checkboxConfirm runtime provider failed: ${providerMessage}`, "provider", context);
    }
    return {
        ok: true,
        toolId: checkboxConfirmDescriptor.toolId,
        output: {
            ...baseOutput(target, purpose, acceptedScopes, false, true),
            actionEnvelope: {
                resource: "pointer",
                action: "confirm",
                executed: true,
                metadataOnly: false,
                actionId: providerResult.actionId,
            },
            finalState: providerResult.finalState,
            providerMetadata: providerResult.metadata,
        },
        audit: [
            auditEvent("agentCore.basicTool.computeruse.checkboxConfirm.confirmed", context, {
                actionId: providerResult.actionId,
                finalState: providerResult.finalState,
                ...(providerResult.metadata ?? {}),
            }),
        ],
        events: ["basicTool.computeruse.checkboxConfirm.confirmed"],
    };
}
export function planCheckboxConfirm(request = {}) {
    return executeCheckboxConfirm(request);
}
