"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useRef } from "react";
import { FetchCache } from "./fetch-cache";
// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------
/**
 * @internal Exported for test-level injection — not part of the public API.
 */
export const FetchCacheContext = createContext(null);
// ---------------------------------------------------------------------------
// Provider
// ---------------------------------------------------------------------------
/**
 * Provides a `FetchCache` instance to descendant `useFetch` hooks.
 *
 * Mount this component **above** any remount boundary (e.g. above a
 * `key`-driven session switch) so that cached data survives when child
 * components unmount and remount. Without this provider, `useFetch`
 * works exactly as before — no cache, no behavior change.
 *
 * @example
 * ```tsx
 * // In your app shell (above the session key boundary):
 * <FetchCacheProvider>
 *   <SessionPageInner key={activeSessionId} id={activeSessionId} />
 * </FetchCacheProvider>
 * ```
 *
 * @example
 * ```tsx
 * // With custom limits:
 * <FetchCacheProvider maxEntries={50} ttl={120_000}>
 *   <App />
 * </FetchCacheProvider>
 * ```
 */
export function FetchCacheProvider({ children, maxEntries, ttl, }) {
    const cacheRef = useRef(null);
    if (!cacheRef.current) {
        cacheRef.current = new FetchCache({ maxEntries, ttl });
    }
    return (_jsx(FetchCacheContext.Provider, { value: cacheRef.current, children: children }));
}
// ---------------------------------------------------------------------------
// Hook
// ---------------------------------------------------------------------------
/**
 * Access the nearest `FetchCache` from context.
 *
 * Returns `null` when no `FetchCacheProvider` is mounted — callers
 * must handle the null case gracefully (skip caching).
 *
 * @internal Consumed by `useFetch`; not intended for direct use by
 * SDK consumers.
 */
export function useFetchCache() {
    return useContext(FetchCacheContext);
}
//# sourceMappingURL=FetchCacheProvider.js.map