"use strict";
/**
 * Grok CLI Detection
 *
 * Detects and manages Grok CLI installation status.
 *
 * @module utils/websearch/grok-cli
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearGrokCliCache = exports.hasGrokCli = exports.getGrokCliStatus = void 0;
const child_process_1 = require("child_process");
// Cache for Grok CLI status (per process)
let grokCliCache = null;
/**
 * Check if Grok CLI is installed globally
 *
 * Grok CLI (grok-4-cli by lalomorales22) provides web search + X search.
 * Requires: `npm install -g grok-cli` and XAI_API_KEY env var.
 *
 * @returns Grok CLI status with path and version
 */
function getGrokCliStatus() {
    // Return cached result if available
    if (grokCliCache) {
        return grokCliCache;
    }
    const result = {
        installed: false,
        path: undefined,
        version: undefined,
    };
    try {
        const isWindows = process.platform === 'win32';
        const whichCmd = isWindows ? 'where grok' : 'which grok';
        const pathResult = (0, child_process_1.execSync)(whichCmd, {
            encoding: 'utf8',
            timeout: 5000,
            stdio: ['pipe', 'pipe', 'pipe'],
        });
        const grokPath = pathResult.trim().split('\n')[0]; // First result on Windows
        if (grokPath) {
            result.installed = true;
            result.path = grokPath;
            // Try to get version
            try {
                const versionResult = (0, child_process_1.execSync)('grok --version', {
                    encoding: 'utf8',
                    timeout: 5000,
                    stdio: ['pipe', 'pipe', 'pipe'],
                });
                result.version = versionResult.trim();
            }
            catch {
                // Version check failed, but CLI is installed
                result.version = 'unknown';
            }
        }
    }
    catch {
        // Command not found - Grok CLI not installed
    }
    // Cache result
    grokCliCache = result;
    return result;
}
exports.getGrokCliStatus = getGrokCliStatus;
/**
 * Check if Grok CLI is available (quick boolean check)
 */
function hasGrokCli() {
    return getGrokCliStatus().installed;
}
exports.hasGrokCli = hasGrokCli;
/**
 * Clear Grok CLI cache (for testing or after installation)
 */
function clearGrokCliCache() {
    grokCliCache = null;
}
exports.clearGrokCliCache = clearGrokCliCache;
//# sourceMappingURL=grok-cli.js.map