"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findNewlySettledTools = findNewlySettledTools;
exports.useToolResultContinuation = useToolResultContinuation;
const react_1 = require("react");
const PENDING_STATUSES = new Set([
    'waitingConfirmation',
    'waitingSubmission',
    'loading',
]);
const TERMINAL_STATUSES = new Set([
    'success',
    'error',
    'cancelled',
]);
function isPending(status) {
    return status !== undefined && PENDING_STATUSES.has(status);
}
function isTerminal(status) {
    return status !== undefined && TERMINAL_STATUSES.has(status);
}
function isToolPartContent(part) {
    return (part.type === 'tool' &&
        typeof part.data === 'object' &&
        part.data !== null &&
        'toolCallId' in part.data);
}
function toContentParts(content) {
    if (typeof content === 'string')
        return [];
    return Array.isArray(content) ? content : [content];
}
function collectToolPartsByCallId(messages) {
    const map = new Map();
    for (const msg of messages) {
        if (msg.role !== 'assistant')
            continue;
        for (const part of toContentParts(msg.content)) {
            if (!part || typeof part !== 'object')
                continue;
            if (!isToolPartContent(part))
                continue;
            map.set(part.data.toolCallId, part.data);
        }
    }
    return map;
}
/**
 * Pure: returns events for every tool whose status was pending in `prev`
 * and is terminal in `next`. Tools that are already terminal in `prev`
 * (replay/restore) do not produce events.
 */
function findNewlySettledTools(prev, next) {
    var _a;
    const prevParts = collectToolPartsByCallId(prev);
    const events = [];
    for (const [toolCallId, data] of collectToolPartsByCallId(next)) {
        const prevStatus = (_a = prevParts.get(toolCallId)) === null || _a === void 0 ? void 0 : _a.status;
        if (isPending(prevStatus) && isTerminal(data.status)) {
            events.push({
                toolCallId,
                toolName: data.toolName,
                status: data.status,
                messages: next,
            });
        }
    }
    return events;
}
/**
 * Observe `messages` and fire `onSettled` once whenever a tool part
 * transitions from pending to terminal. Tools already terminal on the
 * first observed snapshot do not fire — replay/restore is intentionally
 * silent. Pair with `useToolset` to forward results to the model.
 */
function useToolResultContinuation(options) {
    const { messages, onSettled } = options;
    const onSettledRef = (0, react_1.useRef)(onSettled);
    (0, react_1.useEffect)(() => {
        onSettledRef.current = onSettled;
    });
    const prevMessagesRef = (0, react_1.useRef)(messages);
    (0, react_1.useEffect)(() => {
        const events = findNewlySettledTools(prevMessagesRef.current, messages);
        prevMessagesRef.current = messages;
        for (const event of events) {
            onSettledRef.current(event);
        }
    }, [messages]);
}
//# sourceMappingURL=useToolResultContinuation.js.map
