/*
 * 文件定位：Agent 运行态实现层 / 行为暴露运行面。
 * 核心目的：承载 behavior Event Publisher 这一能力位点。
 * 能力要求1：需要把文件名表达的能力落实成清晰的类型、输入输出和最小行为。
 * 能力要求2：如果后续发现语义不足，应优先补接口契约，而不是把逻辑散落到相邻文件。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const behaviorEventPublisherDescriptor = {
    surface: "runtime.behaviorExposure",
    capability: "behaviorEventPublisher",
    purpose: "publish a governed behavior event envelope without notifying subscribers in the first implementation",
    mode: "dry-run",
    unsafeSideEffects: false,
};
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function isRecord(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function cleanBehaviorExposureList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
export function behaviorExposureFailure(code, message, boundary) {
    return {
        ok: false,
        error: {
            code,
            message,
            boundary,
            safeForRuntimeInspection: true,
            internalDetailExposed: false,
        },
        events: ["runtime.behaviorExposure.behaviorEvent.rejected"],
    };
}
function resolveSubscribers(requestedSubscribers, allowedSubscribers) {
    const requested = cleanBehaviorExposureList(requestedSubscribers);
    const allowed = cleanBehaviorExposureList(allowedSubscribers);
    if (requested.length === 0) {
        return [];
    }
    const denied = requested.filter((subscriber) => !allowed.includes(subscriber));
    if (denied.length > 0) {
        return behaviorExposureFailure("SUBSCRIBER_SCOPE_DENIED", `behavior event subscriber ${denied[0]} is outside runtime behavior exposure scope`, "scope");
    }
    return requested;
}
function cleanTrace(trace, sessionId) {
    return {
        correlationId: trace?.correlationId?.trim() || undefined,
        parentEventId: trace?.parentEventId?.trim() || undefined,
        sessionId: trace?.sessionId?.trim() || sessionId,
    };
}
export function publishBehaviorEvent(request) {
    if (request === undefined || isBlank(request.runtimeId)) {
        return behaviorExposureFailure("MISSING_RUNTIME_ID", "behavior event publisher requires runtimeId", "input");
    }
    if (isBlank(request.sessionId)) {
        return behaviorExposureFailure("MISSING_SESSION_ID", "behavior event publisher requires sessionId", "input");
    }
    if (isBlank(request.eventKind)) {
        return behaviorExposureFailure("MISSING_EVENT_KIND", "behavior event publisher requires eventKind", "input");
    }
    if (request.source === undefined) {
        return behaviorExposureFailure("MISSING_EVENT_SOURCE", "behavior event publisher requires an event source", "input");
    }
    if (request.caller === undefined) {
        return behaviorExposureFailure("MISSING_CALLER", "behavior event publisher requires a runtime caller", "input");
    }
    if (request.payload !== undefined && !isRecord(request.payload)) {
        return behaviorExposureFailure("INVALID_EVENT_PAYLOAD", "behavior event payload must be a plain record", "input");
    }
    if (request.runtimeReady === false) {
        return behaviorExposureFailure("RUNTIME_NOT_READY", "behavior events require a ready runtime", "runtime-state");
    }
    if (request.contract?.accepted === false) {
        return behaviorExposureFailure("CONTRACT_REJECTED", request.contract.reason ?? "runtime contract surface rejected the behavior event", "contract");
    }
    if (request.governance?.accepted === false) {
        return behaviorExposureFailure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime governance rejected the behavior event", "governance");
    }
    if (request.deliver === true) {
        return behaviorExposureFailure("REAL_DELIVERY_BLOCKED", "behaviorEventPublisher only creates a dry-run publication envelope in the first implementation", "governance");
    }
    const deliverableSubscribers = resolveSubscribers(request.requestedSubscribers, request.allowedSubscribers);
    if ("ok" in deliverableSubscribers) {
        return deliverableSubscribers;
    }
    const runtimeId = request.runtimeId?.trim() ?? "";
    const sessionId = request.sessionId?.trim() ?? "";
    const eventKind = request.eventKind?.trim() ?? "";
    const observedAt = request.observedAt?.trim() || "dry-run";
    const payload = request.payload ?? {};
    return {
        ok: true,
        event: {
            surface: "runtime.behaviorExposure",
            type: "behavior.event.published",
            runtimeId,
            sessionId,
            eventId: request.eventId?.trim() || `${runtimeId}:${sessionId}:behavior:${eventKind}`,
            eventKind,
            source: request.source,
            caller: request.caller,
            payload,
            payloadKeys: Object.keys(payload).sort(),
            trace: cleanTrace(request.trace, sessionId),
            observedAt,
            route: "agent_runtimeImplementation.runtime.behaviorExposure.behaviorEventPublisher",
            audit: {
                dryRun: true,
                delivered: false,
                unsafeSideEffects: false,
                contractSurface: "runtime.contractSurface",
                governanceRequired: true,
            },
            metadata: request.metadata ?? {},
        },
        publication: {
            mode: "dry-run",
            requestedSubscribers: cleanBehaviorExposureList(request.requestedSubscribers),
            deliverableSubscribers,
            actualSubscriberNotificationStarted: false,
        },
        events: ["runtime.behaviorExposure.behaviorEvent.published"],
    };
}
