import{g as e}from"./record-DW-UYlLY.js";import{A as t,E as n,M as r,T as i,j as a,k as o,x as s}from"./app-elements-CWGamLYk.js";import{g as c,h as l,p as u}from"./text-stream-Ddra2dVe.js";import{s as d}from"./app-page-cache-finalizer-BJkDA_IL.js";import{r as f,t as p}from"./navigation-runtime-Wr-qsq-o.js";new TextEncoder,`${l(p)}`;function m(e,t){try{e?.(t)}catch{}}function h(r){let a=new Headers({"Content-Type":r.contentType,Vary:t});if(s(a,{cacheControl:r.cacheControl}),i(a,r.cacheState),c(a,r.isEdgeRuntime),r.linkHeader)if(Array.isArray(r.linkHeader))for(let e of r.linkHeader)a.append(`Link`,e);else a.set(`Link`,r.linkHeader);return r.mountedSlotsHeader&&a.set(e,r.mountedSlotsHeader),n(a,r.middlewareHeaders??null),a}function g(e){return e?.value.value&&e.value.value.kind===`APP_PAGE`?e.value.value:null}function _(e){return e.renderObservation!==void 0&&u(e.renderObservation,[`searchParams`])}function v(e){let t=e.routeRevalidateSeconds,n=e.renderCacheControl?.revalidate;return n!==void 0&&(t=t>0?Math.min(t,n):n),{expireSeconds:e.renderCacheControl?.expire??e.expireSeconds,revalidateSeconds:t}}function y(e,t){let n=t.middlewareStatus??(e.status||200),{cacheControl:i}=f({cacheState:t.cacheState,kind:`app-page`,revalidateSeconds:t.revalidateSeconds,expireSeconds:t.expireSeconds,cacheControlMeta:t.cacheControl});if(t.isRscRequest){if(!e.rscData)return null;let s=h({cacheControl:i,cacheState:t.cacheState,contentType:o,isEdgeRuntime:t.isEdgeRuntime,middlewareHeaders:t.middlewareHeaders,mountedSlotsHeader:t.mountedSlotsHeader});return a(s),r(s),new Response(e.rscData,{status:n,headers:s})}if(typeof e.html!=`string`||e.html.length===0)return null;let s=h({cacheControl:i,cacheState:t.cacheState,contentType:`text/html; charset=utf-8`,isEdgeRuntime:t.isEdgeRuntime,linkHeader:e.headers?.link,middlewareHeaders:t.middlewareHeaders});return new Response(e.html,{status:n,headers:s})}async function b(e){if(e.isRscRequest&&e.mountedSlotsHeader)return e.isrDebug?.(`MISS (mounted slots RSC variant)`,e.cleanPathname),null;let t=e.isRscRequest?e.isrRscKey(e.cleanPathname,null,e.renderMode,e.interceptionContext):e.isrHtmlKey(e.cleanPathname),n=e.isRscRequest?`rsc`:`html`;try{let r=await e.isrGet(t),i=g(r);if(r&&!i)return m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`non-app-page-entry`}),e.isrDebug?.(`MISS (non app-page cache entry)`,e.cleanPathname),null;if(i&&e.hasRequestSearchParams===!0&&!_(i))return m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`query-variant-unproven`}),e.isrDebug?.(`MISS (query-bearing request lacks cache proof)`,e.cleanPathname),null;if(i&&!r?.isStale){let a=y(i,{cacheState:`HIT`,cacheControl:r?.value.cacheControl,expireSeconds:e.expireSeconds,isEdgeRuntime:e.isEdgeRuntime,isRscRequest:e.isRscRequest,middlewareHeaders:e.middlewareHeaders,middlewareStatus:e.middlewareStatus,mountedSlotsHeader:e.mountedSlotsHeader,revalidateSeconds:e.revalidateSeconds});if(a)return m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`hit`,reason:`served`}),e.isrDebug?.(e.isRscRequest?`HIT (RSC)`:`HIT (HTML)`,e.cleanPathname),e.clearRequestContext(),a;m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`empty-entry`}),e.isrDebug?.(`MISS (empty cached entry)`,e.cleanPathname)}if(r?.isStale&&i){e.scheduleBackgroundRegeneration(t,async()=>{let n=await e.renderFreshPageForCache(),r=v({expireSeconds:e.expireSeconds,renderCacheControl:n.cacheControl,routeRevalidateSeconds:e.revalidateSeconds}),i=[e.isrSet(e.isRscRequest?t:e.isrRscKey(e.cleanPathname,null,e.renderMode,e.interceptionContext),d(``,n.rscData,200,n.rscRenderObservation),r.revalidateSeconds,n.tags,r.expireSeconds)];e.isRscRequest||i.push(e.isrSet(t,d(n.html,void 0,200,n.htmlRenderObservation,n.linkHeader?{link:n.linkHeader}:void 0),r.revalidateSeconds,n.tags,r.expireSeconds)),await Promise.all(i),e.isrDebug?.(`regen complete`,e.cleanPathname)});let a=y(i,{cacheState:`STALE`,cacheControl:r.value.cacheControl,expireSeconds:e.expireSeconds,isEdgeRuntime:e.isEdgeRuntime,isRscRequest:e.isRscRequest,middlewareHeaders:e.middlewareHeaders,middlewareStatus:e.middlewareStatus,mountedSlotsHeader:e.mountedSlotsHeader,revalidateSeconds:e.revalidateSeconds});if(a)return m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`stale`,reason:`served`}),e.isrDebug?.(e.isRscRequest?`STALE (RSC)`:`STALE (HTML)`,e.cleanPathname),e.clearRequestContext(),a;m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`stale-empty-entry`}),e.isrDebug?.(`STALE MISS (empty stale entry)`,e.cleanPathname)}r||(m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`no-entry`}),e.isrDebug?.(`MISS (no cache entry)`,e.cleanPathname))}catch(r){m(e.recordCacheOutcome,{artifact:n,cacheKey:t,outcome:`miss`,reason:`read-error`}),console.error(`[vinext] ISR cache read error:`,r)}return null}export{b as readAppPageCacheResponse};