"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Spinner = void 0;
// Manually implemented spinner to
const cli_spinners_1 = __importDefault(require("cli-spinners"));
const art_1 = require("./art");
const chalk_1 = __importDefault(require("chalk"));
const TokenConfig_1 = require("../controllers/TokenControllerUtils/TokenConfig");
const { frames, interval } = cli_spinners_1.default.arc;
const FRAME_REPEAT_FACTOR = 4;
const actualFrames = frames.flatMap((frame) => Array(FRAME_REPEAT_FACTOR).fill(frame));
const actualInterval = interval / FRAME_REPEAT_FACTOR;
class Spinner {
    constructor() {
        this.genericStyle = chalk_1.default.gray;
        this.highlightStyle = chalk_1.default.gray.bold;
        this.av = new art_1.Art(this.genericStyle, this.highlightStyle);
        this.tokenCounter = 0;
        this.leftFillCharacter = "#";
        this.rightFillCharacter = " ";
        this.interval = actualInterval;
        this.frames = actualFrames;
        this.frameIndex = 0;
        this.timer = null;
    }
    start() {
        this.timer = setInterval(() => {
            const totalLength = process.stdout.columns - 1;
            const leftScarf = "## ";
            const rightScarf = " ##";
            let midStr = " ".repeat(totalLength - (leftScarf + rightScarf).length);
            const availableLength = midStr.length;
            const ratio = this.tokenCounter / new TokenConfig_1.TokenConfig().maxCompletionTokens;
            const SPEEDFACTOR = 1;
            // let midFilledLen = Math.min(
            //   Math.floor(ratio * availableLength * SPEEDFACTOR) % availableLength,
            //   availableLength - 2
            // );
            let midFilledLen = Math.min(Math.floor(this.tokenCounter * SPEEDFACTOR) % availableLength, availableLength - 2);
            if (Math.floor(this.tokenCounter * SPEEDFACTOR) / availableLength > 1) {
                this.rightFillCharacter = "#"; // filled
            }
            else {
                this.rightFillCharacter = " ";
            }
            let currFrame = (midFilledLen === 0 ? "" : " ") + this.frames[this.frameIndex];
            const midUnfilledLen = availableLength - midFilledLen - currFrame.length;
            currFrame = currFrame + (midUnfilledLen > 0 ? " " : "");
            // format here ONLY
            midStr =
                chalk_1.default.dim.gray(this.leftFillCharacter.repeat(midFilledLen)) +
                    chalk_1.default.bold.yellowBright(currFrame) +
                    chalk_1.default.dim.gray(this.rightFillCharacter.repeat(midUnfilledLen > 0 ? midUnfilledLen : 0));
            let str = chalk_1.default.dim.gray(leftScarf) + midStr + chalk_1.default.dim.gray(rightScarf);
            this.frameIndex = (this.frameIndex + 1) % this.frames.length;
            process.stdout.write(str + "\r");
        }, this.interval);
    }
    stop() {
        if (this.timer !== null) {
            clearInterval(this.timer);
        }
        process.stdout.cursorTo(0);
        process.stdout.clearLine(0);
    }
    incrementTokenCounter(str) {
        if (str.includes("\n")) {
            this.tokenCounter = 0;
        }
        else {
            this.tokenCounter += str.length;
        }
    }
    resetTokenCounter() {
        this.tokenCounter = 0;
    }
}
exports.Spinner = Spinner;
