import{DEV_LOGIN_SLUG as w,ServerRoutes as y}from"../../../constants/index.js";import{CACHE_CONTROL_NO_CACHE_HEADER_VALUE as r,DEFAULT_TITLE as N}from"../../constants/common.js";import{canAccessResource as O,filterDataByAccessDeep as S}from"../../utils/rbac.js";import{getServerProps as U}from"../../ssr/index.js";import{findInIterable as $}from"../../../shared/jsUtils.js";import{readSharedData as F}from"../../utils/index.js";import{isDevelopMode as q}from"../../utils/is-develop-mode.js";import{getRedirectLoginUrl as P}from"../utils/get-redirect-login-url.js";import{removeTrailingSlash as B}from"../../../shared/utils.js";import{processRedirects as H}from"../../../shared/urls.js";import{removeErrorDetails as V}from"../utils/remove-error-details.js";import{otelTelemetry as G}from"../../telemetry/telemetry.js";function te(e,a){return async(t,m)=>{const l=t.get("logger"),{req:u}=t,{pathname:d}=new URL(u.url),{seo:f,ssoDirect:h}=e.getConfig(),i=f?.title||N;if(e?.compilationErrors?.length&&q())return t.json({templateId:"compilation-error",props:{compilationErrors:e?.compilationErrors},sharedDataIds:{}},500,{"Cache-Control":r});const p=d.match(/page-data(.*)data.json$/);if(!p)return m();const c=decodeURI(p[1]),n=c==="/index/"?"/":B(c),o=e.getRouteBySlug(n,{followRedirect:!1})||$(e.routesBySlug.values(),s=>s.hasClientRoutes&&c.startsWith(s.slug));if(c===y.OIDC_CALLBACK+"/")return t.json({templateId:"403OIDC",sharedDataIds:{},props:{seo:{title:`${i} - Forbidden`}}},200,{"Cache-Control":r});const{isAuthenticated:C,teams:D,claims:{name:j,picture:v,email:I}}=t.get("auth"),R={isAuthenticated:C,email:I,teams:D},g={isAuthenticated:C,name:j,picture:v,email:I,teams:D},E=H({redirects:e.config.redirects,requestedSlug:n})?.location;if(E)return G.send("redirect",{from:n,templateId:"404"}),t.json({templateId:"404",redirectTo:E,sharedDataIds:{},props:{}},301,{"Cache-Control":r});if(!o){const s=e.getRouteBySlug(n,{followRedirect:!0});return l.error(`Page not found: ${d}`),t.json({templateId:"404",redirectTo:s?.slug,sharedDataIds:{},props:{seo:{title:`${i} - Not Found`}},userData:g},404,{"Cache-Control":r})}if(l.verbose(`Page viewed: ${o.slug}`),!O(o,R,e.config.rbac,e.config.requiresLogin)&&o.slug!==w){if(C)return t.json({templateId:"403",sharedDataIds:{},props:{seo:{title:`${i} - Forbidden`}},userData:g},403,{"Cache-Control":r});const s=Object.keys(h||{}).length>0;return t.json({templateId:"404",sharedDataIds:{},props:{seo:{title:`${i} - Not Found`}},userData:g,...s?{redirectTo:P(e,o.slug)}:{}},s?401:404,{"Cache-Control":r})}const A=S(o.versions,R,e.config.rbac,e.config.requiresLogin),T=e.routesSharedData.get(o.slug)||{},_=await a(o),L=await U(o,t,_,e),b={templateId:o.templateId,versions:A,sharedDataIds:T,props:process.env.NODE_ENV==="production"?V(L):L,slug:o.slug,userData:g};return t.json(b,200,{"Cache-Control":r})}}function oe(e){return async(a,t)=>{const m=a.get("logger"),{req:l}=a,{pathname:u}=new URL(l.url),d=u.match(/\/page-data\/shared\/(.*)\.json/);if(!d)return t();const f=decodeURIComponent(d[1]),h=await F(f,e.outdir),{isAuthenticated:i,teams:p,claims:{email:c}}=a.get("auth"),n=S(h,{isAuthenticated:i,email:c,teams:p},e.config.rbac,e.config.requiresLogin);return n?a.json(n,200,{"Cache-Control":r}):(m.error(`Shared data not found: ${u}`),a.text("Not Found",404,{"Cache-Control":r}))}}export{te as pageDataHandler,oe as sharedPageDataHandler};
