import { homedir } from "node:os";
import { join } from "node:path";
let cachedConfig = null;
export function loadConfig() {
    if (cachedConfig)
        return cachedConfig;
    const dataDir = process.env["CODESIFT_DATA_DIR"] ?? join(homedir(), ".codesift");
    const voyageApiKey = process.env["CODESIFT_VOYAGE_API_KEY"] ?? null;
    const openaiApiKey = process.env["CODESIFT_OPENAI_API_KEY"] ?? null;
    const ollamaUrl = process.env["CODESIFT_OLLAMA_URL"] ?? null;
    const localModel = process.env["CODESIFT_LOCAL_MODEL"] ?? null;
    const localDisabled = process.env["CODESIFT_DISABLE_LOCAL_EMBEDDINGS"] === "true";
    const explicitProvider = process.env["CODESIFT_EMBEDDING_PROVIDER"] ?? null;
    let embeddingProvider = null;
    if (explicitProvider === "voyage" || explicitProvider === "openai" || explicitProvider === "ollama" || explicitProvider === "local") {
        embeddingProvider = explicitProvider;
    }
    else if (voyageApiKey)
        embeddingProvider = "voyage";
    else if (openaiApiKey)
        embeddingProvider = "openai";
    else if (ollamaUrl)
        embeddingProvider = "ollama";
    else if (!localDisabled)
        embeddingProvider = "local";
    cachedConfig = {
        dataDir,
        registryPath: join(dataDir, "registry.json"),
        watchDebounceMs: parseIntEnv("CODESIFT_WATCH_DEBOUNCE_MS", 500),
        bm25FieldWeights: {
            name: 5.0,
            signature: 2.5,
            docstring: 1.5,
            body: 0.5,
            comments: 0.2,
        },
        embeddingProvider,
        voyageApiKey,
        openaiApiKey,
        ollamaUrl,
        localModel,
        embeddingBatchSize: parseIntEnv("CODESIFT_EMBEDDING_BATCH_SIZE", 128),
        defaultTokenBudget: parseIntEnv("CODESIFT_DEFAULT_TOKEN_BUDGET", 8000),
        defaultTopK: parseIntEnv("CODESIFT_DEFAULT_TOP_K", 50),
        secretScanEnabled: process.env["CODESIFT_SECRET_SCAN"] !== "false",
    };
    return cachedConfig;
}
/** Reset cached config — for testing only. */
export function resetConfigCache() {
    cachedConfig = null;
}
function parseIntEnv(name, defaultValue) {
    const raw = process.env[name];
    if (!raw)
        return defaultValue;
    const parsed = parseInt(raw, 10);
    return isNaN(parsed) ? defaultValue : parsed;
}
//# sourceMappingURL=config.js.map