/**
 * Rendering helpers for slash command output (/session, /changelog, /hotkeys, /skills).
 *
 * Pure functions that produce TUI Components or formatted text strings
 * from view model data. No side effects — all output is returned to the caller.
 */
import * as path from 'node:path';
import { Markdown, Spacer, Text } from '../../tui/index.js';
import { DynamicBorder } from '../components/primitives/dynamic-border.js';
import { appKey, editorKey } from '../components/primitives/keybinding-hints.js';
import { theme } from '../theme/runtime.js';
function capitalizeKeyDisplay(key) {
    return key
        .split('/')
        .map((k) => k
        .split('+')
        .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
        .join('+'))
        .join('/');
}
/** Build the /session command output — messages, tokens, cost/credits. */
export function buildSessionInfoText(stats) {
    let info = `${theme.bold('Session Info')}\n\n`;
    if (stats.sessionName) {
        info += `${theme.fg('dim', 'Name:')} ${stats.sessionName}\n`;
    }
    info += `${theme.fg('dim', 'File:')} ${stats.sessionFile ?? 'In-memory'}\n`;
    info += `${theme.fg('dim', 'ID:')} ${stats.sessionId}\n\n`;
    info += `${theme.bold('Messages')}\n`;
    info += `${theme.fg('dim', 'User:')} ${stats.userMessages}\n`;
    info += `${theme.fg('dim', 'Assistant:')} ${stats.assistantMessages}\n`;
    info += `${theme.fg('dim', 'Tool Calls:')} ${stats.toolCalls}\n`;
    info += `${theme.fg('dim', 'Tool Results:')} ${stats.toolResults}\n`;
    info += `${theme.fg('dim', 'Total:')} ${stats.totalMessages}\n\n`;
    info += `${theme.bold('Tokens')}\n`;
    info += `${theme.fg('dim', 'Input:')} ${stats.tokens.input.toLocaleString()}\n`;
    info += `${theme.fg('dim', 'Output:')} ${stats.tokens.output.toLocaleString()}\n`;
    if (stats.tokens.cacheRead > 0) {
        info += `${theme.fg('dim', 'Cache Read:')} ${stats.tokens.cacheRead.toLocaleString()}\n`;
    }
    if (stats.tokens.cacheWrite > 0) {
        info += `${theme.fg('dim', 'Cache Write:')} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
    }
    info += `${theme.fg('dim', 'Total:')} ${stats.tokens.total.toLocaleString()}\n`;
    if (stats.creditBalance) {
        info += `\n${theme.bold('Credits')}\n`;
        info += `${theme.fg('dim', 'Used this session:')} ${stats.creditBalance.isUnlimited ? '∞' : stats.creditBalance.creditsUsed.toLocaleString()}`;
    }
    else if (!stats.usesCredits && stats.cost > 0) {
        info += `\n${theme.bold('Cost')}\n`;
        info += `${theme.fg('dim', 'Total:')} $${stats.cost.toFixed(4)}`;
    }
    return info;
}
/** Build TUI components for the /changelog command. */
export function buildChangelogComponents(changelogMarkdown, markdownTheme) {
    return [
        new Spacer(1),
        new DynamicBorder(),
        new Text(theme.bold(theme.fg('accent', "What's New")), 1, 0),
        new Spacer(1),
        new Markdown(changelogMarkdown, 1, 1, markdownTheme),
        new DynamicBorder()
    ];
}
/** Build the /hotkeys command output as a Markdown string with keybinding tables. */
export function buildHotkeysMarkdown(keybindings, platform = process.platform) {
    const appKeyDisplay = (action) => capitalizeKeyDisplay(appKey(keybindings, action));
    const editorKeyDisplay = (action) => capitalizeKeyDisplay(editorKey(action));
    const cursorWordLeft = editorKeyDisplay('cursorWordLeft');
    const cursorWordRight = editorKeyDisplay('cursorWordRight');
    const cursorLineStart = editorKeyDisplay('cursorLineStart');
    const cursorLineEnd = editorKeyDisplay('cursorLineEnd');
    const jumpForward = editorKeyDisplay('jumpForward');
    const jumpBackward = editorKeyDisplay('jumpBackward');
    const pageUp = editorKeyDisplay('pageUp');
    const pageDown = editorKeyDisplay('pageDown');
    const submit = editorKeyDisplay('submit');
    const newLine = editorKeyDisplay('newLine');
    const deleteWordBackward = editorKeyDisplay('deleteWordBackward');
    const deleteWordForward = editorKeyDisplay('deleteWordForward');
    const deleteToLineStart = editorKeyDisplay('deleteToLineStart');
    const deleteToLineEnd = editorKeyDisplay('deleteToLineEnd');
    const yank = editorKeyDisplay('yank');
    const yankPop = editorKeyDisplay('yankPop');
    const undo = editorKeyDisplay('undo');
    const tab = editorKeyDisplay('tab');
    const interrupt = appKeyDisplay('interrupt');
    const clear = appKeyDisplay('clear');
    const exit = appKeyDisplay('exit');
    const suspend = appKeyDisplay('suspend');
    const cycleThinkingLevel = appKeyDisplay('cycleThinkingLevel');
    const toggleFastMode = appKeyDisplay('toggleFastMode');
    const toggleAutoApprove = appKeyDisplay('toggleAutoApprove');
    const toggleRuntimePermissionBypass = appKeyDisplay('toggleRuntimePermissionBypass');
    const cycleModelForward = appKeyDisplay('cycleModelForward');
    const selectModel = appKeyDisplay('selectModel');
    const expandTools = appKeyDisplay('expandTools');
    const toggleThinking = appKeyDisplay('toggleThinking');
    const externalEditor = appKeyDisplay('externalEditor');
    const followUp = appKeyDisplay('followUp');
    const dequeue = appKeyDisplay('dequeue');
    return `
**Navigation**
| Key | Action |
|-----|--------|
| \`Arrow keys\` | Move cursor / browse history (Up when empty) |
| \`${cursorWordLeft}\` / \`${cursorWordRight}\` | Move by word |
| \`${cursorLineStart}\` | Start of line |
| \`${cursorLineEnd}\` | End of line |
| \`${jumpForward}\` | Jump forward to character |
| \`${jumpBackward}\` | Jump backward to character |
| \`${pageUp}\` / \`${pageDown}\` | Scroll by page |

**Editing**
| Key | Action |
|-----|--------|
| \`${submit}\` | Send message |
| \`${newLine}\` | New line${platform === 'win32' ? ' (Ctrl+Enter on Windows Terminal)' : ''} |
| \`${deleteWordBackward}\` | Delete word backwards |
| \`${deleteWordForward}\` | Delete word forwards |
| \`${deleteToLineStart}\` | Delete to start of line |
| \`${deleteToLineEnd}\` | Delete to end of line |
| \`${yank}\` | Paste the most-recently-deleted text |
| \`${yankPop}\` | Cycle through the deleted text after pasting |
| \`${undo}\` | Undo |

**Other**
| Key | Action |
|-----|--------|
| \`${tab}\` | Path completion / accept autocomplete |
| \`${interrupt}\` | Cancel autocomplete / abort streaming |
| \`${clear}\` | Clear editor (first) / exit (second) |
| \`${exit}\` | Exit (when editor is empty) |
| \`${suspend}\` | Suspend to background |
| \`${cycleThinkingLevel}\` | Cycle thinking levels |
| \`${toggleFastMode}\` | Toggle fast mode for subsequent turns |
| \`${toggleAutoApprove}\` | Toggle auto-approve for spreadsheet changes |
| \`${toggleRuntimePermissionBypass}\` | Toggle file/shell permission bypass |
| \`${cycleModelForward}\` | Cycle models |
| \`${selectModel}\` | Open model selector |
| \`${expandTools}\` | Toggle tool output expansion |
| \`${toggleThinking}\` | Toggle thinking block visibility |
| \`${externalEditor}\` | Edit message in external editor |
| \`${followUp}\` | Queue follow-up message |
| \`${dequeue}\` | Restore queued messages |
| \`Esc\` twice | Open conversation tree |
| \`Ctrl+V\` | Paste image from clipboard |
| \`/\` | Slash commands |
| \`!\` | Run bash command |
`.trim();
}
/** Build TUI components for the /hotkeys command. */
export function buildHotkeysComponents(keybindings, markdownTheme, platform = process.platform) {
    return [
        new Spacer(1),
        new DynamicBorder(),
        new Text(theme.bold(theme.fg('accent', 'Keyboard Shortcuts')), 1, 0),
        new Spacer(1),
        new Markdown(buildHotkeysMarkdown(keybindings, platform), 1, 1, markdownTheme),
        new DynamicBorder()
    ];
}
/** Format the skills list as a grouped text string for the /skills command. */
export function buildSkillsText(skills, groups, formatDisplayPath) {
    const skillByPath = new Map(skills.map((skill) => [skill.filePath, skill]));
    const lines = [];
    for (const group of groups) {
        const allPaths = [...group.paths, ...Array.from(group.packages.values()).flat()];
        const groupSkills = allPaths
            .map((p) => skillByPath.get(p))
            .filter((s) => s != null)
            .sort((a, b) => a.name.localeCompare(b.name));
        const parentDirs = [...new Set(groupSkills.map((skill) => path.dirname(skill.baseDir)))];
        const dir = formatDisplayPath(parentDirs[0] ?? '');
        lines.push(`  ${theme.fg('dim', dir)}`);
        for (const skill of groupSkills) {
            const desc = skill.description ? theme.fg('dim', ` — ${skill.description}`) : '';
            lines.push(`    ${theme.fg('accent', `/${skill.name}`)}${desc}`);
        }
    }
    return lines.join('\n');
}
/** Build TUI components for the /skills command. */
export function buildSkillsComponents(skillsText) {
    const components = [
        new Spacer(1),
        new DynamicBorder(),
        new Text(theme.bold(theme.fg('accent', 'Loaded Skills')), 1, 0),
        new Spacer(1)
    ];
    if (skillsText === null) {
        components.push(new Text(theme.fg('dim', '  No skills loaded.'), 0, 0));
    }
    else {
        components.push(new Text(skillsText, 0, 0));
    }
    components.push(new Spacer(1));
    components.push(new DynamicBorder());
    return components;
}
//# sourceMappingURL=slash-command-rendering.js.map