"use strict";
/**
 * UI Initialization
 *
 * Handles lazy loading of ESM dependencies
 * @module utils/ui/init
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isClaudeCodeContext = exports.isInteractive = exports.useColors = exports.initUI = void 0;
const types_1 = require("./types");
/**
 * Native dynamic import that bypasses TypeScript's require() transformation.
 * This preserves ESM import() at runtime, fixing Node 24 ESM/CJS interop issues.
 *
 * TypeScript compiles `import('x')` to `require('x')` when targeting CommonJS,
 * which breaks ESM packages like ora that depend on other ESM-only modules.
 */
const dynamicImport = new Function('specifier', 'return import(specifier)');
/**
 * Initialize UI dependencies (call once at startup)
 * Uses native dynamic imports for ESM packages in CommonJS project
 */
async function initUI() {
    if (types_1.initialized)
        return;
    try {
        // Use native dynamic import to avoid TypeScript's require() transformation
        const [chalkImport, boxenImport, gradientImport, oraImport, listrImport] = await Promise.all([
            dynamicImport('chalk'),
            dynamicImport('boxen'),
            dynamicImport('gradient-string'),
            dynamicImport('ora'),
            dynamicImport('listr2'),
        ]);
        // ESM modules: use .default for the actual export
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const getDefault = (mod) => mod.default || mod;
        types_1.moduleCache.chalk = getDefault(chalkImport);
        types_1.moduleCache.boxen = getDefault(boxenImport);
        types_1.moduleCache.gradient = getDefault(gradientImport);
        types_1.moduleCache.ora = getDefault(oraImport);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const listr = listrImport;
        types_1.moduleCache.listr = listr.Listr || listr.default?.Listr;
        (0, types_1.setInitialized)(true);
    }
    catch (_e) {
        // Fallback: UI works without colors if imports fail
        console.error('[!] UI initialization failed, using plain text mode');
        (0, types_1.setInitialized)(true);
    }
}
exports.initUI = initUI;
/**
 * Check if colors should be used
 * Respects NO_COLOR and FORCE_COLOR environment variables
 */
function useColors() {
    // FORCE_COLOR overrides all checks
    if (process.env.FORCE_COLOR)
        return true;
    // NO_COLOR disables colors
    if (process.env.NO_COLOR)
        return false;
    // Otherwise, check if TTY
    return !!process.stdout.isTTY;
}
exports.useColors = useColors;
/**
 * Check if interactive mode (TTY + not CI)
 */
function isInteractive() {
    return !!process.stdout.isTTY && !process.env.CI && !process.env.NO_COLOR;
}
exports.isInteractive = isInteractive;
/**
 * Detect if running inside Claude Code tool context
 *
 * Heuristics:
 * - No TTY (stdout captured)
 * - CI-like environment
 * - CLAUDE_CODE env var set
 */
function isClaudeCodeContext() {
    return (!process.stdout.isTTY ||
        !!process.env.CI ||
        !!process.env.CLAUDE_CODE ||
        process.env.TERM === 'dumb');
}
exports.isClaudeCodeContext = isClaudeCodeContext;
//# sourceMappingURL=init.js.map