export class FakeTerminalDriver {
    cols;
    rows;
    keyQueue = [];
    writes = [];
    flushes = [];
    rawMode = false;
    altScreen = false;
    lineWrap = true;
    cursorVisible = true;
    destroyed = false;
    enterAltScreenCount = 0;
    exitAltScreenCount = 0;
    enterRawModeCount = 0;
    exitRawModeCount = 0;
    keypressHandlers = new Set();
    resizeHandlers = new Set();
    constructor(cols = 120, rows = 24) {
        this.cols = cols;
        this.rows = rows;
    }
    get output() {
        return this.writes.join("");
    }
    enterRawMode() {
        this.rawMode = true;
        this.enterRawModeCount += 1;
    }
    exitRawMode() {
        this.rawMode = false;
        this.exitRawModeCount += 1;
    }
    enterAltScreen() {
        this.altScreen = true;
        this.enterAltScreenCount += 1;
    }
    exitAltScreen() {
        this.altScreen = false;
        this.exitAltScreenCount += 1;
    }
    disableLineWrap() {
        this.lineWrap = false;
    }
    enableLineWrap() {
        this.lineWrap = true;
    }
    hideCursor() {
        this.cursorVisible = false;
    }
    showCursor() {
        this.cursorVisible = true;
    }
    moveTo(x, y) {
        this.write(`\u001b[${Math.max(1, y + 1)};${Math.max(1, x + 1)}H`);
    }
    write(text) {
        if (!this.destroyed) {
            this.writes.push(text);
        }
    }
    flush(changes) {
        this.flushes.push(changes);
    }
    getSize() {
        return { cols: this.cols, rows: this.rows };
    }
    resize(cols, rows) {
        this.cols = cols;
        this.rows = rows;
        for (const handler of this.resizeHandlers) {
            handler();
        }
    }
    onResize(handler) {
        this.resizeHandlers.add(handler);
        return () => {
            this.resizeHandlers.delete(handler);
        };
    }
    onKeypress(handler) {
        this.keypressHandlers.add(handler);
        return () => {
            this.keypressHandlers.delete(handler);
        };
    }
    press(key) {
        this.keyQueue.push(key);
        for (const handler of this.keypressHandlers) {
            handler(key);
        }
    }
    destroy() {
        this.destroyed = true;
        this.rawMode = false;
        this.lineWrap = true;
        this.altScreen = false;
        this.cursorVisible = true;
        this.keypressHandlers.clear();
        this.resizeHandlers.clear();
    }
}
