// engine/auto-updater/npm-registry.ts — Queries the npm registry for the latest @planu/cli version.
// SPEC-443
// Endpoint: https://registry.npmjs.org/-/package/@planu/cli/dist-tags
// Timeout: 3 seconds. Fallback: null (never block the user).
const NPM_REGISTRY_URL = 'https://registry.npmjs.org/-/package/@planu/cli/dist-tags';
const TIMEOUT_MS = 3_000;
/** In-memory cache for the current process lifetime. */
let cachedVersion = undefined;
/**
 * Fetches the latest version of @planu/cli from the npm registry.
 * Uses in-memory cache to avoid redundant network calls within the same session.
 * Returns null on timeout or any network error.
 */
export async function fetchLatestVersion() {
    if (cachedVersion !== undefined) {
        return cachedVersion;
    }
    const controller = new AbortController();
    const timer = setTimeout(() => {
        controller.abort();
    }, TIMEOUT_MS);
    try {
        const res = await fetch(NPM_REGISTRY_URL, { signal: controller.signal });
        const tags = (await res.json());
        const version = typeof tags.latest === 'string' ? tags.latest : null;
        cachedVersion = version;
        return version;
    }
    catch {
        cachedVersion = null;
        return null;
    }
    finally {
        clearTimeout(timer);
    }
}
/**
 * Clears the in-memory version cache.
 * Exposed for testing purposes only.
 */
export function clearVersionCache() {
    cachedVersion = undefined;
}
//# sourceMappingURL=npm-registry.js.map