//#region src/tui/tui-stdio-filter.ts
/**
* While pi-tui owns the screen, strip whole-line pino JSON from stdout/stderr.
* ANSI chunks must pass through immediately (see runTui).
*
* Longer-term (openclaw-style): prefer routing logger transports away from stdout when the TUI
* is active (e.g. file-only + explicit subsystem tags) instead of patching stdio here.
*/
function isLikelyPinoJsonLogLine(line) {
	const t = line.trim();
	if (!t.startsWith("{")) return false;
	try {
		return typeof JSON.parse(t).level === "number";
	} catch {
		return false;
	}
}
/** Patch process stdout/stderr; call `restore()` before exiting the TUI. */
function installTuiStdioFilter() {
	const originalStdoutWrite = process.stdout.write.bind(process.stdout);
	const originalStderrWrite = process.stderr.write.bind(process.stderr);
	let suppressLogs = true;
	const stdoutBuf = { s: "" };
	const stderrBuf = { s: "" };
	const takeEmitCompleteLines = (buf) => {
		let emit = "";
		while (true) {
			const idx = buf.s.indexOf("\n");
			if (idx === -1) break;
			const line = buf.s.slice(0, idx);
			buf.s = buf.s.slice(idx + 1);
			if (!isLikelyPinoJsonLogLine(line)) emit += `${line}\n`;
		}
		return emit;
	};
	const installBufferedLogFilter = (original, buf) => function filteredWrite(chunk, ...rest) {
		if (!suppressLogs) {
			const extra = typeof chunk === "string" ? chunk : chunk instanceof Buffer ? chunk.toString() : "";
			const combined = buf.s ? buf.s + extra : extra;
			buf.s = "";
			return combined.length > 0 ? original(combined, ...rest) : true;
		}
		const text = typeof chunk === "string" ? chunk : chunk instanceof Buffer ? chunk.toString() : "";
		if (text.includes("\x1B")) {
			let emit = takeEmitCompleteLines(buf);
			if (buf.s.length > 0) {
				if (!buf.s.trimStart().startsWith("{")) {
					emit += buf.s;
					buf.s = "";
				}
			}
			if (emit.length > 0) original(emit, ...rest);
			return original(text, ...rest);
		}
		buf.s += text;
		const emit = takeEmitCompleteLines(buf);
		return emit.length > 0 ? original(emit, ...rest) : true;
	};
	const patch = () => {
		process.stdout.write = installBufferedLogFilter(originalStdoutWrite, stdoutBuf);
		process.stderr.write = installBufferedLogFilter(originalStderrWrite, stderrBuf);
	};
	const flushRemainder = (buf, orig) => {
		const rest = buf.s.trimEnd();
		buf.s = "";
		if (!rest.length) return;
		if (!isLikelyPinoJsonLogLine(rest)) orig(`${rest}\n`);
	};
	const unpatch = () => {
		process.stdout.write = originalStdoutWrite;
		process.stderr.write = originalStderrWrite;
	};
	patch();
	return {
		restore: () => {
			suppressLogs = false;
			flushRemainder(stdoutBuf, originalStdoutWrite);
			flushRemainder(stderrBuf, originalStderrWrite);
			unpatch();
		},
		pause: () => {
			suppressLogs = false;
			flushRemainder(stdoutBuf, originalStdoutWrite);
			flushRemainder(stderrBuf, originalStderrWrite);
			unpatch();
		},
		resume: () => {
			suppressLogs = true;
			patch();
		}
	};
}
//#endregion
export { installTuiStdioFilter, isLikelyPinoJsonLogLine };

//# sourceMappingURL=tui-stdio-filter.js.map