/**
 * @license
 * Copyright 2025 Qwen Code
 * SPDX-License-Identifier: Apache-2.0
 */
const STICKY_TODO_STATUS_PRIORITY = {
    in_progress: 0,
    pending: 1,
    completed: 2,
};
function extractTodosFromResultDisplay(resultDisplay) {
    if (!resultDisplay) {
        return null;
    }
    if (typeof resultDisplay === 'object') {
        const candidate = resultDisplay;
        if (candidate['type'] === 'todo_list' &&
            Array.isArray(candidate['todos'])) {
            return candidate['todos'];
        }
    }
    if (typeof resultDisplay === 'string') {
        try {
            const parsed = JSON.parse(resultDisplay);
            if (parsed['type'] === 'todo_list' && Array.isArray(parsed['todos'])) {
                return parsed['todos'];
            }
        }
        catch {
            return null;
        }
    }
    return null;
}
function findLatestTodoSnapshot(items) {
    for (let itemIndex = items.length - 1; itemIndex >= 0; itemIndex -= 1) {
        const item = items[itemIndex];
        if (item.type !== 'tool_group') {
            continue;
        }
        for (let toolIndex = item.tools.length - 1; toolIndex >= 0; toolIndex -= 1) {
            const tool = item.tools[toolIndex];
            const todos = extractTodosFromResultDisplay(tool.resultDisplay);
            if (todos) {
                return todos.length > 0 ? todos : null;
            }
        }
    }
    return undefined;
}
function areAllTodosCompleted(todos) {
    return todos.length > 0 && todos.every((todo) => todo.status === 'completed');
}
export function getStickyTodos(history, pendingHistoryItems) {
    const pendingSnapshot = findLatestTodoSnapshot(pendingHistoryItems);
    if (pendingSnapshot !== undefined) {
        return pendingSnapshot;
    }
    const historySnapshot = findLatestTodoSnapshot(history);
    if (historySnapshot && areAllTodosCompleted(historySnapshot)) {
        return null;
    }
    return historySnapshot ?? null;
}
export function getOrderedStickyTodos(todos) {
    return todos
        .map((todo, index) => ({ todo, index }))
        .sort((left, right) => STICKY_TODO_STATUS_PRIORITY[left.todo.status] -
        STICKY_TODO_STATUS_PRIORITY[right.todo.status] ||
        left.index - right.index)
        .map(({ todo }) => todo);
}
//# sourceMappingURL=todoSnapshot.js.map