import { APPROVAL } from '../../app/approvals/types.js';
import { Container, getEditorKeybindings, Spacer, Text } from '../../tui/index.js';
import { editorKey } from '../components/primitives/keybinding-hints.js';
import { ApprovalButtonBar, Border } from './approval.js';
const QUERY_PREVIEW_LINES = 5;
/** Convert snake_case agent type to Title Case for display. */
function formatAgentType(agentType) {
    return agentType.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
}
/** Render the query as preview lines, with an expand hint when truncated. */
function renderQueryLines(query, expanded, contPrefix, theme) {
    const allLines = query.split('\n');
    const displayLines = expanded ? allLines : allLines.slice(0, QUERY_PREVIEW_LINES);
    const lines = displayLines.map((line) => `${contPrefix}${theme.fg('text', line)}`);
    if (!expanded && allLines.length > QUERY_PREVIEW_LINES) {
        lines.push(`${contPrefix}${theme.fg('muted', `... (${allLines.length - QUERY_PREVIEW_LINES} more lines, ${editorKey('expandTools')} to expand)`)}`);
    }
    return lines;
}
/** Render the tree-style list of pending subagent task entries. */
function renderTaskEntries(items, expanded, theme) {
    const lines = [];
    for (let i = 0; i < items.length; i++) {
        const item = items[i];
        const isLast = i === items.length - 1;
        const branch = isLast ? '└─ ' : '├─ ';
        const cont = isLast ? '   ' : '│  ';
        const agentLabel = formatAgentType(item.subagentType);
        const modelSuffix = item.model ? theme.fg('dim', `  ${item.model}`) : '';
        const fullWorkerLabel = theme.fg('warning', '  full worker');
        const launchLabel = theme.fg('muted', `  requested ${item.requestedRunInBackground ? 'background' : 'foreground'}`);
        const recursiveSpawnLabel = item.canSpawnSubagents
            ? theme.fg('warning', '  recursive spawn')
            : '';
        lines.push(`${theme.fg('dim', branch)}${theme.fg('accent', item.name || 'Subagent')} ${theme.fg('dim', `[${agentLabel}]`)}${modelSuffix}${fullWorkerLabel}${launchLabel}${recursiveSpawnLabel}`);
        lines.push(...renderQueryLines(item.query, expanded, theme.fg('dim', cont), theme));
    }
    return lines;
}
export class TaskSpawnApprovalComponent extends Container {
    data;
    theme;
    onDone;
    queueInfo;
    buttonBar;
    expanded = false;
    /** Build the approval component for one or more subagent spawn requests. */
    constructor(data, theme, onDone, queueInfo, _viewport) {
        super();
        this.data = data;
        this.theme = theme;
        this.onDone = onDone;
        this.queueInfo = queueInfo;
        this.buttonBar = new ApprovalButtonBar(theme, `←→ select  enter confirm  ${editorKey('expandTools')} expand  esc cancel`, [APPROVAL.RUN_FOREGROUND, APPROVAL.RUN_BACKGROUND, APPROVAL.REJECT]);
        this.renderContent();
    }
    /** Re-render the approval container's children based on current state. */
    renderContent() {
        this.clear();
        const borderColor = (s) => this.theme.fg('border', s);
        const queueLabel = this.queueInfo
            ? this.theme.fg('accent', ` (${this.queueInfo.current}/${this.queueInfo.total})`)
            : '';
        const count = this.data.length;
        const header = count === 1 ? ' Review subagent spawn' : ` Review ${count} parallel subagent spawns`;
        this.addChild(new Border(borderColor));
        this.addChild(new Text(this.theme.fg('accent', header) + queueLabel, 0, 0));
        this.addChild(new Spacer(1));
        this.addChild(new Text(this.theme.fg('dim', ' Approving this launch authorizes a full-worker subagent for this run.'), 0, 0));
        if (this.data.some((item) => item.canSpawnSubagents)) {
            this.addChild(new Text(this.theme.fg('warning', ' This subagent can recursively spawn more full-worker subagents.'), 0, 0));
            this.addChild(new Spacer(1));
        }
        this.addChild(new Spacer(1));
        this.addChild(new Text(renderTaskEntries(this.data, this.expanded, this.theme).join('\n'), 0, 0));
        this.addChild(new Spacer(1));
        this.addChild(this.buttonBar.getContainer());
        this.addChild(new Border(borderColor));
    }
    /** Route key input to button bar or expand toggle. */
    handleInput(keyData) {
        const choice = this.buttonBar.handleInput(keyData);
        if (choice) {
            this.onDone(choice);
            return;
        }
        if (getEditorKeybindings().matches(keyData, 'expandTools')) {
            this.expanded = !this.expanded;
            this.renderContent();
        }
    }
}
//# sourceMappingURL=task-spawn-approval.js.map