"use client";
import { useMemo } from "react";
const HEALTHY_CEILING = 70;
const WARNING_CEILING = 90;
const NEAR_THRESHOLD_MARGIN = 5;
const EMPTY = {
    hasContextInfo: false,
    currentTokenCount: 0,
    contextWindowLimit: 0,
    utilizationPercent: 0,
    triggerThreshold: 0,
    targetTokens: 0,
    summarizationEnabled: false,
    summarizationEvents: [],
    health: "healthy",
    isNearThreshold: false,
};
function deriveHealth(utilization) {
    if (utilization >= WARNING_CEILING)
        return "critical";
    if (utilization >= HEALTHY_CEILING)
        return "warning";
    return "healthy";
}
function mapEvent(e) {
    return {
        timestamp: e.timestamp,
        tokensBefore: e.tokensBefore,
        tokensAfter: e.tokensAfter,
        compressionRatio: e.compressionRatio,
        durationMs: e.durationMs,
        model: e.summarizationModel,
        messagesBefore: e.messagesBefore,
        messagesAfter: e.messagesAfter,
        costUsd: e.summarizationCostUsd,
    };
}
/**
 * Extracts context window information from an execution's status.
 *
 * Provides real-time visibility into context utilization, summarization
 * thresholds, and summarization history for the native harness. Returns
 * a stable empty state when `ContextInfo` is absent (e.g., Cursor harness
 * where context is managed externally).
 *
 * The derived `health` and `isNearThreshold` fields are intended for
 * driving visual indicators (gauge color, alert badges) without coupling
 * rendering components to threshold arithmetic.
 *
 * @param execution - The execution snapshot from `useExecutionStream`, or `null`.
 *
 * @example
 * ```tsx
 * const { execution } = useExecutionStream(executionId);
 * const ctx = useContextWindow(execution);
 *
 * if (ctx.hasContextInfo) {
 *   return <ContextGauge {...ctx} />;
 * }
 * ```
 */
export function useContextWindow(execution) {
    const contextInfo = execution?.status?.contextInfo ?? null;
    const events = useMemo(() => {
        if (!contextInfo?.summarizationEvents?.length)
            return [];
        return contextInfo.summarizationEvents.map(mapEvent);
    }, [contextInfo?.summarizationEvents]);
    return useMemo(() => {
        if (!contextInfo || contextInfo.contextWindowLimit === 0)
            return EMPTY;
        const utilization = contextInfo.utilizationPercent;
        const trigger = contextInfo.summarizationTriggerThreshold;
        const triggerPercent = trigger > 0 && contextInfo.contextWindowLimit > 0
            ? (trigger / contextInfo.contextWindowLimit) * 100
            : WARNING_CEILING;
        return {
            hasContextInfo: true,
            currentTokenCount: contextInfo.currentTokenCount,
            contextWindowLimit: contextInfo.contextWindowLimit,
            utilizationPercent: utilization,
            triggerThreshold: trigger,
            targetTokens: contextInfo.summarizationTargetTokens,
            summarizationEnabled: contextInfo.summarizationEnabled,
            summarizationEvents: events,
            health: deriveHealth(utilization),
            isNearThreshold: utilization >= triggerPercent - NEAR_THRESHOLD_MARGIN,
        };
    }, [contextInfo, events]);
}
//# sourceMappingURL=useContextWindow.js.map