import { Text } from '../../../../tui/index.js';
function countReferenceLines(output) {
    const marker = '\nReferences:\n';
    const markerIndex = output.indexOf(marker);
    if (markerIndex === -1)
        return 0;
    const references = output.slice(markerIndex + marker.length);
    return references
        .split('\n')
        .map((line) => line.trim())
        .filter((line) => /^\[\d+\]\s+/.test(line)).length;
}
export function renderWebSearchCall(args, expanded, theme) {
    const parsed = args && typeof args === 'object' ? args : undefined;
    const description = typeof parsed?.description === 'string' && parsed.description.trim()
        ? parsed.description.trim()
        : 'Searching the web';
    const title = theme.fg('toolTitle', theme.bold(`Searching the web: ${description}`));
    if (!expanded) {
        return new Text(title, 0, 0);
    }
    const query = typeof parsed?.query === 'string' ? parsed.query.trim() : '';
    return new Text(query ? `${title}\n${theme.fg('toolOutput', `  ${query}`)}` : title, 0, 0);
}
export function renderWebSearchResult(result, expanded, theme) {
    const textBlocks = result?.content?.filter((block) => block.type === 'text') ?? [];
    const output = textBlocks
        .map((block) => block.text ?? '')
        .join('\n')
        .trim();
    if (!output)
        return undefined;
    if (expanded) {
        return new Text(theme.fg('toolOutput', output), 0, 0);
    }
    const firstLine = output
        .split('\n')
        .find((line) => line.trim())
        ?.trim();
    if (!firstLine)
        return undefined;
    const citationCount = countReferenceLines(output);
    const suffix = citationCount > 0 ? ` [${citationCount} source${citationCount === 1 ? '' : 's'}]` : '';
    return new Text(theme.fg('dim', `  ${firstLine}${suffix}`), 0, 0);
}
//# sourceMappingURL=web-search.js.map