"use strict";
/**
 * Gemini CLI Detection
 *
 * Detects and manages Gemini CLI installation status.
 *
 * @module utils/websearch/gemini-cli
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearGeminiCliCache = exports.isGeminiAuthenticated = exports.hasGeminiCli = exports.getGeminiCliStatus = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const child_process_1 = require("child_process");
// Cache for Gemini CLI status (per process)
let geminiCliCache = null;
/**
 * Check if Gemini CLI is installed globally
 *
 * Requires global install: `npm install -g @google/gemini-cli`
 * No npx fallback - must be in PATH
 *
 * @returns Gemini CLI status with path and version
 */
function getGeminiCliStatus() {
    // Return cached result if available
    if (geminiCliCache) {
        return geminiCliCache;
    }
    const result = {
        installed: false,
        path: undefined,
        version: undefined,
    };
    try {
        const isWindows = process.platform === 'win32';
        const whichCmd = isWindows ? 'where gemini' : 'which gemini';
        const pathResult = (0, child_process_1.execSync)(whichCmd, {
            encoding: 'utf8',
            timeout: 5000,
            stdio: ['pipe', 'pipe', 'pipe'],
        });
        const geminiPath = pathResult.trim().split('\n')[0]; // First result on Windows
        if (geminiPath) {
            result.installed = true;
            result.path = geminiPath;
            // Try to get version
            try {
                const versionResult = (0, child_process_1.execSync)('gemini --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 - Gemini CLI not installed
    }
    // Cache result
    geminiCliCache = result;
    return result;
}
exports.getGeminiCliStatus = getGeminiCliStatus;
/**
 * Check if Gemini CLI is available (quick boolean check)
 */
function hasGeminiCli() {
    return getGeminiCliStatus().installed;
}
exports.hasGeminiCli = hasGeminiCli;
/**
 * Check if Gemini CLI is authenticated
 *
 * Gemini CLI stores OAuth credentials in ~/.gemini/oauth_creds.json
 * Authentication is done by running `gemini` which opens browser for OAuth.
 * Note: There is NO `gemini auth login` command - just run `gemini` to authenticate.
 *
 * @returns true if oauth_creds.json exists with access_token
 */
function isGeminiAuthenticated() {
    const oauthPath = path.join(os.homedir(), '.gemini', 'oauth_creds.json');
    if (!fs.existsSync(oauthPath)) {
        return false;
    }
    try {
        const content = fs.readFileSync(oauthPath, 'utf8');
        const creds = JSON.parse(content);
        // Check if access_token exists (doesn't validate expiry - Gemini CLI handles refresh)
        return !!creds.access_token;
    }
    catch {
        return false;
    }
}
exports.isGeminiAuthenticated = isGeminiAuthenticated;
/**
 * Clear Gemini CLI cache (for testing or after installation)
 */
function clearGeminiCliCache() {
    geminiCliCache = null;
}
exports.clearGeminiCliCache = clearGeminiCliCache;
//# sourceMappingURL=gemini-cli.js.map