/*
 * 文件定位：Agent 运行态实现层 / 应用承托面。
 * 核心目的：返回给上层应用的稳定 runtime 句柄。
 * 能力要求1：句柄需要能发起调用、订阅事件、查询状态、关闭实例，但不暴露内部可变对象。
 * 能力要求2：它是开发者真正拿在手里使用 agentCore 的对象边界。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
const defaultHandleOperations = ["invoke", "subscribe", "inspect", "close"];
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 cleanOperations(values) {
    return [...new Set(values ?? defaultHandleOperations)];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
        events: ["runtime.handle.rejected"],
    };
}
function callFailure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
    };
}
export function createAgentRuntimeHandle(request) {
    if (isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtimeId is required before creating a runtime handle", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "runtime handle can only wrap a ready runtime", "runtime-state");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime handle creation was rejected by governance", "governance");
    }
    const runtimeId = request.runtimeId.trim();
    const applicationId = request.applicationId?.trim() || undefined;
    const enabledOperations = cleanOperations(request.enabledOperations);
    const visibleSessions = cleanList(request.visibleSessions);
    const visibleEventTypes = cleanList(request.visibleEventTypes);
    let closed = false;
    const handle = {
        runtimeId,
        applicationId,
        enabledOperations,
        visibleSessions,
        visibleEventTypes,
        call(call) {
            if (closed) {
                return callFailure("HANDLE_CLOSED", "runtime handle is already closed", "runtime-state");
            }
            if (!enabledOperations.includes(call.operation)) {
                return callFailure("UNSUPPORTED_OPERATION", `operation ${call.operation} is not enabled for this runtime handle`, "input");
            }
            return {
                ok: true,
                operation: call.operation,
                runtimeId,
                applicationId,
                accepted: true,
                dryRun: true,
            };
        },
        getStatus() {
            return {
                runtimeId,
                applicationId,
                status: closed ? "closed" : "ready",
                enabledOperations,
                visibleSessions,
                visibleEventTypes,
            };
        },
        close() {
            if (closed) {
                return {
                    ok: false,
                    error: {
                        code: "HANDLE_CLOSED",
                        message: "runtime handle is already closed",
                        boundary: "runtime-state",
                    },
                    events: ["runtime.handle.close.rejected"],
                };
            }
            closed = true;
            return {
                ok: true,
                runtimeId,
                status: "closed",
                events: ["runtime.handle.closed"],
            };
        },
    };
    return {
        ok: true,
        handle,
        events: ["runtime.handle.ready"],
    };
}
