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