import{c as h,l as c,n as s}from"./main-few-lands-burn.js";import{c as i}from"./mutative-C6xjlr6O.js";const r=new BroadcastChannel("shareOauthInfo"),n=h(e=>({actions:{setOauthInfo:t=>{e({oauthInfo:t}),r.postMessage(t)}},oauthInfo:{}}));r.addEventListener("message",e=>{n.setState({oauthInfo:e.data})});const m=(e=64)=>{const t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";let a="";for(let o=0;o<e;o++)a+=t.charAt(Math.floor(Math.random()*t.length));return a},u=({queryParams:e,url:t})=>{const a=new URL(t);return e&&(Object.entries(e).forEach(([o,d])=>a.searchParams.append(o,String(d))),a.searchParams.append("state",s.PLUGIN_ADMIN_URL+"/auth-response/"),a.searchParams.append("redirect_uri",s.REDIRECT_URI)),a},I=(e,t)=>{if(e.type!=="oauth2"||!e.authCodeEndpoint)return console.error("Auth code endpoint is not defined"),t(!1),!1;const a=window.open(u(e.authCodeEndpoint),e.label,"width=500,height=600,toolbar=off"),o=setInterval(()=>{a?.closed&&(clearInterval(o),Object.keys(n.getState().oauthInfo).length===0&&t(!1))},1e3)},f=async e=>{const t=await c(e);return t.status==="error"||!t?.data||"error"in t.data?(console.error(t?.data),!1):t?.data},w=async(e,t)=>{if(e.type!=="oauth2"||!e.tokenEndpoint||!e.tokenEndpoint.bodyParams)return console.error("Token endpoint or body params is not defined"),!1;const a=i(e.tokenEndpoint,o=>{o.bodyParams&&(o.bodyParams.redirect_uri=s.REDIRECT_URI,o.bodyParams.code=decodeURIComponent(t.code))});return f(a)},R=()=>{const e={},t=new URLSearchParams(window.location.hash);for(const[a,o]of t)o&&(e[a]=o);n.getState().actions.setOauthInfo(e),window.close()};export{w as a,m as b,I as c,f as g,R as s,n as u};
