/**
 * TUI renderers for search/filesystem tools (ls, find, grep).
 *
 * Each tool has a renderCall (styled header with path/pattern) and
 * renderResult (output lines with truncation warnings).
 */
import { Text } from '../../../../tui/index.js';
import { formatSize } from '../../../../utils/truncate.js';
import { coerceToolString, shortenHomePath } from '../../runtime/tool-execution-types.js';
function invalidArg(theme) {
    return theme.fg('error', '[invalid arg]');
}
/** Format search output with optional line truncation for collapsed view. */
function formatSearchOutput(textOutput, expanded, maxCollapsedLines, theme) {
    const trimmedOutput = textOutput.trim();
    if (!trimmedOutput) {
        return '';
    }
    const lines = trimmedOutput.split('\n');
    const maxLines = expanded ? lines.length : maxCollapsedLines;
    const displayLines = lines.slice(0, maxLines);
    const remaining = lines.length - maxLines;
    let text = '';
    if (displayLines.length > 0) {
        text += displayLines.map((line) => theme.fg('toolOutput', line)).join('\n');
    }
    if (remaining > 0) {
        text += `${text ? '\n' : ''}${theme.fg('muted', `... (${remaining} more lines)`)}`;
    }
    return text;
}
export function renderLsCall(args, theme) {
    const parsedArgs = (args ?? {});
    const rawPath = coerceToolString(parsedArgs.path);
    const path = rawPath !== null ? shortenHomePath(rawPath || '.') : null;
    const limit = typeof parsedArgs.limit === 'number' ? parsedArgs.limit : undefined;
    let text = `${theme.fg('toolTitle', theme.bold('ls'))} ${path === null ? invalidArg(theme) : theme.fg('accent', path)}`;
    if (limit !== undefined) {
        text += theme.fg('toolOutput', ` (limit ${limit})`);
    }
    return new Text(text, 0, 0);
}
export function renderLsResult(result, expanded, textOutput, theme) {
    if (!result)
        return undefined;
    const chunks = [];
    const output = formatSearchOutput(textOutput, expanded, 20, theme);
    if (output) {
        chunks.push(output);
    }
    const entryLimit = result.details?.entryLimitReached;
    const truncation = result.details?.truncation;
    if (entryLimit || truncation?.truncated) {
        const warnings = [];
        if (entryLimit)
            warnings.push(`${entryLimit} entries limit`);
        if (truncation?.truncated)
            warnings.push(`${formatSize(truncation.maxChars)} char limit`);
        chunks.push(theme.fg('warning', `[Truncated: ${warnings.join(', ')}]`));
    }
    return chunks.length > 0 ? new Text(chunks.join('\n'), 0, 0) : undefined;
}
export function renderFindCall(args, theme) {
    const parsedArgs = (args ?? {});
    const pattern = coerceToolString(parsedArgs.pattern);
    const rawPath = coerceToolString(parsedArgs.path);
    const path = rawPath !== null ? shortenHomePath(rawPath || '.') : null;
    const limit = typeof parsedArgs.limit === 'number' ? parsedArgs.limit : undefined;
    let text = theme.fg('toolTitle', theme.bold('find')) +
        ' ' +
        (pattern === null ? invalidArg(theme) : theme.fg('accent', pattern || '')) +
        theme.fg('toolOutput', ` in ${path === null ? invalidArg(theme) : path}`);
    if (limit !== undefined) {
        text += theme.fg('toolOutput', ` (limit ${limit})`);
    }
    return new Text(text, 0, 0);
}
export function renderFindResult(result, expanded, textOutput, theme) {
    if (!result)
        return undefined;
    const chunks = [];
    const output = formatSearchOutput(textOutput, expanded, 20, theme);
    if (output) {
        chunks.push(output);
    }
    const resultLimit = result.details?.resultLimitReached;
    const truncation = result.details?.truncation;
    if (resultLimit || truncation?.truncated) {
        const warnings = [];
        if (resultLimit)
            warnings.push(`${resultLimit} results limit`);
        if (truncation?.truncated)
            warnings.push(`${formatSize(truncation.maxChars)} char limit`);
        chunks.push(theme.fg('warning', `[Truncated: ${warnings.join(', ')}]`));
    }
    return chunks.length > 0 ? new Text(chunks.join('\n'), 0, 0) : undefined;
}
export function renderGrepCall(args, theme) {
    const parsedArgs = (args ?? {});
    const pattern = coerceToolString(parsedArgs.pattern);
    const rawPath = coerceToolString(parsedArgs.path);
    const path = rawPath !== null ? shortenHomePath(rawPath || '.') : null;
    const glob = coerceToolString(parsedArgs.glob);
    const limit = typeof parsedArgs.limit === 'number' ? parsedArgs.limit : undefined;
    let text = theme.fg('toolTitle', theme.bold('grep')) +
        ' ' +
        (pattern === null ? invalidArg(theme) : theme.fg('accent', `/${pattern || ''}/`)) +
        theme.fg('toolOutput', ` in ${path === null ? invalidArg(theme) : path}`);
    if (glob) {
        text += theme.fg('toolOutput', ` (${glob})`);
    }
    if (limit !== undefined) {
        text += theme.fg('toolOutput', ` limit ${limit}`);
    }
    return new Text(text, 0, 0);
}
export function renderGrepResult(result, expanded, textOutput, theme) {
    if (!result)
        return undefined;
    const chunks = [];
    const output = formatSearchOutput(textOutput, expanded, 0, theme);
    if (output) {
        chunks.push(output);
    }
    const matchLimit = result.details?.matchLimitReached;
    const truncation = result.details?.truncation;
    const linesTruncated = result.details?.linesTruncated;
    if (matchLimit || truncation?.truncated || linesTruncated) {
        const warnings = [];
        if (matchLimit)
            warnings.push(`${matchLimit} matches limit`);
        if (truncation?.truncated)
            warnings.push(`${formatSize(truncation.maxChars)} char limit`);
        if (linesTruncated)
            warnings.push('some lines truncated');
        chunks.push(theme.fg('warning', `[Truncated: ${warnings.join(', ')}]`));
    }
    return chunks.length > 0 ? new Text(chunks.join('\n'), 0, 0) : undefined;
}
//# sourceMappingURL=search-ops.js.map