function findContentPartIndex(parts, partType, id) {
    return parts.findIndex((p) => p.type === partType && p.id === id);
}
export function applyContentUpdate(parts, update) {
    switch (update.kind) {
        case 'text_delta':
            return applyTextDelta(parts, update);
        case 'tool_add':
            return applyToolAdd(parts, update);
        case 'tool_update':
            return applyToolUpdate(parts, update);
        case 'thinking_add':
            return applyThinkingAdd(parts, update);
        case 'thinking_delta':
            return applyThinkingDelta(parts, update);
        case 'thinking_done':
            return applyThinkingDone(parts, update);
        default:
            return null;
    }
}
function applyTextDelta(parts, update) {
    const last = parts[parts.length - 1];
    if ((last === null || last === void 0 ? void 0 : last.type) === 'text') {
        return [
            ...parts.slice(0, -1),
            { type: 'text', data: { text: last.data.text + update.delta } },
        ];
    }
    return [...parts, { type: 'text', data: { text: update.delta } }];
}
function applyToolAdd(parts, update) {
    var _a;
    const status = (_a = update.status) !== null && _a !== void 0 ? _a : 'loading';
    const data = {
        toolName: update.toolName,
        status,
    };
    if (update.headerContent !== undefined) {
        data.headerContent = update.headerContent;
    }
    if (update.mcpRequest !== undefined) {
        data.mcpRequest = update.mcpRequest;
    }
    return [
        ...parts,
        {
            type: 'tool',
            id: update.id,
            data,
        },
    ];
}
function applyToolUpdate(parts, update) {
    var _a;
    let idx = findContentPartIndex(parts, 'tool', update.item_id);
    let partsToUpdate = parts;
    if (idx < 0) {
        partsToUpdate = applyToolAdd(parts, {
            kind: 'tool_add',
            id: update.item_id,
            toolName: (_a = update.toolName) !== null && _a !== void 0 ? _a : '',
            status: update.status,
        });
        idx = partsToUpdate.length - 1;
    }
    const prev = partsToUpdate[idx];
    if (prev.type !== 'tool')
        return partsToUpdate;
    const bodyText = update.error || update.output || undefined;
    return partsToUpdate.map((p, i) => i === idx
        ? Object.assign(Object.assign({}, prev), { data: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, prev.data), (update.toolName && { toolName: update.toolName })), { status: update.status }), (bodyText ? { bodyContent: bodyText } : {})), (update.mcpRequest === undefined ? {} : { mcpRequest: update.mcpRequest })), (update.mcpResponse === undefined
                ? {}
                : { mcpResponse: update.mcpResponse })) }) : p);
}
function applyThinkingAdd(parts, update) {
    return [
        ...parts,
        {
            type: 'thinking',
            id: update.item_id,
            data: {
                content: '',
                status: 'thinking',
                defaultExpanded: false,
            },
        },
    ];
}
function applyThinkingDelta(parts, update) {
    const idx = findContentPartIndex(parts, 'thinking', update.item_id);
    if (idx < 0 || parts[idx].type !== 'thinking') {
        return null;
    }
    const prev = parts[idx];
    const prevContent = prev.data.content;
    let prevText = '';
    if (typeof prevContent === 'string') {
        prevText = prevContent;
    }
    else if (Array.isArray(prevContent)) {
        prevText = prevContent.join('');
    }
    const newContent = prevText + update.delta;
    return parts.map((p, i) => i === idx ? Object.assign(Object.assign({}, prev), { data: Object.assign(Object.assign({}, prev.data), { content: newContent }) }) : p);
}
function applyThinkingDone(parts, update) {
    const idx = findContentPartIndex(parts, 'thinking', update.item_id);
    if (idx < 0 || parts[idx].type !== 'thinking') {
        return null;
    }
    const prev = parts[idx];
    return parts.map((p, i) => i === idx
        ? Object.assign(Object.assign({}, prev), { data: Object.assign(Object.assign({}, prev.data), { content: update.text, status: 'thought' }) }) : p);
}
//# sourceMappingURL=applyContentUpdate.js.map
