import { cleanStringList, guardAccepted, isJsonObject, optionalTrimmedString, } from "../../_shared/baseToolAdapter.js";
export const mcpAuthenticateDescriptor = {
    toolId: "mcp.authenticate",
    capability: "authenticate-mcp-server",
    route: "agent_executionEngine.basic_toolLayer.baseTools.mcpBase.auth",
    permissionsRequired: ["mcp:connect", "mcp:auth"],
    defaultDryRun: true,
    acceptsRawSecrets: false,
    tapOwnsApproval: true,
};
function normalizeContext(value) {
    if (value === undefined)
        return {};
    if (!isJsonObject(value)) {
        return { ok: false, code: "INVALID_CONTEXT", message: "mcp.authenticate context must be a JSON object.", boundary: "context" };
    }
    const allowedServerIds = cleanStringList(value.allowedServerIds);
    const allowedScopes = cleanStringList(value.allowedScopes);
    const grantedPermissions = cleanStringList(value.grantedPermissions);
    if ((value.allowedServerIds !== undefined && allowedServerIds === undefined) ||
        (value.allowedScopes !== undefined && allowedScopes === undefined) ||
        (value.grantedPermissions !== undefined && grantedPermissions === undefined)) {
        return {
            ok: false,
            code: "INVALID_CONTEXT",
            message: "mcp.authenticate context lists must contain strings.",
            boundary: "context",
        };
    }
    return {
        runtimeId: optionalTrimmedString(value.runtimeId),
        sessionId: optionalTrimmedString(value.sessionId),
        invocationId: optionalTrimmedString(value.invocationId),
        dryRun: typeof value.dryRun === "boolean" ? value.dryRun : undefined,
        guard: isJsonObject(value.guard) ? value.guard : undefined,
        allowedServerIds,
        allowedScopes,
        grantedPermissions,
        auditMetadata: isJsonObject(value.auditMetadata) ? value.auditMetadata : undefined,
    };
}
function normalizeAuthStrategy(value) {
    return value === "oauth" || value === "api-key" || value === "bearer-token" || value === "custom" ? value : undefined;
}
function validate(request) {
    const root = request === undefined ? {} : request;
    if (!isJsonObject(root)) {
        return { ok: false, code: "INVALID_REQUEST", message: "mcp.authenticate request must be a JSON object.", boundary: "input" };
    }
    const context = normalizeContext(root.context);
    if ("ok" in context)
        return context;
    if (!isJsonObject(root.target)) {
        return { ok: false, code: "MISSING_SERVER_ID", message: "mcp.authenticate requires target.serverId.", boundary: "input", context };
    }
    if (root.target.serverId !== undefined && typeof root.target.serverId !== "string") {
        return { ok: false, code: "INVALID_SERVER_ID", message: "mcp.authenticate target.serverId must be a string.", boundary: "input", context };
    }
    const serverId = optionalTrimmedString(root.target.serverId);
    if (serverId === undefined) {
        return { ok: false, code: "MISSING_SERVER_ID", message: "mcp.authenticate requires target.serverId.", boundary: "input", context };
    }
    const authStrategy = normalizeAuthStrategy(root.target.authStrategy);
    if (authStrategy === undefined) {
        return {
            ok: false,
            code: "INVALID_AUTH_STRATEGY",
            message: "mcp.authenticate requires target.authStrategy to be oauth, api-key, bearer-token, or custom.",
            boundary: "input",
            context,
        };
    }
    const credentialRef = optionalTrimmedString(root.target.credentialRef);
    if (credentialRef === undefined) {
        return {
            ok: false,
            code: "MISSING_CREDENTIAL_REF",
            message: "mcp.authenticate requires target.credentialRef instead of raw credential material.",
            boundary: "input",
            context,
        };
    }
    const requestedScopes = cleanStringList(root.target.requestedScopes) ?? [];
    if (root.target.requestedScopes !== undefined && !Array.isArray(root.target.requestedScopes)) {
        return {
            ok: false,
            code: "INVALID_REQUESTED_SCOPES",
            message: "mcp.authenticate target.requestedScopes must be an array of strings.",
            boundary: "input",
            context,
        };
    }
    if (context.allowedServerIds !== undefined && !context.allowedServerIds.includes(serverId)) {
        return { ok: false, code: "SCOPE_REJECTED", message: "mcp.authenticate target server is outside allowed MCP server ids.", boundary: "scope", context };
    }
    if (context.allowedScopes !== undefined && requestedScopes.some((scope) => !context.allowedScopes?.includes(scope))) {
        return { ok: false, code: "SCOPE_REJECTED", message: "mcp.authenticate requested scopes are outside allowed MCP scopes.", boundary: "scope", context };
    }
    const missingPermissions = mcpAuthenticateDescriptor.permissionsRequired.filter((permission) => !context.grantedPermissions?.includes(permission));
    if (context.grantedPermissions !== undefined && missingPermissions.length > 0) {
        return {
            ok: false,
            code: "PERMISSION_DENIED",
            message: `mcp.authenticate is missing permissions: ${missingPermissions.join(", ")}`,
            boundary: "permission",
            context,
        };
    }
    return { ok: true, target: { serverId, authStrategy, credentialRef, requestedScopes }, context };
}
function auditEvent(type, dryRun, context) {
    return {
        type,
        toolId: "mcp.authenticate",
        invocationId: context.invocationId ?? "mcp.authenticate:dry-run",
        dryRun,
        metadata: {
            runtimeId: context.runtimeId,
            sessionId: context.sessionId,
            ...(context.auditMetadata ?? {}),
        },
    };
}
function failure(error, context = {}, event = "basicTool.mcp.authenticate.rejected") {
    return {
        ok: false,
        toolId: "mcp.authenticate",
        error: {
            code: error.code,
            message: error.message,
            boundary: error.boundary,
            publicSafe: true,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        audit: [auditEvent("mcp.authenticate.rejected", context.dryRun !== false, context)],
        events: [event],
    };
}
function output(target, options) {
    return {
        kind: "agentCore.basicTool.mcp.authenticate",
        target,
        dryRun: options.dryRun,
        executionBlocked: options.dryRun,
        providerCalled: options.providerCalled,
        permissionsRequired: mcpAuthenticateDescriptor.permissionsRequired,
        unsafeSideEffects: true,
        credentialMaterialAccepted: false,
        tokenIssued: options.result?.status === "authenticated" || options.result?.status === "already_authenticated",
        authEnvelope: {
            serverId: options.result?.serverId ?? target.serverId,
            authStrategy: target.authStrategy,
            credentialRef: target.credentialRef,
            requestedScopes: target.requestedScopes,
            state: options.result?.status ?? "planned",
            authSessionId: options.result?.authSessionId,
            expiresAt: options.result?.expiresAt,
            scopesGranted: options.result?.scopesGranted,
            source: options.result === undefined ? "dry-run" : "runtime-provider",
        },
        providerMetadata: options.result?.providerMetadata,
    };
}
export function planMcpAuthenticate(request = {}) {
    const normalized = validate(request);
    if (!normalized.ok)
        return failure(normalized, normalized.context);
    if (normalized.context.dryRun === false) {
        return failure({ ok: false, code: "REAL_EXECUTION_BLOCKED", message: "planMcpAuthenticate only produces dry-run envelopes.", boundary: "contract", context: normalized.context }, normalized.context);
    }
    return {
        ok: true,
        toolId: "mcp.authenticate",
        output: output(normalized.target, { dryRun: true, providerCalled: false }),
        audit: [auditEvent("mcp.authenticate.planned", true, normalized.context)],
        events: ["basicTool.mcp.authenticate.dryRun"],
    };
}
export async function executeMcpAuthenticate(request = {}, provider) {
    const normalized = validate(request);
    if (!normalized.ok)
        return failure(normalized, normalized.context);
    if (normalized.context.dryRun !== false) {
        return {
            ok: true,
            toolId: "mcp.authenticate",
            output: output(normalized.target, { dryRun: true, providerCalled: false }),
            audit: [auditEvent("mcp.authenticate.planned", true, normalized.context)],
            events: ["basicTool.mcp.authenticate.dryRun"],
        };
    }
    if (!guardAccepted(normalized.context.guard)) {
        return failure({ ok: false, code: "GOVERNANCE_REJECTED", message: "mcp.authenticate requires an accepted runtime guard before real dispatch.", boundary: "governance", context: normalized.context }, normalized.context);
    }
    if (provider === undefined) {
        return failure({ ok: false, code: "PROVIDER_UNAVAILABLE", message: "Runtime MCP authentication provider is unavailable.", boundary: "provider", context: normalized.context }, normalized.context, "basicTool.mcp.authenticate.providerUnavailable");
    }
    try {
        const result = await provider(normalized.target, {
            runtimeId: normalized.context.runtimeId,
            sessionId: normalized.context.sessionId,
            invocationId: normalized.context.invocationId,
            auditMetadata: normalized.context.auditMetadata,
        });
        return {
            ok: true,
            toolId: "mcp.authenticate",
            output: output(normalized.target, { dryRun: false, providerCalled: true, result }),
            audit: [auditEvent("mcp.authenticate.executed", false, normalized.context)],
            events: ["basicTool.mcp.authenticate.executed"],
        };
    }
    catch {
        return failure({ ok: false, code: "PROVIDER_REJECTED", message: "Runtime MCP provider failed authentication.", boundary: "provider", context: normalized.context }, normalized.context, "basicTool.mcp.authenticate.providerRejected");
    }
}
