/**
 * Vigthoria CLI — Multi-Step Terminal Task Display
 *
 * Renders a live, updating task progress list in the terminal using ANSI
 * escape codes and chalk — no external UI framework required.
 *
 * Only activates when stderr is a real TTY; JSON mode and piped output stay clean.
 */
import chalk from 'chalk';
const ICONS = {
    pending: chalk.gray('○'),
    running: chalk.cyan('⟳'),
    done: chalk.green('✓'),
    error: chalk.red('✗'),
    skipped: chalk.gray('\u2013'),
};
const LABEL_FN = {
    pending: (s) => chalk.gray(s),
    running: (s) => chalk.cyan.bold(s),
    done: (s) => chalk.gray(s),
    error: (s) => chalk.red(s),
    skipped: (s) => chalk.gray(s),
};
export class TaskDisplay {
    tasks = [];
    linesRendered = 0;
    enabled;
    constructor(labels, enabled = true) {
        this.enabled = enabled && process.stderr.isTTY === true;
        this.tasks = labels.map((label) => ({ label, status: 'pending' }));
    }
    clearLines() {
        if (this.linesRendered > 0) {
            process.stderr.write(`\u001b[${this.linesRendered}A\u001b[0J`);
        }
    }
    renderLines() {
        return this.tasks.map((t) => {
            const icon = ICONS[t.status];
            const label = LABEL_FN[t.status](t.label);
            const detail = t.detail ? chalk.gray(` \u2014 ${t.detail.slice(0, 60)}`) : '';
            return `  ${icon} ${label}${detail}`;
        });
    }
    render() {
        if (!this.enabled)
            return;
        this.clearLines();
        const lines = this.renderLines();
        process.stderr.write(lines.join('\n') + '\n');
        this.linesRendered = lines.length;
    }
    start(index, detail) {
        if (!this.enabled || index < 0 || index >= this.tasks.length)
            return;
        this.tasks[index].status = 'running';
        if (detail !== undefined)
            this.tasks[index].detail = detail;
        this.render();
    }
    complete(index, detail) {
        if (!this.enabled || index < 0 || index >= this.tasks.length)
            return;
        this.tasks[index].status = 'done';
        if (detail !== undefined)
            this.tasks[index].detail = detail;
        this.render();
    }
    fail(index, detail) {
        if (!this.enabled || index < 0 || index >= this.tasks.length)
            return;
        this.tasks[index].status = 'error';
        if (detail !== undefined)
            this.tasks[index].detail = detail;
        this.render();
    }
    skip(index, detail) {
        if (!this.enabled || index < 0 || index >= this.tasks.length)
            return;
        this.tasks[index].status = 'skipped';
        if (detail !== undefined)
            this.tasks[index].detail = detail;
        this.render();
    }
    setDetail(index, detail) {
        if (!this.enabled || index < 0 || index >= this.tasks.length)
            return;
        this.tasks[index].detail = detail;
        this.render();
    }
    clear() {
        if (!this.enabled)
            return;
        this.clearLines();
        this.linesRendered = 0;
    }
    finalize() {
        if (!this.enabled)
            return;
        this.clearLines();
        const lines = this.renderLines();
        process.stderr.write(lines.join('\n') + '\n');
        this.linesRendered = 0;
    }
    get isEnabled() {
        return this.enabled;
    }
}
