var b=Object.defineProperty;var G=(a,e,t)=>e in a?b(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var n=(a,e)=>b(a,"name",{value:e,configurable:!0});var c=(a,e,t)=>G(a,typeof e!="symbol"?e+"":e,t);import H from"axios";import{z as D}from"zod";import E from"axios";import{z as N}from"zod";import k from"axios";import{z as q}from"zod";var l;(r=>(r.extractServerResponseCode=n(s=>{if(s instanceof q.ZodError)return"validation-exception";if(!k.isAxiosError(s)||!s.response)return null;let i=s.response.data;return typeof i?.code=="string"?i.code:null},"extractServerResponseCode"),r.doesServerErrorMessageContain=n((s,i)=>{let p=(0,r.extractServerErrorMessage)(s);return p==null?!1:p.toLowerCase().includes(i.toLowerCase())},"doesServerErrorMessageContain"),r.extractServerErrorMessage=n(s=>{if(s instanceof q.ZodError)return s.message;if(!k.isAxiosError(s)||!s.response)return null;let i=s.response.data;return typeof i?.message=="string"?i.message:null},"extractServerErrorMessage"),r.extractContentDispositionFilename=n(s=>{let i=s["content-disposition"];return i?/filename=["']?([^"';]+)/i.exec(i)?.[1]:void 0},"extractContentDispositionFilename")))(l||(l={}));var g=class g extends Error{constructor(t,o,r){super(t);c(this,"code");c(this,"serverMessage",null);this.code=o,this.serverMessage=r}};n(g,"ApplicationException");var d=g,C=class C{constructor({entries:e,onRethrowError:t}){c(this,"entries",[]);c(this,"onRethrowError");this.onRethrowError=t;let o={code:"DATA_VALIDATION_ERROR",condition:n(u=>u instanceof N.ZodError,"condition"),getMessage:n(()=>"An error occurred while validating the data","getMessage")},r={code:"INTERNAL_ERROR",condition:n(u=>E.isAxiosError(u)?u.response?.status!=null&&u.response.status>=500&&u.response.status<600:!1,"condition"),getMessage:n(()=>"An internal error occurred. This is most likely a bug on our end. Please try again later.","getMessage")},s={code:"NETWORK_ERROR",condition:n(u=>E.isAxiosError(u)?u.code==="ERR_NETWORK":!1,"condition"),getMessage:n(()=>"A network error occurred. Are you connected to the internet?","getMessage")},i={code:"CANCELED_ERROR",condition:n(u=>!!(E.isCancel(u)||E.isAxiosError(u)&&u.code==="ECONNABORTED"),"condition"),getMessage:n(()=>"The request was canceled.","getMessage")},p={code:"UNKNOWN_ERROR",condition:n(()=>!0,"condition"),getMessage:n(()=>"An unknown error occurred. Please try again later.","getMessage")};this.entries=[...e,o,r,s,i,p]}rethrowError(e){let t=this.entries.find(s=>s.condition(e??{})),o=l.extractServerErrorMessage(e),r=new d(t.getMessage(e),t.code,o);throw this.onRethrowError?.(e,r),r}getError(e){return e instanceof d?e:null}getErrorCode(e){return e instanceof d?e.code:null}static getErrorMessage(e,t=!0){return typeof e=="string"?e:e instanceof Error||e instanceof d?e.message:t?"An unknown error occurred. Please try again later.":null}};n(C,"ErrorHandler");var m=C,x=new m({entries:[]});var I=class I{constructor({config:e,interceptors:t,errorHandler:o}={}){c(this,"client");c(this,"errorHandler");this.client=H.create(e),this.errorHandler=o??x,this.attachInterceptors(t)}attachInterceptors(e,...t){e?.forEach(o=>this.attachInterceptor(o,...t))}attachInterceptor(e,...t){e.addInterceptor(this.client,...t)}ejectInterceptor(e){e.removeInterceptor(this.client)}async get(e,t,o){return this.makeRequest(e,{...o,method:"get",url:t})}async post(e,t,o,r){return this.makeRequest(e,{...r,method:"post",url:t,data:o})}async patch(e,t,o,r){return this.makeRequest(e,{...r,method:"patch",url:t,data:o})}async put(e,t,o,r){return this.makeRequest(e,{...r,method:"put",url:t,data:o})}async delete(e,t,o,r){return this.makeRequest(e,{...r,method:"delete",url:t,data:o})}async makeRequest(e,t){let o=new Error().stack;try{let{rawResponse:r,...s}=t,i=await this.client(s),p=e.resSchema.parse(i.data);return r?{...i,data:p}:p}catch(r){throw r instanceof D.ZodError&&(r.name="BE Response schema mismatch - ZodError",r.stack=[r.stack,...o?.split(`
`).slice(2)??[]].join(`
`)),(e.errorHandler??this.errorHandler).rethrowError(r),r}}};n(I,"RestClient");var h=I;var T=class T{constructor(e){this.applyInterceptor=e;c(this,"interceptorIdMap",[])}addInterceptor(e,...t){this.removeInterceptor(e);let o=this.applyInterceptor(e,...t);this.interceptorIdMap.push({client:e,interceptorId:o})}removeInterceptor(e){let t=this.interceptorIdMap.find(o=>o.client===e)?.interceptorId;t!=null&&(e.interceptors.request.eject(t),this.interceptorIdMap=this.interceptorIdMap.filter(o=>o.client!==e))}};n(T,"RestInterceptor");var y=T;import{createContext as S,use as W,useMemo as _}from"react";import{jsx as L}from"react/jsx-runtime";var R;(o=>{let a=S(null);o.Provider=n(({children:r,replace:s})=>{let i=_(()=>({replace:s}),[s]);return L(a,{value:i,children:r})},"Provider"),o.useRouter=n(()=>{let r=W(a);if(!r)throw new Error("useRouter must be used within an OpenApiRouter.Provider");return r},"useRouter")})(R||(R={}));import{createContext as U,use as K,useMemo as V}from"react";import{jsx as z}from"react/jsx-runtime";var w;(o=>{let a=U({});o.Provider=n(({preferUpdate:r=!0,children:s})=>{let i=V(()=>({preferUpdate:r}),[r]);return z(a.Provider,{value:i,children:s})},"Provider"),o.useConfig=n(()=>K(a)??{},"useConfig")})(w||(w={}));import{createContext as F,use as j,useMemo as B}from"react";import{jsx as Q}from"react/jsx-runtime";var f;(o=>{let a=F({});o.Provider=n(({isAuthenticated:r,isInitializing:s,logout:i,updateTokens:p,accessToken:u,user:A,userPromise:v,routes:O,loadingState:P,children:M})=>{let Z=B(()=>({isAuthenticated:r,isInitializing:s,logout:i,updateTokens:p,accessToken:u,user:A,userPromise:v,routes:O,loadingState:P}),[r,s,i,p,u,A,v,O,P]);return Q(a.Provider,{value:Z,children:M})},"Provider"),o.useAuth=n(()=>j(a),"useAuth")})(f||(f={}));import{useEffect as J,useState as X}from"react";var Y=n(({type:a,redirectTo:e,children:t})=>{let{isAuthenticated:o,routes:r,loadingState:s}=f.useAuth(),{replace:i}=R.useRouter(),[p,u]=X(!1);return J(()=>{u(!0)},[]),p?a==="private"&&!o?(i(e||r?.unauthenticated||"/"),null):a==="public-only"&&o?(i(e||r?.authenticated||"/"),null):t:s},"AuthGuard");export{d as ApplicationException,f as AuthContext,Y as AuthGuard,m as ErrorHandler,w as OpenApiQueryConfig,R as OpenApiRouter,h as RestClient,y as RestInterceptor,l as RestUtils,x as SharedErrorHandler};
