/**
 * Cross-platform terminal clearing with scrollback support.
 * Detects modern terminals that support ESC[3J for clearing scrollback.
 */
import { CURSOR_HOME, csi, ERASE_SCREEN, ERASE_SCROLLBACK, } from './termio/csi.js';
// HVP (Horizontal Vertical Position) - legacy Windows cursor home
const CURSOR_HOME_WINDOWS = csi(0, 'f');
function isWindowsTerminal() {
    return process.platform === 'win32' && !!process.env.WT_SESSION;
}
function isMintty() {
    // mintty 3.1.5+ sets TERM_PROGRAM to 'mintty'
    if (process.env.TERM_PROGRAM === 'mintty') {
        return true;
    }
    // GitBash/MSYS2/MINGW use mintty and set MSYSTEM
    if (process.platform === 'win32' && process.env.MSYSTEM) {
        return true;
    }
    return false;
}
function isModernWindowsTerminal() {
    // Windows Terminal sets WT_SESSION environment variable
    if (isWindowsTerminal()) {
        return true;
    }
    // VS Code integrated terminal on Windows with ConPTY support
    if (process.platform === 'win32' &&
        process.env.TERM_PROGRAM === 'vscode' &&
        process.env.TERM_PROGRAM_VERSION) {
        return true;
    }
    // mintty (GitBash/MSYS2/Cygwin) supports modern escape sequences
    if (isMintty()) {
        return true;
    }
    return false;
}
/**
 * Default full-reset sequence. Preserves terminal scrollback so that
 * previously-committed Static rows (via watermark v2) remain visible
 * when the user scrolls up in Terminal.app / tmux.
 *
 * Used by Ink internals when recomposing from scratch (resize, full
 * repaint after a static commit, etc.).
 */
export function getClearTerminalSequence() {
    if (process.platform === 'win32') {
        if (isModernWindowsTerminal()) {
            return ERASE_SCREEN + CURSOR_HOME;
        }
        else {
            // Legacy Windows console - can't clear scrollback
            return ERASE_SCREEN + CURSOR_HOME_WINDOWS;
        }
    }
    return ERASE_SCREEN + CURSOR_HOME;
}
/**
 * Nuclear clear — wipes both visible screen AND terminal scrollback.
 * Reserved for user-invoked clears (`/clear` slash command). Not used
 * by Ink internals; only exposed for callers that explicitly want to
 * obliterate history.
 */
export function getNuclearClearSequence() {
    if (process.platform === 'win32') {
        if (isModernWindowsTerminal()) {
            return ERASE_SCREEN + ERASE_SCROLLBACK + CURSOR_HOME;
        }
        else {
            // Legacy Windows console - can't clear scrollback
            return ERASE_SCREEN + CURSOR_HOME_WINDOWS;
        }
    }
    return ERASE_SCREEN + ERASE_SCROLLBACK + CURSOR_HOME;
}
/**
 * Clears the terminal screen. On supported terminals, also clears scrollback.
 */
export const clearTerminal = getNuclearClearSequence();
//# sourceMappingURL=clearTerminal.js.map