import{h as i,aP as g,da as p,z as b,y as l}from"./main-tangy-colts-beg.js";globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,e){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,e),e)}};function m(t,e){const a=i(e);a.debugLabel="baseAtom";const o=new Set,c=new BroadcastChannel(t);c.onmessage=s=>{for(const r of o)r(s)};const h=i(s=>s(a),(s,r,n)=>{r(a,n.value),n.isEvent||c.postMessage(s(a))});h.debugLabel="broadcastAtom",h.onMount=s=>{const r=n=>{s({isEvent:!0,value:n.data})};return o.add(r),()=>{o.delete(r)}};const d=i(s=>s(h),(s,r,n)=>{r(h,{isEvent:!1,value:n})});return d.debugLabel="returnedAtom",d}globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,e){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,e),e)}};const u=m("shareOauthInfo",{});globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,e){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,e),e)}};const f=g(),A=f.get,C=f.set,I=(t=64)=>{const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";let a="";for(let o=0;o<t;o++)a+=e.charAt(Math.floor(Math.random()*e.length));return a},w=({queryParams:t,url:e})=>{const a=new URL(e);return t&&(Object.entries(t).forEach(([o,c])=>a.searchParams.append(o,String(c))),a.searchParams.append("state",l.PLUGIN_ADMIN_URL+"/auth-response/"),a.searchParams.append("redirect_uri",l.REDIRECT_URI)),a},v=(t,e)=>{if(t.type!=="oauth2"||!t.authCodeEndpoint)return console.error("Auth code endpoint is not defined"),e(!1),!1;const a=window.open(w(t.authCodeEndpoint),t.label,"width=500,height=600,toolbar=off"),o=setInterval(()=>{a!=null&&a.closed&&(clearInterval(o),Object.keys(A(u)).length===0&&e(!1))},1e3)},y=async t=>{const e=await p(t);return e.status==="error"||!(e!=null&&e.data)||"error"in e.data?(console.error(e==null?void 0:e.data),!1):e==null?void 0:e.data},P=async(t,e)=>{if(t.type!=="oauth2"||!t.tokenEndpoint||!t.tokenEndpoint.bodyParams)return console.error("Token endpoint or body params is not defined"),!1;const a=b(t.tokenEndpoint,o=>{o.bodyParams&&(o.bodyParams.redirect_uri=l.REDIRECT_URI,o.bodyParams.code=decodeURIComponent(e.code))});return y(a)},U=()=>{const t={},e=new URLSearchParams(window.location.hash);for(const[a,o]of e)o&&(t[a]=o);C(u,t),window.close()};export{u as $,P as a,I as b,v as c,y as g,U as s};
