"use strict";
// src/orchestrator/router/ProviderProbe.ts
// v1.14.0 — Detect which CLIs are installed locally. Non-blocking,
// cached. Never throws — returns "false" on any error.
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProviderProbe = void 0;
const child_process = __importStar(require("child_process"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const ProviderCapabilityCatalog_1 = require("./ProviderCapabilityCatalog");
const PROBE_TIMEOUT_MS = 1500;
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
function cachePath() {
    const home = process.env.HOME || process.env.USERPROFILE || '/tmp';
    return path.join(home, '.openlife', 'provider-probe.json');
}
function readCache() {
    const p = cachePath();
    if (!fs.existsSync(p))
        return null;
    try {
        const j = JSON.parse(fs.readFileSync(p, 'utf-8'));
        if (Date.now() - j.ts > CACHE_TTL_MS)
            return null;
        return j.entries;
    }
    catch {
        return null;
    }
}
function writeCache(entries) {
    const p = cachePath();
    try {
        fs.mkdirSync(path.dirname(p), { recursive: true });
        fs.writeFileSync(p, JSON.stringify({ ts: Date.now(), entries }, null, 2));
    }
    catch { /* best effort */ }
}
function probeCli(binary) {
    try {
        const r = child_process.spawnSync(binary, ['--version'], {
            timeout: PROBE_TIMEOUT_MS,
            stdio: ['ignore', 'ignore', 'ignore'],
        });
        return r.status === 0;
    }
    catch {
        return false;
    }
}
function probeApi(provider) {
    switch (provider) {
        case 'openai-cli':
        case 'openai-api': return !!process.env.OPENAI_API_KEY;
        case 'gemini-cli':
        case 'gemini-api': return !!process.env.GEMINI_API_KEY;
        case 'anthropic': return !!process.env.ANTHROPIC_API_KEY;
        case 'ollama': return !!process.env.OLLAMA_URL || true; // ollama defaults to localhost
        default: return false;
    }
}
class ProviderProbe {
    static run(opts) {
        if (!opts?.force) {
            const cached = readCache();
            if (cached)
                return cached;
        }
        const out = ProviderCapabilityCatalog_1.DEFAULT_CATALOG.map(entry => {
            const cliAvailable = entry.cliBinary ? probeCli(entry.cliBinary) : false;
            const apiAvailable = probeApi(entry.provider);
            return { ...entry, cliAvailable, apiAvailable };
        });
        writeCache(out);
        return out;
    }
}
exports.ProviderProbe = ProviderProbe;
