"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TerminalEmulator = void 0;
const headless_1 = require("@xterm/headless");
class TerminalEmulator {
    terminal;
    _dirty = true;
    disposed = false;
    constructor(cols, rows, scrollback = 1000) {
        this.terminal = new headless_1.Terminal({
            cols,
            rows,
            scrollback,
            allowProposedApi: true,
        });
        this.terminal.onWriteParsed(() => {
            this._dirty = true;
        });
    }
    write(data) {
        if (this.disposed)
            return;
        this.terminal.write(data);
    }
    consumeDirty() {
        const was = this._dirty;
        this._dirty = false;
        return was;
    }
    markDirty() {
        this._dirty = true;
    }
    resize(cols, rows) {
        if (this.disposed)
            return;
        this.terminal.resize(Math.max(1, cols), Math.max(1, rows));
        this._dirty = true;
    }
    getCell(row, col) {
        const buffer = this.terminal.buffer.active;
        const line = buffer.getLine(row + buffer.viewportY);
        if (!line)
            return this.emptyCell();
        const cell = line.getCell(col);
        if (!cell)
            return this.emptyCell();
        return {
            char: cell.getChars() || ' ',
            width: cell.getWidth(),
            fg: this.extractColor(cell.isFgDefault(), cell.isFgPalette(), cell.isFgRGB(), cell.getFgColor()),
            bg: this.extractColor(cell.isBgDefault(), cell.isBgPalette(), cell.isBgRGB(), cell.getBgColor()),
            bold: cell.isBold() !== 0,
            italic: cell.isItalic() !== 0,
            underline: cell.isUnderline() !== 0,
            dim: cell.isDim() !== 0,
            inverse: cell.isInverse() !== 0,
            strikethrough: cell.isStrikethrough() !== 0,
        };
    }
    getCursor() {
        if (this.disposed)
            return { x: 0, y: 0 };
        const buf = this.terminal.buffer.active;
        return { x: buf.cursorX, y: buf.cursorY };
    }
    getRow(row) {
        if (this.disposed)
            return [];
        const cells = [];
        for (let c = 0; c < this.terminal.cols; c++) {
            cells.push(this.getCell(row, c));
        }
        return cells;
    }
    scrollUp(lines = 3) {
        if (this.disposed)
            return;
        this.terminal.scrollLines(-lines);
        this._dirty = true;
    }
    scrollDown(lines = 3) {
        if (this.disposed)
            return;
        this.terminal.scrollLines(lines);
        this._dirty = true;
    }
    scrollToBottom() {
        if (this.disposed)
            return;
        this.terminal.scrollToBottom();
        this._dirty = true;
    }
    get isScrolledBack() {
        if (this.disposed)
            return false;
        const buf = this.terminal.buffer.active;
        return buf.viewportY < buf.baseY;
    }
    get cols() { return this.terminal.cols; }
    get rows() { return this.terminal.rows; }
    dispose() {
        this.disposed = true;
        this.terminal.dispose();
    }
    extractColor(isDefault, isPalette, isRGB, value) {
        if (isDefault)
            return { mode: 'default', value: 0 };
        if (isPalette)
            return { mode: 'palette', value };
        if (isRGB)
            return { mode: 'rgb', value };
        return { mode: 'default', value: 0 };
    }
    emptyCell() {
        return {
            char: ' ', width: 1,
            fg: { mode: 'default', value: 0 },
            bg: { mode: 'default', value: 0 },
            bold: false, italic: false, underline: false,
            dim: false, inverse: false, strikethrough: false,
        };
    }
}
exports.TerminalEmulator = TerminalEmulator;
//# sourceMappingURL=terminal-emulator.js.map