#!/usr/bin/env node
import "./logger-BHK-IvoX.js";
import { t as canUseInteractiveUI } from "./interactiveCheck-D3Z7EJq4.js";

//#region src/ui/render.ts
/**
* Process exit codes for signal handling.
*/
const EXIT_CODES = {
	SIGINT: 130,
	SIGTERM: 143
};
let inkModule = null;
async function getInk() {
	if (!inkModule) inkModule = await import("ink");
	return inkModule;
}
/**
* Render an Ink component in the terminal.
*
* This is the main entry point for rendering interactive UI components.
* It handles TTY detection, cleanup, and error handling.
*
* @param element - The React element to render
* @param options - Render options
* @returns A render result with control methods
*/
async function renderInteractive(element, options = {}) {
	const { exitOnCtrlC = false, patchConsole = true, debug = false, onSignal } = options;
	if (!canUseInteractiveUI()) throw new Error("Cannot render interactive UI: stdout is not a TTY. Interactive UI requires a terminal. Use non-interactive fallback instead.");
	const instance = (await getInk()).render(element, {
		exitOnCtrlC,
		patchConsole,
		debug
	});
	let isCleanedUp = false;
	const handleSigint = () => {
		if (!isCleanedUp) {
			isCleanedUp = true;
			instance.unmount();
		}
		onSignal?.("SIGINT");
		process.exitCode = EXIT_CODES.SIGINT;
		setImmediate(() => {
			process.exit();
		});
	};
	const handleSigterm = () => {
		if (!isCleanedUp) {
			isCleanedUp = true;
			instance.unmount();
		}
		onSignal?.("SIGTERM");
		process.exitCode = EXIT_CODES.SIGTERM;
		setImmediate(() => {
			process.exit();
		});
	};
	process.once("SIGINT", handleSigint);
	process.once("SIGTERM", handleSigterm);
	const cleanup = () => {
		if (isCleanedUp) return;
		isCleanedUp = true;
		process.removeListener("SIGINT", handleSigint);
		process.removeListener("SIGTERM", handleSigterm);
		instance.unmount();
	};
	return {
		instance,
		waitUntilExit: () => instance.waitUntilExit(),
		cleanup,
		rerender: (node) => instance.rerender(node),
		clear: () => instance.clear(),
		unmount: () => instance.unmount()
	};
}
/**
* Higher-order function to run an Ink component and wait for it to exit.
*
* This is a convenience wrapper that handles the full lifecycle:
* 1. Renders the component
* 2. Waits for it to unmount
* 3. Cleans up
*
* @param Component - The React component to render
* @param props - Props to pass to the component
* @param options - Render options
*/
async function runInteractive(Component, props, options = {}) {
	const { createElement } = await import("react");
	const { waitUntilExit, cleanup } = await renderInteractive(createElement(Component, props), options);
	try {
		await waitUntilExit();
	} finally {
		cleanup();
	}
}
/**
* Get terminal dimensions with sensible defaults.
*/
function getTerminalSize() {
	return {
		columns: process.stdout.columns || 80,
		rows: process.stdout.rows || 24
	};
}
/**
* Check if the terminal supports colors.
*/
function supportsColor() {
	if (process.env.NO_COLOR) return false;
	if (process.env.FORCE_COLOR) return true;
	return process.stdout.isTTY && process.stdout.hasColors?.() !== false;
}

//#endregion
export { canUseInteractiveUI, getTerminalSize, renderInteractive, runInteractive, supportsColor };
//# sourceMappingURL=render-eNOwFYnQ.js.map