import { DateTime } from "luxon";
const DEFAULT_SPAN_DURATION_MS = 1000;
const MINIMUM_VISIBLE_DURATION = 0.01;
const TIMELINE_PLAYER_HEIGHT = 96;
const shouldRenderTimelinePlayer = (enableTimelinePlayer, durationMs, normalizedSpans)=>{
    if (!enableTimelinePlayer || 0 === durationMs || 0 === normalizedSpans.length) return false;
    return true;
};
function calculateSpanDepth(spans) {
    const depthMap = new Map();
    const parentMap = new Map();
    for (const span of spans)if (span.ParentId) parentMap.set(span.Id, span.ParentId);
    const getDepth = (spanId)=>{
        if (depthMap.has(spanId)) return depthMap.get(spanId) || 0;
        const parentId = parentMap.get(spanId);
        if (!parentId) {
            depthMap.set(spanId, 0);
            return 0;
        }
        const depth = getDepth(parentId) + 1;
        depthMap.set(spanId, depth);
        return depth;
    };
    for (const span of spans)getDepth(span.Id);
    return depthMap;
}
function normalizeSpans(spans) {
    const depthMap = calculateSpanDepth(spans);
    const getTime = (s)=>{
        if (!s) return null;
        const luxonDate = DateTime.fromISO(s);
        if (luxonDate.isValid) return luxonDate.toMillis();
        return null;
    };
    const times = spans.map((s)=>{
        const start = getTime(s.StartTime);
        let end = getTime(s.EndTime);
        if (!end && start) end = s.UpdatedAt ? getTime(s.UpdatedAt) : Date.now();
        return {
            start: start || 0,
            end: end || (start || 0) + DEFAULT_SPAN_DURATION_MS
        };
    });
    const validTimes = times.filter((t)=>t.start > 0 && t.end > 0);
    if (0 === validTimes.length) return spans.map((span)=>({
            ...span,
            normalizedStart: 0,
            normalizedDuration: 0,
            depth: depthMap.get(span.Id) || 0
        }));
    const minStart = Math.min(...validTimes.map((t)=>t.start));
    const maxEnd = Math.max(...validTimes.map((t)=>t.end));
    const duration = maxEnd - minStart;
    if (0 === duration) return spans.map((span)=>({
            ...span,
            normalizedStart: 0,
            normalizedDuration: 1,
            depth: depthMap.get(span.Id) || 0
        }));
    return spans.map((span, index)=>{
        const time = times[index];
        const start = time.start;
        const end = time.end;
        const spanDuration = end - start;
        return {
            ...span,
            normalizedStart: (start - minStart) / duration,
            normalizedDuration: Math.max(spanDuration / duration, MINIMUM_VISIBLE_DURATION),
            depth: depthMap.get(span.Id) || 0
        };
    });
}
function findAgentRunSpan(spans) {
    return spans.find((span)=>'agentRun' === span.SpanType);
}
function findAllAgentRunSpans(spans) {
    return spans.filter((span)=>'agentRun' === span.SpanType);
}
function filterChildSpans(spans, agentSpans) {
    const agentIds = agentSpans.map((s)=>s.Id);
    return spans.filter((span)=>agentIds.includes(span.ParentId ?? ''));
}
function calculateDurationMs(parentSpan) {
    const start = parentSpan?.StartTime ?? 0;
    const end = parentSpan?.EndTime ?? parentSpan?.UpdatedAt ?? 0;
    if (start && end) return DateTime.fromISO(end).toMillis() - DateTime.fromISO(start).toMillis();
    return 0;
}
const calculateTimelineHeight = (enableTimelinePlayer, spans)=>{
    const parentSpan = findAgentRunSpan(spans);
    const agentSpans = findAllAgentRunSpans(spans);
    const filteredSpans = filterChildSpans(spans, agentSpans);
    const normalizedSpans = normalizeSpans(filteredSpans);
    const durationMs = calculateDurationMs(parentSpan);
    if (!shouldRenderTimelinePlayer(enableTimelinePlayer, durationMs, normalizedSpans)) return 0;
    return TIMELINE_PLAYER_HEIGHT;
};
export { calculateDurationMs, calculateSpanDepth, calculateTimelineHeight, filterChildSpans, findAgentRunSpan, findAllAgentRunSpans, normalizeSpans, shouldRenderTimelinePlayer };
