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