"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    normalizeSpans: ()=>normalizeSpans,
    calculateDurationMs: ()=>calculateDurationMs,
    findAllAgentRunSpans: ()=>findAllAgentRunSpans,
    filterChildSpans: ()=>filterChildSpans,
    shouldRenderTimelinePlayer: ()=>shouldRenderTimelinePlayer,
    findAgentRunSpan: ()=>findAgentRunSpan,
    calculateTimelineHeight: ()=>calculateTimelineHeight,
    calculateSpanDepth: ()=>calculateSpanDepth
});
const external_luxon_namespaceObject = require("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 = external_luxon_namespaceObject.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 external_luxon_namespaceObject.DateTime.fromISO(end).toMillis() - external_luxon_namespaceObject.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;
};
exports.calculateDurationMs = __webpack_exports__.calculateDurationMs;
exports.calculateSpanDepth = __webpack_exports__.calculateSpanDepth;
exports.calculateTimelineHeight = __webpack_exports__.calculateTimelineHeight;
exports.filterChildSpans = __webpack_exports__.filterChildSpans;
exports.findAgentRunSpan = __webpack_exports__.findAgentRunSpan;
exports.findAllAgentRunSpans = __webpack_exports__.findAllAgentRunSpans;
exports.normalizeSpans = __webpack_exports__.normalizeSpans;
exports.shouldRenderTimelinePlayer = __webpack_exports__.shouldRenderTimelinePlayer;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "calculateDurationMs",
    "calculateSpanDepth",
    "calculateTimelineHeight",
    "filterChildSpans",
    "findAgentRunSpan",
    "findAllAgentRunSpans",
    "normalizeSpans",
    "shouldRenderTimelinePlayer"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
