"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateView = void 0;
const chalk_1 = __importDefault(require("chalk"));
const art_1 = require("../utils/art");
const marked_utils_1 = require("../utils/marked-utils");
const strip_ansi_1 = require("../utils/strip-ansi");
class StateView {
    constructor() {
        this.genericStyle = chalk_1.default.green;
        this.highlightStyle = chalk_1.default.redBright.bold;
        this.av = new art_1.Art(this.genericStyle, this.highlightStyle);
    }
    render(input, withNewline = true) {
        process.stdout.write(input);
        if (withNewline) {
            process.stdout.write("\n");
        }
    }
    renderAppendBg(input) {
        // Split input by `\n`
        const lines = input.split("\n");
        // For each line...
        for (const line of lines) {
            // Calculate length of line without ANSI characters
            const noAnsiLen = (0, strip_ansi_1.stripAnsi)(line).length;
            // Calculate number of spaces needed to fill console width
            const numSpaces = process.stdout.columns - noAnsiLen;
            let paddedLine = line + "";
            if (numSpaces > 0) {
                // Append spaces to line
                paddedLine = line + " ".repeat(numSpaces);
            }
            // Write line with no background (any background makes it all look worse lol)
            process.stdout.write(paddedLine);
            // Write newline character to start a new line
            process.stdout.write("\n");
        }
    }
    headerRender(input) {
        const str = this.av.createMessage(input);
        this.render(str);
    }
    conversationStateRenamed(oldName, newName) {
        this.headerRender(`Current conversation state **${oldName}** renamed to **${newName}**`);
    }
    conversationCannotRenameToSameName(newName) {
        this.headerRender(`Cannot rename to the same name **${newName}**`);
    }
    conversationStateSaved(saveName) {
        this.headerRender(`Conversation state **${saveName}** saved`);
    }
    conversationStateLoadDoesNotExist(saveName) {
        this.headerRender(`Conversation state **${saveName}** does not exist`);
    }
    conversationStateLoaded(saveName, ch) {
        if (ch.length === 0) {
            this.headerRender(`Conversation state **${saveName}** loaded, with **no history** found`);
            return;
        }
        this.headerRender(`Loading **${saveName}** conversation history ⌛`);
        ch.forEach((m, ind) => {
            const displayKey = (0, art_1.processCenterMessage)([
                { content: "--", styler: chalk_1.default.dim.blue },
                {
                    content: `${m.key === "user" ? "👋" : "🤖"}::`,
                    styler: chalk_1.default.blue.bold,
                },
                { content: `${Math.floor(ind / 2)}::`, styler: chalk_1.default.blue },
                { content: `${saveName}`, styler: chalk_1.default.gray.italic },
                { content: "--", styler: chalk_1.default.dim.blue },
            ], { content: "-", styler: chalk_1.default.bold.blue });
            this.render(displayKey);
            let displayContent = m.content;
            if (m.key === "user") {
                displayContent = chalk_1.default.gray(displayContent);
            }
            else {
                displayContent = (0, marked_utils_1.mdBlockStr)(displayContent);
            }
            this.render(displayContent, ind !== ch.length - 1);
        });
        this.render((0, art_1.processCenterMessage)([
            { content: "--", styler: chalk_1.default.dim.blue },
            {
                content: `👋 END 🤖`,
                styler: chalk_1.default.blue.bold,
            },
            { content: "--", styler: chalk_1.default.dim.blue },
        ], { content: "-", styler: chalk_1.default.bold.blue }));
        this.headerRender(`Conversation history: **${saveName}** loaded 📝`);
    }
    conversationStateDeleted(saveName) {
        this.headerRender(`Conversation state **${saveName}** deleted`);
    }
    noSaveFound(saveName) {
        this.headerRender(`No save found with the name **${saveName}**`);
    }
    savedConversationStatesList(saveNames) {
        if (saveNames.length > 0) {
            this.headerRender(`Saved conversation states:`);
            saveNames.forEach((saveName) => this.headerRender(`- ${saveName}`));
        }
        else {
            this.headerRender(`No conversation states saved`);
        }
    }
    renderSaveOverwrite(saveName) {
        this.headerRender(`Save **${saveName}** already exists. Overwrite with **/so ${saveName}**`);
    }
    renderNoSaveName() {
        this.headerRender("No save name provided and no save name currently set");
    }
    renderSuccessCacheMove(saveName) {
        this.headerRender(`Saved cache to **${saveName}**, cache is reset`);
    }
    renderSaveOverwriteFromCache(saveName) {
        this.headerRender(`Save **${saveName}** already exists. Use overwrite with **/sco ${saveName}**`);
    }
    renderCacheDoesNotExist() {
        this.headerRender("Cache does not exist");
    }
    renderLoadingAlreadyCurrentState(saveName) {
        this.headerRender(`Save **${saveName}** is already loaded`);
    }
    renderSavedToCacheBeforeLoad() {
        this.headerRender("To properly save **cache**, use **/sc <new-name>**");
    }
    allConversationStatesDeleted() {
        this.headerRender("All convos deleted, use **/delete <save-name>** to delete by name");
    }
}
exports.StateView = StateView;
