"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TERMINAL_SSE_EVENT_TYPES = void 0;
exports.parseLastEventId = parseLastEventId;
exports.appendReplayableSseEvent = appendReplayableSseEvent;
exports.hasTerminalReplayAfter = hasTerminalReplayAfter;
const streamProjector_1 = require("./streamProjector");
exports.TERMINAL_SSE_EVENT_TYPES = new Set([
    'analysis_completed',
    'error',
    'end',
]);
function parseLastEventId(headerValue, legacyQueryValue) {
    const raw = headerValue ?? legacyQueryValue;
    if (Array.isArray(raw)) {
        return parseLastEventId(raw[0]);
    }
    if (raw === undefined || raw === null || raw === '') {
        return null;
    }
    const parsed = Number.parseInt(String(raw), 10);
    return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
}
function appendReplayableSseEvent(state, eventType, payload, maxBufferedEvents = streamProjector_1.SSE_RING_BUFFER_SIZE) {
    const event = {
        seqId: ++state.sseEventSeq,
        eventType,
        eventData: JSON.stringify(payload),
    };
    state.sseEventBuffer.push(event);
    if (state.sseEventBuffer.length > maxBufferedEvents) {
        state.sseEventBuffer.splice(0, state.sseEventBuffer.length - maxBufferedEvents);
    }
    return event;
}
function hasTerminalReplayAfter(state, lastEventId) {
    return state.sseEventBuffer.some(event => event.seqId > lastEventId && exports.TERMINAL_SSE_EVENT_TYPES.has(event.eventType));
}
//# sourceMappingURL=sessionSseReplay.js.map