var J=t=>{try{return JSON.parse(t??"null")}catch{}},p=t=>{let e=new Map,s=new Map,r=o=>{let a=Date.now(),{jwt:n,exp:i}=e.get(o)??{};return n&&i&&i>a?n:(n&&e.delete(o),{jwt:n,exp:i}=J(t?.getItem(o))??{},n&&i&&i>a?(e.set(o,{jwt:n,exp:i}),n):(n&&t?.removeItem(o),null))},c=(o,a,n)=>{let i=Date.now();a===""||n<i||(e.set(o,{jwt:a,exp:n}),t?.setItem(o,JSON.stringify({jwt:a,exp:n})),clearTimeout(s.get(o)),s.set(o,setTimeout(()=>u(o),n-i)))},u=o=>{e.delete(o),clearTimeout(s.get(o)),s.delete(o),t?.removeItem(o)};return{getJwt:r,setJwt:c,clear:()=>{t&&Array.from(e.keys()).forEach(o=>t?.removeItem(o)),e.clear(),Array.from(s.values()).forEach(clearTimeout),s.clear()}}};var l=class extends Error{#t;get status(){return this.#t}#e;get statusText(){return this.#e}#s;get url(){return this.#s}constructor(e,{status:s,statusText:r,url:c}){super(e),this.#t=s,this.#e=r,this.#s=c}toJSON(){return{name:this.constructor.name,message:this.message,status:this.#t,statusText:this.#e,url:this.#s}}};var d=(t,e)=>fetch(t,e).then(s=>s.json().catch(r=>({message:`${r.name} - ${r.message}`})).then(({message:r,token:c})=>{if(!s.ok||r&&!c)throw new l(r,{status:s.status,statusText:s.statusText,url:s.url});return c}));var f="http://schemas.xmlsoap.org/ws/2005/05/identity/claims",$="http://schemas.microsoft.com/ws/2008/06/identity/claims",w="http://schemas.dndbeyond.com/ws/2019/08/identity/claims",A={[`${f}/nameidentifier`]:"id",[`${f}/name`]:"name",[`${$}/role`]:"roles",[`${f}/emailaddress`]:"email",[`${w}/subscriber`]:"isSubscriber",[`${w}/subscriptiontier`]:"subscriptionTier"},O={id:Number,roles:t=>[t].flat(),isSubscriber:t=>t==="True"},v=t=>t;function N(t,e){let s=A[t]??t,r=(O[s]??v)(e);if(s===t)return r;this[s]=r}var y=(t,e)=>{try{return JSON.parse(t,e)}catch{}},k=(t,e)=>new RegExp(t,e),x={"-":"+",_:"/"},P=k(`[${Object.keys(x).join("")}]`,"g"),I=t=>x[t],_=t=>t.replace(P,I),g=t=>{try{return atob(_(t))}catch{}},h=t=>!!t&&typeof t=="object",j=t=>h(t)&&"alg"in t&&!!t.alg,D=()=>Math.floor(Date.now()/1e3),R=t=>h(t)&&typeof t.id=="number"&&t.id>0&&typeof t.exp=="number",M=t=>h(t)&&typeof t.exp=="number"&&t.exp<=D(),C=t=>typeof t=="string",K=t=>t?.length===3&&t.every(C),L=([t,e])=>{let s=y(g(t)??"{}"),r=y(g(e)??e,N);return j(s)&&R(r)?{header:s,payload:r,isExpired:M(r)}:null},U=t=>K(t)?L(t):null,T=t=>U(t?.split("."));var V=t=>t*1e3,Y=()=>({jwt:null,token:null}),b=(t,e)=>async(s,{useCache:r=!0,parseToken:c=!0,...u}={})=>{let m="cache",o=await e(s,u),a=r&&t.getJwt(o);if(a&&!c)return{jwt:a,token:null};if(a||(a=await d(s,u),m="network"),!a)return Y();let n=T(a);if(!n)return{jwt:null,token:null};if(r&&m==="network"&&!n.isExpired){let{exp:i}=n.payload;t.setJwt(o,a,V(i))}return{jwt:a,token:n}};var S=t=>{let e=new Map;return(s,r)=>{let c=`${s.href} ${JSON.stringify(r)}`,u=e.get(c)??t(s,r).finally(()=>e.delete(c));return e.has(c)||e.set(c,u),u}};var H=/\/v([1-9]*)(?:\/|$)/,B=(t,...e)=>(...s)=>e.reduce((r,c)=>c(r),t(...s)),F=t=>t?.href??"",G=t=>t.match(H)?.[1]??"0",X=t=>parseInt(t,10)||0,E=B(F,G,X);var q="_kobold-token",z=t=>Promise.resolve(`${q}-${E(t)}`),Q=p(globalThis.localStorage),W=b(Q,z),Z=S(W);var pt=(t,e)=>Z(t,e).then(({jwt:s})=>s);export{pt as a};
//# sourceMappingURL=chunk-EY555SK2.js.map
