/**
 * OMK Theme — ANSI escape utilities & text sanitization
 * Extracted from util/theme.ts to break God Module coupling
 */
export function isColorEnabled(env = process.env, stream = process.stdout) {
    if (env.NO_COLOR !== undefined || env.TERM === "dumb")
        return false;
    if (env.FORCE_COLOR === "1" || env.FORCE_COLOR === "true")
        return true;
    return Boolean(stream.isTTY);
}
/** Backward-compatible snapshot; new ANSI rendering paths call isColorEnabled() dynamically. */
export const colorEnabled = isColorEnabled();
function isSafeAnsiCode(codes) {
    return /^[0-9;]{1,48}$/.test(codes);
}
export const esc = (codes) => isColorEnabled() && isSafeAnsiCode(codes) ? `\x1b[${codes}m` : "";
export const rgb = (r, g, b) => `38;2;${r};${g};${b}`;
export const bgRgb = (r, g, b) => `48;2;${r};${g};${b}`;
export function stripBrokenAnsi(value) {
    return value
        .replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "")
        .replace(/\[[0-9;]{1,64}m/g, "")
        .replace(/\[0m/g, "");
}
export function sanitizeTerminalText(value) {
    return stripBrokenAnsi(value
        .replace(/\x1B\][\s\S]*?(?:\x07|\x1B\\)/g, "")
        .replace(/\x1B[P^_][\s\S]*?\x1B\\/g, ""))
        .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, "")
        .replace(/^::code-comment\{.*?\}[ \t]*\r?\n?/gm, "");
}
export function visibleTerminalWidth(text) {
    return sanitizeTerminalText(text).length;
}
export function stripAnsi(str) {
    return sanitizeTerminalText(str);
}
export function padEndAnsi(str, len) {
    return str + " ".repeat(Math.max(0, len - stripAnsi(str).length));
}
