"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliTrainerDisplay = void 0;
const chalk = require('chalk');
class CliTrainerDisplay {
    constructor(options) {
        this.steps = [];
        this.status = 'waiting';
        this.statusMessage = '';
        this.lastRenderHeight = 0;
        this.testName = options.testName;
        this.url = options.url;
        this.maxVisibleSteps = options.maxVisibleSteps ?? 15;
        this.logger = options.logger;
    }
    log(...args) {
        if (this.logger) {
            this.logger.terminal(...args);
        }
        else {
            console.log(...args);
        }
    }
    initialize() {
        this.clearScreen();
        this.render();
    }
    addStep(index, description) {
        this.steps.push({ index, description });
        this.render();
    }
    setStatus(status, message) {
        this.status = status;
        this.statusMessage = message ?? '';
        this.render();
    }
    clearSteps() {
        this.steps = [];
        this.render();
    }
    render() {
        if (this.lastRenderHeight > 0) {
            process.stdout.write(`\x1B[${this.lastRenderHeight}A`);
            for (let i = 0; i < this.lastRenderHeight; i++) {
                process.stdout.write('\x1B[2K\n');
            }
            process.stdout.write(`\x1B[${this.lastRenderHeight}A`);
        }
        const lines = [];
        lines.push(this.renderHeader());
        lines.push(chalk.gray('─'.repeat(60)));
        const stepLines = this.renderSteps();
        lines.push(...stepLines);
        lines.push(chalk.gray('─'.repeat(60)));
        lines.push(this.renderStatus());
        lines.push(this.renderControls());
        const output = lines.join('\n');
        this.log(output);
        this.lastRenderHeight = lines.length;
    }
    renderHeader() {
        const mabl = chalk.magenta.bold('mabl');
        const trainer = chalk.white.bold('Trainer');
        const stepCount = chalk.cyan(`${this.steps.length} steps`);
        return `${mabl} ${trainer}  ${chalk.gray('|')}  ${stepCount}`;
    }
    renderSteps() {
        const lines = [];
        if (this.steps.length === 0) {
            lines.push('');
            lines.push(chalk.gray('  No steps recorded yet.'));
            lines.push(chalk.gray('  Interact with the browser to record steps.'));
            lines.push('');
            return lines;
        }
        const startIndex = Math.max(0, this.steps.length - this.maxVisibleSteps);
        if (startIndex > 0) {
            lines.push(chalk.gray(`  ... ${startIndex} more steps above`));
        }
        for (let i = startIndex; i < this.steps.length; i++) {
            const step = this.steps[i];
            lines.push(this.renderStep(step));
        }
        return lines;
    }
    renderStep(step) {
        const indexStr = chalk.gray(`${step.index}.`.padStart(4));
        const description = step.isDisabled
            ? chalk.strikethrough.gray(step.description)
            : chalk.white(step.description);
        return `${indexStr} ${description}`;
    }
    renderStatus() {
        let statusIcon;
        let statusText;
        let statusColor;
        switch (this.status) {
            case 'recording':
                statusIcon = '●';
                statusText = 'Recording';
                statusColor = chalk.red;
                break;
            case 'waiting':
                statusIcon = '○';
                statusText = 'Waiting for input';
                statusColor = chalk.yellow;
                break;
            case 'paused':
                statusIcon = '⏸';
                statusText = 'Paused';
                statusColor = chalk.gray;
                break;
            case 'saving':
                statusIcon = '↻';
                statusText = 'Saving...';
                statusColor = chalk.cyan;
                break;
            case 'saved':
                statusIcon = '✓';
                statusText = 'Saved';
                statusColor = chalk.green;
                break;
            case 'error':
                statusIcon = '✗';
                statusText = 'Error';
                statusColor = chalk.red;
                break;
        }
        const status = statusColor(`${statusIcon} ${statusText}`);
        const message = this.statusMessage
            ? chalk.gray(` - ${this.statusMessage}`)
            : '';
        return `${status}${message}`;
    }
    renderControls() {
        return chalk.gray('Press [Enter] to stop recording  |  [Ctrl+C] to cancel');
    }
    clearScreen() {
        process.stdout.write('\x1B[2J\x1B[0f');
    }
    printSummary() {
        this.log('');
        this.log(chalk.gray('─'.repeat(60)));
        this.log(chalk.bold('Training Summary'));
        this.log(chalk.gray('─'.repeat(60)));
        this.log(`Test Name: ${chalk.cyan(this.testName)}`);
        this.log(`URL: ${chalk.gray(this.url)}`);
        this.log(`Total Steps: ${chalk.cyan(this.steps.length.toString())}`);
        this.log('');
        if (this.steps.length > 0) {
            this.log(chalk.bold('Recorded Steps:'));
            for (const step of this.steps) {
                this.log(`  ${chalk.gray(`${step.index}.`)} ${step.description}`);
            }
        }
        this.log('');
    }
    async waitForEnter(rl) {
        return new Promise((resolve) => {
            rl.once('line', () => {
                resolve();
            });
        });
    }
}
exports.CliTrainerDisplay = CliTrainerDisplay;
