/**
 * Stderr interception for interactive TUI mode.
 *
 * When the TUI is active, raw writes to process.stderr would corrupt the
 * differential renderer's state. This module intercepts process.stderr.write
 * and routes all output through a callback instead of letting it hit the terminal.
 *
 * Analogous to output-guard.ts (which handles stdout for non-interactive modes),
 * but specifically for protecting the TUI's display in interactive mode.
 */
let stderrTakeoverState;
/**
 * Intercept all process.stderr.write calls and route them through the callback.
 * Idempotent — multiple calls are no-ops if already taken over.
 */
export function takeOverStderr(callback) {
    if (stderrTakeoverState) {
        return;
    }
    const rawStderrWrite = process.stderr.write.bind(process.stderr);
    const originalStderrWrite = process.stderr.write;
    process.stderr.write = ((chunk, encodingOrCallback, callback) => {
        const text = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString();
        if (text.length > 0) {
            try {
                stderrTakeoverState?.callback(text);
            }
            catch {
                rawStderrWrite(text);
            }
        }
        // Signal success to caller — call the callback if provided
        const cb = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
        if (cb)
            cb(null);
        return true;
    });
    stderrTakeoverState = {
        rawStderrWrite,
        originalStderrWrite,
        callback,
    };
}
/**
 * Write a message directly through the interceptor callback with level info.
 * Used by the logger to pass severity through without going via process.stderr.write.
 * Falls back to rawStderrWrite if stderr is not taken over.
 */
export function writeIntercepted(message, level) {
    if (stderrTakeoverState) {
        try {
            stderrTakeoverState.callback(message, level);
        }
        catch {
            stderrTakeoverState.rawStderrWrite(message);
        }
    }
    else {
        process.stderr.write(`${message}\n`);
    }
}
/**
 * Restore the original process.stderr.write behavior.
 */
export function restoreStderr() {
    if (!stderrTakeoverState) {
        return;
    }
    process.stderr.write = stderrTakeoverState.originalStderrWrite;
    stderrTakeoverState = undefined;
}
/**
 * Check whether stderr is currently intercepted.
 */
export function isStderrTakenOver() {
    return stderrTakeoverState !== undefined;
}
/**
 * Write directly to the real stderr, bypassing interception.
 * Use for intentional writes that must reach the terminal
 * (e.g., fatal errors before exit, post-TUI teardown messages).
 */
export function writeRawStderr(text) {
    if (stderrTakeoverState) {
        stderrTakeoverState.rawStderrWrite(text);
        return;
    }
    process.stderr.write(text);
}
//# sourceMappingURL=stderr-guard.js.map