import { normalizeAgentId } from "../agentMessageHelpers";
import { mergeMessagesById } from "../agentMessageGrouping";
import { parseContextBreakdown } from "./agentSessionLiveTotals";
function isRunningAgentStatus(status) {
    return status === "running" || status === 1;
}
function isWaitingAgentStatus(status) {
    return (status === "waitingForApproval" ||
        status === 2 ||
        status === "waitingForInput" ||
        status === "costLimitReached" ||
        status === 7);
}
function isWaitingForApprovalStatus(status) {
    return status === "waitingForApproval" || status === 2;
}
function keepLocalStreamingMessage(localMessage, dbMessages) {
    if (!localMessage.id)
        return false;
    if (localMessage.isCompleted || localMessage.messageType !== "streaming") {
        return false;
    }
    return !dbMessages.some((dbMessage) => dbMessage.id &&
        dbMessage.id.toLowerCase() === localMessage.id.toLowerCase());
}
export function mergeLoadedAgentTranscript(input) {
    const { agentData, localMessages, markSeenMessageId } = input;
    const dbMessages = agentData.messages || [];
    for (const message of dbMessages) {
        if (message.id) {
            markSeenMessageId?.(message.id);
        }
    }
    const isRunning = isRunningAgentStatus(agentData.status);
    const isWaiting = isWaitingAgentStatus(agentData.status);
    const loadedAgentId = normalizeAgentId(agentData.id);
    const agentScopedLocalMessages = localMessages.filter((message) => normalizeAgentId(message.agentId) === loadedAgentId);
    const localStreaming = isRunning || isWaiting
        ? agentScopedLocalMessages.filter((message) => keepLocalStreamingMessage(message, dbMessages))
        : [];
    const merged = mergeMessagesById(dbMessages, localStreaming);
    return {
        merged,
        isRunning,
        isWaiting,
        waitingForApproval: isWaitingForApprovalStatus(agentData.status),
        waitingForInput: agentData.status === "waitingForInput",
        hasStreaming: merged.some((message) => !message.isCompleted && message.messageType === "streaming"),
    };
}
export function buildLoadedContextWindowStatus(agentData) {
    if (agentData.contextUsedTokens === undefined) {
        return undefined;
    }
    return {
        contextWindowTokens: agentData.contextWindowTokens ?? 0,
        estimatedInputTokens: agentData.contextUsedTokens,
        contextUsedPercent: agentData.contextWindowTokens
            ? (agentData.contextUsedTokens / agentData.contextWindowTokens) * 100
            : 0,
        model: agentData.model,
        breakdown: parseContextBreakdown(agentData.contextBreakdown),
    };
}
export function parseLoadedAgentMetadata(agentData) {
    try {
        const contextJson = agentData.agentContext;
        if (!contextJson)
            return null;
        const parsedContext = JSON.parse(contextJson);
        if (parsedContext && typeof parsedContext === "object") {
            return parsedContext;
        }
        return null;
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=agentSessionSnapshot.js.map