/*
 * 文件定位：Agent 运行态实现层 / 应用承托面。
 * 核心目的：给上层应用提供受控 runtime 上下文。
 * 能力要求1：需要包含当前能力、会话、模式、事件订阅、治理状态等应用需要的信息。
 * 能力要求2：不能泄露执行引擎、模型适配或官方模块的内部可变状态。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function visibleCapabilities(capabilities) {
    return (capabilities ?? [])
        .filter((capability) => !isBlank(capability.capabilityId))
        .filter((capability) => capability.visibility !== "internal")
        .map((capability) => {
        const visible = {
            capabilityId: capability.capabilityId.trim(),
        };
        if (capability.kind !== undefined) {
            visible.kind = capability.kind;
        }
        return visible;
    });
}
function visibleSessions(sessions) {
    return (sessions ?? [])
        .filter((session) => !isBlank(session.sessionId))
        .map((session) => ({
        sessionId: session.sessionId.trim(),
        agentId: session.agentId?.trim() || undefined,
        status: session.status,
    }));
}
function visibleModes(modes) {
    return (modes ?? [])
        .filter((mode) => !isBlank(mode.modeId))
        .map((mode) => ({
        modeId: mode.modeId.trim(),
        label: mode.label?.trim() || undefined,
        active: mode.active,
    }));
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
        events: ["application.runtime.context.rejected"],
    };
}
export function createApplicationRuntimeContext(request) {
    if (isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtimeId is required before exposing application context", "input");
    }
    if (isBlank(request.applicationId)) {
        return failure("MISSING_APPLICATION_ID", "applicationId is required before exposing application context", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "application context can only expose a ready runtime", "runtime-state");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "application runtime context was rejected by governance", "governance");
    }
    const modes = visibleModes(request.modes);
    const activeMode = modes.find((mode) => mode.active);
    return {
        ok: true,
        context: {
            runtimeId: request.runtimeId.trim(),
            applicationId: request.applicationId.trim(),
            sessionId: request.sessionId?.trim() || undefined,
            capabilities: visibleCapabilities(request.capabilities),
            sessions: visibleSessions(request.sessions),
            modes,
            activeModeId: activeMode?.modeId,
            eventSubscriptions: cleanList(request.eventSubscriptions),
            governanceState: "accepted",
            internalStateExposed: false,
        },
        events: ["application.runtime.context.ready"],
    };
}
