"use strict";
/**
 * OpenCode CLI Detection
 *
 * Detects and manages OpenCode CLI installation status.
 *
 * @module utils/websearch/opencode-cli
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearOpenCodeCliCache = exports.hasOpenCodeCli = exports.getOpenCodeCliStatus = void 0;
const child_process_1 = require("child_process");
// Cache for OpenCode CLI status (per process)
let opencodeCliCache = null;
/**
 * Check if OpenCode CLI is installed globally
 *
 * OpenCode provides built-in web search via opencode/grok-code model.
 * Install: curl -fsSL https://opencode.ai/install | bash
 *
 * @returns OpenCode CLI status with path and version
 */
function getOpenCodeCliStatus() {
    // Return cached result if available
    if (opencodeCliCache) {
        return opencodeCliCache;
    }
    const result = {
        installed: false,
        path: undefined,
        version: undefined,
    };
    try {
        const isWindows = process.platform === 'win32';
        const whichCmd = isWindows ? 'where opencode' : 'which opencode';
        const pathResult = (0, child_process_1.execSync)(whichCmd, {
            encoding: 'utf8',
            timeout: 5000,
            stdio: ['pipe', 'pipe', 'pipe'],
        });
        const opencodePath = pathResult.trim().split('\n')[0]; // First result on Windows
        if (opencodePath) {
            result.installed = true;
            result.path = opencodePath;
            // Try to get version
            try {
                const versionResult = (0, child_process_1.execSync)('opencode --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 - OpenCode CLI not installed
    }
    // Cache result
    opencodeCliCache = result;
    return result;
}
exports.getOpenCodeCliStatus = getOpenCodeCliStatus;
/**
 * Check if OpenCode CLI is available (quick boolean check)
 */
function hasOpenCodeCli() {
    return getOpenCodeCliStatus().installed;
}
exports.hasOpenCodeCli = hasOpenCodeCli;
/**
 * Clear OpenCode CLI cache (for testing or after installation)
 */
function clearOpenCodeCliCache() {
    opencodeCliCache = null;
}
exports.clearOpenCodeCliCache = clearOpenCodeCliCache;
//# sourceMappingURL=opencode-cli.js.map