"use strict";
/**
 * Simple Progress Indicator
 *
 * Features:
 * - ASCII-only spinner frames (cross-platform compatible)
 * - TTY detection (no spinners in pipes/logs)
 * - Elapsed time display
 * - CI environment detection
 * - Color support via UI system
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressIndicator = void 0;
const colors_1 = require("./ui/colors");
class ProgressIndicator {
    /**
     * Create a progress indicator
     * @param message - Message to display
     * @param options - Options
     */
    constructor(message, options = {}) {
        this.message = message;
        // ASCII-only frames for cross-platform compatibility
        this.frames = options.frames || ['|', '/', '-', '\\'];
        this.frameIndex = 0;
        this.interval = null;
        this.startTime = Date.now();
        // TTY detection: only animate if stderr is TTY and not in CI
        this.isTTY = process.stderr.isTTY === true && !process.env.CI && !process.env.NO_COLOR;
    }
    /**
     * Start the spinner
     */
    start() {
        if (!this.isTTY) {
            // Non-TTY: just print message once
            process.stderr.write(`${(0, colors_1.color)('[i]', 'info')} ${this.message}...\n`);
            return;
        }
        // TTY: animate spinner
        this.interval = setInterval(() => {
            const frame = this.frames[this.frameIndex];
            const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(1);
            process.stderr.write(`\r${(0, colors_1.color)(`[${frame}]`, 'info')} ${this.message}... (${elapsed}s)`);
            this.frameIndex = (this.frameIndex + 1) % this.frames.length;
        }, 80); // 12.5fps for smooth animation
    }
    /**
     * Stop spinner with success message
     * @param message - Optional success message (defaults to original message)
     */
    succeed(message) {
        this.stop();
        const finalMessage = message || this.message;
        const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(1);
        if (this.isTTY) {
            // Clear spinner line and show success
            process.stderr.write(`\r${(0, colors_1.color)('[OK]', 'success')} ${finalMessage} (${elapsed}s)\n`);
        }
        else {
            // Non-TTY: just show completion
            process.stderr.write(`${(0, colors_1.color)('[OK]', 'success')} ${finalMessage}\n`);
        }
    }
    /**
     * Stop spinner with failure message
     * @param message - Optional failure message (defaults to original message)
     */
    fail(message) {
        this.stop();
        const finalMessage = message || this.message;
        if (this.isTTY) {
            // Clear spinner line and show failure
            process.stderr.write(`\r${(0, colors_1.color)('[X]', 'error')} ${finalMessage}\n`);
        }
        else {
            // Non-TTY: just show failure
            process.stderr.write(`${(0, colors_1.color)('[X]', 'error')} ${finalMessage}\n`);
        }
    }
    /**
     * Update spinner message (while running)
     * @param newMessage - New message to display
     */
    update(newMessage) {
        this.message = newMessage;
    }
    /**
     * Stop the spinner without showing success/failure
     */
    stop() {
        if (this.interval) {
            clearInterval(this.interval);
            this.interval = null;
            if (this.isTTY) {
                // Clear the spinner line
                process.stderr.write('\r\x1b[K');
            }
        }
    }
}
exports.ProgressIndicator = ProgressIndicator;
exports.default = ProgressIndicator;
//# sourceMappingURL=progress-indicator.js.map