import { randomUUID } from "node:crypto";
import { projectOpenCodeRuntimeTimelineEvent } from "./runtimeTimelineContract.js";
export function normalizeOpenCodeEvent(event, context = {}) {
    const projected = projectOpenCodeRuntimeTimelineEvent(event.type);
    const runtimeSessionId = readSessionId(event);
    return {
        id: `rte_${randomUUID()}`,
        source: "opencode",
        type: projected.type,
        at: new Date().toISOString(),
        title: projected.title,
        description: projected.description,
        tone: projected.tone,
        metadata: projected.metadata,
        ...(context.runId ? { runId: context.runId } : {}),
        ...(runtimeSessionId ? { runtimeSessionId } : {}),
    };
}
function readSessionId(event) {
    const candidates = [event.sessionID, event.sessionId];
    for (const candidate of candidates) {
        if (typeof candidate === "string")
            return candidate;
    }
    if (typeof event.session === "object" &&
        event.session !== null &&
        "id" in event.session) {
        const id = event.session.id;
        if (typeof id === "string")
            return id;
    }
    return undefined;
}
