import{setCookie as E}from"hono/cookie";import{withPathPrefix as m}from"@redocly/theme/core/utils";import{DEV_LOGIN_SLUG as P}from"../../constants/index.js";import{CACHE_CONTROL_NO_CACHE_HEADER_VALUE as b}from"../constants/common.js";import{getAuthProviderLoginParams as w,buildLoginUrl as A}from"./auth.js";import{renderPage as C}from"../ssr/index.js";async function j(r,t,n,o){const{isAuthenticated:i}=r.get("auth"),e=r.req.raw.headers.get("x-forwarded-host"),a=e?"https://"+e:new URL(r.req.url).origin,l=t.getConfig().ssoDirect,u=Object.keys(l||{}),s=o||u[0],c=l?.[s];if(i)return _(r,t,{slug:n},"403");const g=s&&c?await w(s,c):void 0,{loginUrl:U,cookies:d={}}=g&&A(g,a,n)||{},f=t.globalData.auth?.devLogin||u.length>1?H(n):U;return Object.keys(d).forEach(h=>{E(r,h,d[h].value,d[h].options)}),f?r.newResponse(null,302,{Location:f}):r.text("Unauthorized",401)}const p={};async function _(r,t,n,o,i){let e=p[o];if(!e){const a={templateId:i||o,fsPath:"/",...n,baseSlug:n.slug};e=(await C(a,{},r,t)).html,p[o]=e}return r.html(e,parseInt(o),{"Cache-Control":b})}function H(r){const t=new URLSearchParams({redirectTo:m(r)});return`${m(P)}?${t}`}async function q(r){return r.text("Forbidden",L(r))}function F(r){return r.json({message:"Forbidden"},L(r))}function L(r){const{isAuthenticated:t}=r.get("auth");return t?403:401}export{H as getLoginUrlWithRedirect,_ as handleErrorPageRender,j as handleUnauthorized,F as handleUnauthorizedApiRequest,q as handleUnauthorizedAsset};
