import chalk from 'chalk';
import { emitKeypressEvents } from 'readline';
export async function runStartupUpdatePromptIfNeeded(updateStatus, deps = {}) {
    if (deps.suppress || updateStatus.kind !== 'available' || !updateStatus.command) {
        return { kind: 'continue' };
    }
    const write = deps.write ?? ((text) => process.stdout.write(text));
    const promptSelection = deps.promptSelection ?? promptSelectionInteractive;
    const selection = await promptSelection(updateStatus, write);
    return selection === 'update-now'
        ? { kind: 'run-update', update: updateStatus }
        : { kind: 'continue' };
}
export function renderStartupUpdatePrompt(update, selected = 'update-now') {
    const optionRow = (option, label) => {
        const isSelected = option === selected;
        const prefix = isSelected ? chalk.bold.greenBright('› ') : '  ';
        const text = isSelected ? chalk.bold.greenBright(label) : chalk.dim(label);
        return `${prefix}${text}`;
    };
    return [
        '',
        chalk.green('────────────────────────────────────────────────────────────'),
        chalk.bold.greenBright(' Update Available'),
        chalk.dim(` New version ${update.latestVersion} is available.`),
        chalk.dim(' ShortcutXL can update now before launching the shell.'),
        '',
        optionRow('update-now', 'Update now'),
        optionRow('skip', 'Skip'),
        '',
        chalk.green(' Use ↑/↓ to choose and Enter to continue.'),
        chalk.green('────────────────────────────────────────────────────────────'),
        ''
    ].join('\n');
}
async function promptSelectionInteractive(update, write) {
    if (!process.stdin.isTTY || !process.stdout.isTTY) {
        write(renderStartupUpdatePrompt(update, 'skip'));
        return 'skip';
    }
    return new Promise((resolve) => {
        let selection = 'update-now';
        let renderedLineCount = 0;
        let firstRender = true;
        const render = () => {
            const frame = renderStartupUpdatePrompt(update, selection);
            const nextLineCount = frame.split('\n').length;
            if (firstRender) {
                write('\x1b[?25l');
                write(`${frame}\n`);
                firstRender = false;
            }
            else {
                write(`\x1b[${renderedLineCount}F\x1b[J${frame}\n`);
            }
            renderedLineCount = nextLineCount;
        };
        const cleanup = () => {
            process.stdin.off('keypress', onKeypress);
            if (process.stdin.isTTY) {
                process.stdin.setRawMode(false);
            }
            write('\x1b[?25h');
        };
        const finish = (result) => {
            cleanup();
            resolve(result);
        };
        const onKeypress = (_input, key) => {
            if (key.ctrl && (key.name === 'c' || key.name === 'd')) {
                finish('skip');
                return;
            }
            switch (key.name) {
                case 'up':
                case 'k':
                    selection = 'update-now';
                    render();
                    return;
                case 'down':
                case 'j':
                    selection = 'skip';
                    render();
                    return;
                case 'return':
                    finish(selection);
                    return;
                case 'escape':
                    finish('skip');
                    return;
                default:
                    return;
            }
        };
        emitKeypressEvents(process.stdin);
        process.stdin.setRawMode(true);
        process.stdin.on('keypress', onKeypress);
        render();
    });
}
//# sourceMappingURL=update-prompt.js.map