// engine/telemetry/geo-resolver.ts — Cached geo-IP lookup for telemetry enrichment.
// Uses ip-api.com (free, no API key, 45 req/min). Called once per process lifecycle.
// Never throws — geo enrichment is purely best-effort.
let cachedGeo = null;
let lookupAttempted = false;
/**
 * Returns geo data for the current machine IP.
 * Cached after the first successful call; null if the lookup fails or times out.
 */
export async function resolveGeo() {
    if (lookupAttempted) {
        return cachedGeo;
    }
    lookupAttempted = true;
    try {
        const res = await fetch('http://ip-api.com/json/?fields=country,countryCode,city,timezone', {
            signal: AbortSignal.timeout(2_000),
        });
        if (!res.ok) {
            return null;
        }
        const data = (await res.json());
        if (typeof data.countryCode !== 'string') {
            return null;
        }
        cachedGeo = {
            country: typeof data.country === 'string' ? data.country : '',
            countryCode: data.countryCode,
            city: typeof data.city === 'string' ? data.city : '',
            timezone: typeof data.timezone === 'string' ? data.timezone : '',
        };
        return cachedGeo;
    }
    catch {
        return null;
    }
}
/** Reset cache — used in tests. */
export function resetGeoCache() {
    cachedGeo = null;
    lookupAttempted = false;
}
//# sourceMappingURL=geo-resolver.js.map