/**
 * Browser-backed terminal transport for the bridge-hosted native TUI.
 * It implements the pi-tui Terminal contract but forwards output over the
 * RPC bridge instead of writing to process stdout.
 */
export class RemoteTerminal {
    constructor(options) {
        this.options = options;
        this._columns = Math.max(1, options.initialColumns ?? 120);
        this._rows = Math.max(1, options.initialRows ?? 30);
    }
    start(onInput, onResize) {
        this.inputHandler = onInput;
        this.resizeHandler = onResize;
    }
    stop() {
        this.inputHandler = undefined;
        this.resizeHandler = undefined;
    }
    async drainInput() {
        // Browser transport has no local stdin buffer to drain.
    }
    write(data) {
        if (!data)
            return;
        this.options.onWrite(data);
    }
    get columns() {
        return this._columns;
    }
    get rows() {
        return this._rows;
    }
    get kittyProtocolActive() {
        return false;
    }
    pushInput(data) {
        if (!data)
            return;
        this.inputHandler?.(data);
    }
    resize(columns, rows) {
        const nextColumns = Math.max(1, Math.floor(columns));
        const nextRows = Math.max(1, Math.floor(rows));
        const changed = nextColumns !== this._columns || nextRows !== this._rows;
        this._columns = nextColumns;
        this._rows = nextRows;
        if (changed) {
            this.resizeHandler?.();
        }
    }
    moveBy(lines) {
        if (lines > 0) {
            this.write(`\x1b[${lines}B`);
        }
        else if (lines < 0) {
            this.write(`\x1b[${-lines}A`);
        }
    }
    hideCursor() {
        this.write("\x1b[?25l");
    }
    showCursor() {
        this.write("\x1b[?25h");
    }
    clearLine() {
        this.write("\x1b[K");
    }
    clearFromCursor() {
        this.write("\x1b[J");
    }
    clearScreen() {
        this.write("\x1b[2J\x1b[H");
    }
    setTitle(title) {
        this.write(`\x1b]0;${title}\x07`);
    }
}
//# sourceMappingURL=remote-terminal.js.map