import {} from "@steipete/summarize-core/runtime";
import { closeBufferedSseChannel, createBufferedSseChannel, pushBufferedSseEvent, } from "./server-event-channel.js";
const SESSION_TTL_MS = 15 * 60 * 1000;
export function createSession(idFactory) {
    return {
        id: idFactory(),
        createdAtMs: Date.now(),
        summaryEvents: createBufferedSseChannel(),
        slideEvents: createBufferedSseChannel(),
        slidesRequested: false,
        slidesLastStatus: null,
        lastMeta: {
            model: null,
            modelLabel: null,
            inputSummary: null,
            summaryFromCache: null,
        },
        transcriptTimedText: null,
        slides: null,
    };
}
export function pushToSession(session, event, onSessionEvent) {
    if (session.summaryEvents.done)
        return;
    pushBufferedSseEvent(session.summaryEvents, event);
    onSessionEvent?.(event, session.id);
    if (event.event === "done" || event.event === "error") {
        session.summaryEvents.done = true;
        closeBufferedSseChannel(session.summaryEvents);
    }
}
export function pushSlidesToSession(session, event, onSessionEvent) {
    pushBufferedSseEvent(session.slideEvents, event);
    onSessionEvent?.(event, session.id);
    if (event.event === "done") {
        session.slideEvents.done = true;
        closeBufferedSseChannel(session.slideEvents);
    }
    if (event.event === "status")
        session.slidesLastStatus = event.data.text;
}
export function emitMeta(session, data, onSessionEvent) {
    session.lastMeta = {
        model: typeof data.model === "string" ? data.model : session.lastMeta.model,
        modelLabel: typeof data.modelLabel === "string" ? data.modelLabel : session.lastMeta.modelLabel,
        inputSummary: typeof data.inputSummary === "string" ? data.inputSummary : session.lastMeta.inputSummary,
        summaryFromCache: typeof data.summaryFromCache === "boolean"
            ? data.summaryFromCache
            : session.lastMeta.summaryFromCache,
    };
    pushToSession(session, { event: "meta", data: session.lastMeta }, onSessionEvent);
}
export function emitSlides(session, data, onSessionEvent) {
    pushToSession(session, { event: "slides", data }, onSessionEvent);
    pushSlidesToSession(session, { event: "slides", data }, onSessionEvent);
}
export function emitSlidesStatus(session, text, onSessionEvent) {
    const trimmed = text.trim();
    if (!trimmed)
        return;
    pushSlidesToSession(session, { event: "status", data: { text: trimmed } }, onSessionEvent);
}
export function emitSlidesDone(session, result, onSessionEvent) {
    if (!result.ok) {
        const message = result.error?.trim() || "Slides failed.";
        pushSlidesToSession(session, { event: "error", data: { message } }, onSessionEvent);
    }
    pushSlidesToSession(session, { event: "done", data: {} }, onSessionEvent);
}
export function endSession(session) {
    closeBufferedSseChannel(session.summaryEvents);
    closeBufferedSseChannel(session.slideEvents);
}
export function scheduleSessionCleanup({ sessions, refreshSessions, session, }) {
    setTimeout(() => {
        sessions.delete(session.id);
        refreshSessions.delete(session.id);
        endSession(session);
    }, SESSION_TTL_MS).unref();
}
//# sourceMappingURL=server-session.js.map