import { mkdirSync, readFileSync, writeFileSync, existsSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
function defaultCacheDir() {
    return process.env['GUARDIAN_CVE_CACHE_DIR'] || join(homedir(), '.mcp-guardian', 'cve-cache');
}
export function getCveCacheTtlMs() {
    const raw = process.env['GUARDIAN_CVE_CACHE_TTL_MS'] ?? '900000';
    const n = parseInt(raw, 10);
    return Number.isFinite(n) && n > 0 ? n : 900_000;
}
export function readCveDiskCache(key) {
    const path = join(defaultCacheDir(), `${sanitizeKey(key)}.json`);
    if (!existsSync(path))
        return null;
    try {
        const parsed = JSON.parse(readFileSync(path, 'utf-8'));
        if (parsed?.data === undefined || typeof parsed.cachedAt !== 'number')
            return null;
        return parsed;
    }
    catch {
        return null;
    }
}
export function writeCveDiskCache(key, data) {
    const dir = defaultCacheDir();
    mkdirSync(dir, { recursive: true });
    const entry = { data, cachedAt: Date.now() };
    writeFileSync(join(dir, `${sanitizeKey(key)}.json`), JSON.stringify(entry), 'utf-8');
}
export function isCveCacheFresh(entry, ttlMs = getCveCacheTtlMs()) {
    return Date.now() - entry.cachedAt < ttlMs;
}
function sanitizeKey(key) {
    return key.replace(/[^a-zA-Z0-9@._-]/g, '_').slice(0, 200);
}
//# sourceMappingURL=cve-api-disk-cache.js.map