/**
 * In-Memory Segment Cache Store
 *
 * Simple in-memory implementation of SegmentCacheStore.
 * Uses globalThis to survive HMR in development.
 */
const CACHE_REGISTRY_KEY = "__rsc_router_segment_cache_registry__";
/**
 * Returns the globalThis-backed registry of named cache Maps.
 * The registry itself survives HMR; individual stores are keyed by name.
 */
function getGlobalRegistry() {
    let registry = globalThis[CACHE_REGISTRY_KEY];
    if (!registry) {
        registry = new Map();
        globalThis[CACHE_REGISTRY_KEY] = registry;
    }
    return registry;
}
/**
 * In-memory segment cache store.
 *
 * Suitable for development and single-instance deployments.
 * For production with multiple instances, use a distributed store
 * like Cloudflare KV or Redis.
 *
 * @example
 * ```typescript
 * // Basic usage
 * const store = new MemorySegmentCacheStore();
 *
 * // With defaults for cache() boundaries
 * const store = new MemorySegmentCacheStore({
 *   defaults: { ttl: 60 }
 * });
 *
 * createRSCHandler({
 *   router,
 *   cache: { store }
 * })
 * ```
 */
export class MemorySegmentCacheStore {
    constructor(options) {
        if (options?.name != null) {
            // Named stores use the globalThis registry so data survives HMR.
            // Each name gets its own isolated Map.
            const registry = getGlobalRegistry();
            let map = registry.get(options.name);
            if (!map) {
                map = new Map();
                registry.set(options.name, map);
            }
            this.cache = map;
        }
        else {
            // Unnamed stores get a plain instance-level Map (no globalThis sharing).
            this.cache = new Map();
        }
        this.defaults = options?.defaults;
        this.keyGenerator = options?.keyGenerator;
    }
    async get(key) {
        const cached = this.cache.get(key);
        if (!cached) {
            return null;
        }
        // Check expiration
        if (Date.now() > cached.expiresAt) {
            this.cache.delete(key);
            return null;
        }
        // Memory store doesn't support SWR - never triggers revalidation
        return { data: cached, shouldRevalidate: false };
    }
    async set(key, data, ttl, _swr) {
        // Note: Memory store doesn't implement SWR - entries just expire at TTL
        // For SWR support, use CFCacheStore or similar distributed cache
        const entry = {
            ...data,
            expiresAt: Date.now() + ttl * 1000,
        };
        this.cache.set(key, entry);
    }
    async delete(key) {
        return this.cache.delete(key);
    }
    async clear() {
        this.cache.clear();
    }
    /**
     * Get cache statistics for debugging purposes.
     * @internal
     */
    getStats() {
        return {
            size: this.cache.size,
            keys: Array.from(this.cache.keys()),
        };
    }
    /**
     * Reset the global cache registry.
     * Useful for test isolation - call this in beforeEach to ensure
     * tests don't share cache state via globalThis.
     *
     * @example
     * ```typescript
     * beforeEach(() => {
     *   MemorySegmentCacheStore.resetGlobalCache();
     * });
     * ```
     */
    static resetGlobalCache() {
        delete globalThis[CACHE_REGISTRY_KEY];
    }
}
//# sourceMappingURL=memory-segment-store.js.map