/**
 * MK2 Stdout Guard - NO_STDOUT enforcement
 * Catches any stdout violations during TUI mode
 */
import fs from 'fs';
let guardActive = false;
const logPath = '/tmp/mk2_stdout_violation.log';
const originalLog = console.log.bind(console);
const originalError = console.error.bind(console);
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
/**
 * Enable stdout guard - redirects all output to violation log
 */
export function enableStdoutGuard() {
    if (guardActive)
        return;
    guardActive = true;
    // Intercept console.log
    console.log = (...args) => {
        const msg = args.map(a => String(a)).join(' ');
        logViolation('console.log', msg);
    };
    // Intercept console.error
    console.error = (...args) => {
        const msg = args.map(a => String(a)).join(' ');
        logViolation('console.error', msg);
    };
    // Intercept stdout
    process.stdout.write = function (chunk, encoding, cb) {
        // Allow blessed output (check stack)
        const stack = new Error().stack || '';
        if (stack.includes('blessed') || stack.includes('neo-blessed') || stack.includes('program.js')) {
            return originalStdoutWrite(chunk, encoding, cb);
        }
        // Allow escape sequences
        if (typeof chunk === 'string' && (chunk.includes('\x1b[') || chunk.includes('\u001b['))) {
            return originalStdoutWrite(chunk, encoding, cb);
        }
        logViolation('stdout.write', String(chunk));
        if (cb)
            cb();
        return true;
    };
    // Intercept stderr (but allow debug messages and blessed output)
    process.stderr.write = function (chunk, encoding, cb) {
        const stack = new Error().stack || '';
        const chunkStr = String(chunk);
        // Allow blessed output
        if (stack.includes('blessed') || stack.includes('neo-blessed')) {
            return originalStderrWrite(chunk, encoding, cb);
        }
        // Allow debug messages (they contain [DEBUG] or [ERROR])
        if (chunkStr.includes('[DEBUG]') || chunkStr.includes('[ERROR]')) {
            return originalStderrWrite(chunk, encoding, cb);
        }
        // Block everything else
        logViolation('stderr.write', chunkStr);
        if (cb)
            cb();
        return true;
    };
}
/**
 * Disable stdout guard
 */
export function disableStdoutGuard() {
    if (!guardActive)
        return;
    guardActive = false;
    console.log = originalLog;
    console.error = originalError;
    process.stdout.write = originalStdoutWrite;
    process.stderr.write = originalStderrWrite;
}
/**
 * Log stdout violation to file
 */
function logViolation(source, message) {
    try {
        const timestamp = new Date().toISOString();
        const stack = new Error().stack || '';
        const entry = `[${timestamp}] ${source}: ${message}\nStack: ${stack}\n\n`;
        fs.appendFileSync(logPath, entry);
    }
    catch {
        // Ignore errors writing to log
    }
}
/**
 * Get violation log path
 */
export function getViolationLogPath() {
    return logPath;
}
//# sourceMappingURL=stdoutGuard.js.map