/**
 * Minimal fixed footer for Grawkus — bottom-anchored status bar.
 * Keeps the visual interface from v1.36.x without the complex
 * heartbeat/animation system that introduced multi-turn regressions.
 */
import chalk from 'chalk';
import { compactCwd } from './theme.js';
// ── State ──
let active = null;
let state = {
    provider: '', model: '', permissionMode: '', mode: 'dev', sessionId: '',
    cwd: '', sandbox: 'off',
    activity: 'Ready',
    turn: 0,
    cost: 0, tokens: 0,
    draft: '',
    acceptingPrompt: false,
};
const FOOTER_ROWS = 5;
// ANSI helpers
const ANSI = {
    saveCursor: '\x1b[s',
    restoreCursor: '\x1b[u',
    clearLine: '\x1b[K',
    moveTo: (row, col) => `\x1b[${row};${col}H`,
    scrollRegion: (top, bottom) => `\x1b[${top};${bottom}r`,
    resetScrollRegion: '\x1b[r',
};
// ── Public API ──
export function isFooterActive() { return active !== null; }
export function activateFooter(config, sessionId, mode, cwd) {
    if (!process.stdout.isTTY)
        return;
    const rows = process.stdout.rows || 0;
    const cols = process.stdout.columns || 0;
    if (rows < FOOTER_ROWS + 6 || cols < 40)
        return;
    state.provider = config.provider || '';
    state.model = config.model || '';
    state.permissionMode = config.permissionMode || 'ask';
    state.sessionId = sessionId;
    state.mode = mode;
    state.cwd = cwd;
    state.sandbox = config.sandbox?.level || 'off';
    if (active) {
        redraw();
        return;
    }
    active = { rows, cols, scrollBottom: rows - FOOTER_ROWS, footerTop: rows - FOOTER_ROWS + 1 };
    process.stdout.write('\n');
    process.stdout.write(ANSI.scrollRegion(1, active.scrollBottom));
    process.stdout.write(ANSI.moveTo(active.scrollBottom, 1));
    redraw();
}
export function deactivateFooter() {
    if (!active)
        return;
    process.stdout.write(ANSI.saveCursor);
    process.stdout.write(ANSI.resetScrollRegion);
    for (let row = active.footerTop; row <= active.rows; row++) {
        process.stdout.write(ANSI.moveTo(row, 1));
        process.stdout.write(ANSI.clearLine);
    }
    process.stdout.write(ANSI.restoreCursor);
    active = null;
}
export function setFooterActivity(text) {
    state.activity = text;
    if (active)
        redraw();
}
export function setFooterReady() {
    state.activity = 'Ready';
    state.turn = 0;
    if (active)
        redraw();
}
export function setFooterDraft(draft) {
    state.draft = draft;
    if (active && !state.acceptingPrompt)
        redraw();
}
export function setFooterCost(cost, tokens) {
    state.cost = cost;
    state.tokens = tokens;
    if (active)
        redraw();
}
export function updateFooter(config, sessionId, mode, cwd) {
    state.provider = config.provider || '';
    state.model = config.model || '';
    state.permissionMode = config.permissionMode || 'ask';
    state.sessionId = sessionId;
    state.mode = mode;
    state.cwd = cwd;
    state.sandbox = config.sandbox?.level || 'off';
    if (active)
        redraw();
}
// ── Rendering ──
function redraw() {
    if (!active)
        return;
    const lines = buildLines(active.cols);
    process.stdout.write(ANSI.saveCursor);
    for (let i = 0; i < FOOTER_ROWS; i++) {
        process.stdout.write(ANSI.moveTo(active.footerTop + i, 1));
        process.stdout.write(ANSI.clearLine);
        process.stdout.write(lines[i] || '');
    }
    process.stdout.write(ANSI.restoreCursor);
}
function buildLines(cols) {
    const d = chalk.dim;
    const c = chalk.cyan;
    const w = chalk.white;
    const b = chalk.bold;
    // Line 1: Status
    const status = `Provider ${state.provider}  |  Model ${state.model}  |  Permissions ${state.permissionMode}  |  Session ${state.sessionId.slice(0, 12)}`;
    // Line 2: Activity
    const activityLine = `${state.activity}`;
    // Line 3: Prompt
    const prompt = state.draft
        ? `${d('> ')}${w(state.draft)}${d(' ▊')}`
        : d('> ▊ Type your message or @path/to/file');
    // Line 4-5: Info
    const ws = compactCwd(state.cwd);
    const sb = state.sandbox === 'off' ? 'no sandbox' : state.sandbox;
    const cost = `$${state.cost.toFixed(4)} | ${state.tokens} tokens`;
    return [
        d(padRight(status, cols)),
        activityLine,
        prompt,
        d(`workspace         sandbox           mode              cost/tokens`),
        `${ws}${padRight('', 10)}${sb}${padRight('', 10)}${state.mode}${padRight('', 10)}${cost}`,
    ];
}
function padRight(s, len) {
    if (s.length >= len)
        return s;
    return s + ' '.repeat(len - s.length);
}
// Re-export compactCwd for convenience
export { compactCwd };
//# sourceMappingURL=fixed-footer.js.map