import { CacheState } from "./cache-request-state.js";
import { RootParamsState } from "./root-params.js";
import { ExecutionContextLike } from "./request-context.js";
import { FetchCacheState } from "./fetch-cache.js";
import { VinextHeadersShimState } from "./headers.js";
import { RouterState } from "./router-state.js";
import { PrivateCacheState } from "./cache-runtime.js";
import { HeadState } from "./head-state.js";
import { I18nState } from "./i18n-state.js";
import { NavigationState } from "./navigation-state.js";
import "./request-state-types.js";
//#region src/shims/unified-request-context.d.ts
/**
 * Flat union of all per-request state previously spread across
 * VinextHeadersShimState, NavigationState, CacheState, PrivateCacheState,
 * FetchCacheState, and ExecutionContextLike.
 *
 * Each field group is documented with its source shim module.
 */
type UnifiedRequestContext = {
  /** Cloudflare Workers ExecutionContext, or null on Node.js dev. */
  executionContext: ExecutionContextLike | null;
  /** Per-request cache for cacheForRequest(). Keyed by factory function reference. */
  requestCache: WeakMap<(...args: any[]) => any, unknown>;
  /** Shared lifecycle state for work deferred until the response closes. */
  afterContext: AfterRequestContext;
} & VinextHeadersShimState & I18nState & NavigationState & CacheState & PrivateCacheState & FetchCacheState & RouterState & HeadState & RootParamsState;
type AfterRequestContext = {
  callbacks: Array<() => unknown>;
  responseClosed: boolean;
  pendingCallbacks: number;
  pendingPromises: number;
  completion: Promise<void> | null;
  resolveCompletion: (() => void) | null;
};
/**
 * Create a fresh `UnifiedRequestContext` with defaults for all fields.
 * Pass partial overrides for the fields you need to pre-populate.
 */
declare function createRequestContext(opts?: Partial<UnifiedRequestContext>): UnifiedRequestContext;
/** Queue a callback until response close, or start it immediately once closed. */
declare function queueAfterCallback(ctx: UnifiedRequestContext, callback: () => unknown): void;
/** Track promise-form after() work that can register a callback before settling. */
declare function trackAfterPromise<T>(ctx: UnifiedRequestContext, promise: Promise<T>): Promise<T>;
/** Bind a callback to every AsyncLocalStorage context active at registration. */
declare function bindRequestContextSnapshot<T>(ctx: UnifiedRequestContext, callback: () => T): () => T;
/**
 * Release function-form `after()` work once the response body has closed.
 * All queued callbacks start together, matching Next.js' unbounded PromiseQueue.
 */
declare function closeAfterResponse(ctx: UnifiedRequestContext): Promise<void>;
/**
 * Whether this request has function-form `after()` work that still needs to
 * observe the response body closing.
 *
 * Promise-form `after(promise)` is included because its continuation can
 * register a function-form `after()` before the promise settles. Function-form
 * work needs the body's close observed because its contract is "run once the
 * response has been sent". `resolveCompletion` stays non-null for as long as
 * any callback is queued or in flight, so checking it alongside the explicit
 * counters keeps this correct on re-entry after callbacks have started.
 */
declare function requiresResponseCloseTracking(ctx: UnifiedRequestContext): boolean;
/**
 * Mark a response whose body vinext constructed from a fully in-memory string
 * or byte array, as opposed to a body handed back by user code, which could
 * still be producing. With no producer left, no `after()` call can originate
 * from this body — the one signal that makes it safe for
 * `closeAfterResponseWithBody()` to skip close tracking.
 *
 * Not set for a metadata route's `result instanceof Response` passthrough (a
 * user `icon.tsx`/`opengraph-image.tsx` can return a streaming
 * `ImageResponse`) or any handler-returned `new Response(stream)` — those
 * bodies can still be producing and must keep close tracking.
 */
declare function markFullyBufferedBody(response: Response): Response;
/** Preserve the internal buffered-body signal when response metadata is rebuilt. */
declare function preserveFullyBufferedBodyMetadata(source: Response, target: Response): Response;
/**
 * Wrap a response so deferred `after()` callbacks start on stream completion
 * or cancellation. Skipped only when the body is marked fully buffered (see
 * `markFullyBufferedBody`) and nothing is currently registered — that lets
 * the runtime send it with an accurate `Content-Length` instead of chunked
 * transfer encoding.
 */
declare function closeAfterResponseWithBody(response: Response, ctx: UnifiedRequestContext): Response;
/**
 * Run `fn` within a unified request context scope.
 * All shim modules will read/write their state from `ctx` for the
 * duration of the call, including async continuations.
 */
declare function runWithRequestContext<T>(ctx: UnifiedRequestContext, fn: () => Promise<T>): Promise<T>;
declare function runWithRequestContext<T>(ctx: UnifiedRequestContext, fn: () => T | Promise<T>): T | Promise<T>;
/**
 * Run `fn` in a nested unified scope derived from the current request context.
 * Used by legacy runWith* wrappers to reset or override one sub-state while
 * preserving proper async isolation for continuations created inside `fn`.
 * The child scope is a shallow clone of the parent store, so untouched fields
 * keep sharing their existing references while overridden slices can be reset.
 *
 * @internal
 */
declare function runWithUnifiedStateMutation<T>(mutate: (ctx: UnifiedRequestContext) => void, fn: () => Promise<T>): Promise<T>;
declare function runWithUnifiedStateMutation<T>(mutate: (ctx: UnifiedRequestContext) => void, fn: () => T | Promise<T>): T | Promise<T>;
/**
 * Get the current unified request context.
 * Returns the ALS store when inside a `runWithRequestContext()` scope,
 * or a fresh detached context otherwise. Unlike the legacy per-shim fallback
 * singletons, this detached value is ephemeral — mutations do not persist
 * across calls. This is intentional to prevent state leakage outside request
 * scopes.
 *
 * Only direct callers observe this detached fallback. Shim `_getState()`
 * helpers should continue to gate on `isInsideUnifiedScope()` and fall back
 * to their standalone ALS/fallback singletons outside the unified scope.
 * If called inside a standalone `runWithExecutionContext()` scope, the
 * detached context still reflects that inherited `executionContext`.
 */
declare function getRequestContext(): UnifiedRequestContext;
/**
 * Check whether the current execution is inside a `runWithRequestContext()` scope.
 * Shim modules use this to decide whether to read from the unified store
 * or fall back to their own standalone ALS.
 */
declare function isInsideUnifiedScope(): boolean;
//#endregion
export { AfterRequestContext, UnifiedRequestContext, bindRequestContextSnapshot, closeAfterResponse, closeAfterResponseWithBody, createRequestContext, getRequestContext, isInsideUnifiedScope, markFullyBufferedBody, preserveFullyBufferedBodyMetadata, queueAfterCallback, requiresResponseCloseTracking, runWithRequestContext, runWithUnifiedStateMutation, trackAfterPromise };