import type { ToolCall, ContentSegment } from '../types/search';

export function splitContentByToolCalls(
  content: string | undefined,
  toolCalls: ToolCall[] | undefined,
): ContentSegment[] {
  if (!toolCalls || toolCalls.length === 0) {
    return [{ type: 'text', text: content || '' }];
  }

  const segments: ContentSegment[] = [];
  let lastPos = 0;

  for (const toolCall of toolCalls) {
    if (content && toolCall.position > lastPos) {
      segments.push({ type: 'text', text: content.substring(lastPos, toolCall.position) });
    }
    segments.push({ type: 'tool', toolCall });
    lastPos = toolCall.position;
  }

  if (content && lastPos < content.length) {
    segments.push({ type: 'text', text: content.substring(lastPos) });
  }

  return segments;
}
