var e;function t(e,t,n){const s=document.createElement("div");s.style.display="flex",s.style.flexDirection="column",s.style.alignItems="flex-end",s.style.width="fit-content",s.style.fontFamily="Roboto, sans-serif";const o=document.createElement("button"),r=document.createElement("span");r.textContent=`$${t.charge}`,r.style.fontWeight="bold";const a=document.createElement("span");a.textContent=" / Generation",o.appendChild(r),o.appendChild(a),o.style.margin="0.5rem",o.style.padding="1rem 4rem",o.style.backgroundColor="hsla(280, 68%, 60%, 1)",o.style.color="white",o.style.border="none",o.style.fontSize="24px",o.style.alignItems="center",o.style.justifyContent="center",o.style.display="flex",o.style.gap="0.5rem",o.style.borderRadius="8px",o.style.width="fit-content";const i=function(){const e="http://www.w3.org/2000/svg",t=document.createElementNS(e,"svg");t.setAttribute("width","30"),t.setAttribute("height","30"),t.setAttribute("viewBox","0 0 800 800");const n=document.createElementNS(e,"rect");n.setAttribute("width","800.12"),n.setAttribute("height","800.12"),n.setAttribute("x","0.4"),n.setAttribute("y","0.26"),n.setAttribute("rx","96.52"),n.setAttribute("ry","96.52"),n.setAttribute("fill","hsla(280, 68%, 60%, 1)"),t.appendChild(n);const s=document.createElementNS(e,"path");s.setAttribute("d","M674.74 399.88s54.16 117.48 8.73 211.82H543.71s-1.75-89.1 61.14-132.77c62.89-43.67 69.88-79.05 69.88-79.05Z"),s.setAttribute("fill","#ffffff"),t.appendChild(s);const o=document.createElementNS(e,"path");o.setAttribute("d","M424.05 369.75s-.87-120.15 110.93-159.65c0 0 127.53-43.97 148.49-62.7 0 0 10.48 86.09-78.61 93.95 0 0 73.37 75.99 34.07 159.85-39.31 83.86-158.1 77.74-194.79 23.58 0 0-19.97-26.04-20.09-26.2-1.59-2.07 7.93-7.46 9.12-7.71 11.38-2.42 18.97 11.24 26 17.59 22.97 20.76 57.51 31.51 88.05 26.28 44.16-7.56 70.84-45.84 70.41-88.96-.41-41.36-25.05-72.95-63.67-86.96-90.69-32.9-129.9 110.93-129.9 110.93ZM399.09 369.75s.87-120.15-110.93-159.65c0 0-127.53-43.97-148.49-62.7 0 0-10.48 86.09 78.61 93.95 0 0-73.37 75.99-34.07 159.85 39.31 83.86 158.1 77.74 194.79 23.58 0 0 19.91-25.97 20.09-26.2 2.49-3.25-10.73-7.94-12.29-7.9-10.39.23-19.99 15.39-27.57 21.23-21.37 16.48-48.51 26.3-75.67 23.86-46.15-4.15-77.31-42.05-78.02-86.9-.68-42.54 23.81-75.6 63.66-90.05 90.69-32.9 129.9 110.93 129.9 110.93Z"),o.setAttribute("fill","#ffffff"),t.appendChild(o);const r=document.createElementNS(e,"path");r.setAttribute("d","M148.89 399.88s-54.16 117.48-8.73 211.82h139.76s1.75-89.1-61.14-132.77c-62.89-43.67-69.88-79.05-69.88-79.05Z"),r.setAttribute("fill","#ffffff"),t.appendChild(r);const a=document.createElementNS(e,"path");a.setAttribute("d","M411.82 147.42S345 145.7 272.93 178.46c0 0 91.28 24.46 138.89 94.34 47.61-69.88 138.89-94.34 138.89-94.34-72.06-32.76-138.89-31.04-138.89-31.04Z"),a.setAttribute("fill","#ffffff"),t.appendChild(a);const i=document.createElementNS(e,"circle");i.setAttribute("cx","522.31"),i.setAttribute("cy","346.16"),i.setAttribute("r","53.72"),i.setAttribute("fill","#2dd4e0"),t.appendChild(i);const c=document.createElementNS(e,"circle");c.setAttribute("cx","298.26"),c.setAttribute("cy","346.16"),c.setAttribute("r","53.72"),c.setAttribute("fill","#2dd4e0"),t.appendChild(c);const l=document.createElementNS(e,"path");return l.setAttribute("d","M410.25 372.31c-6.56.27-11.51.16-17.12 4.11-5.18 3.65-9.76 8.09-13.85 12.91-2.04 2.41-21 30.77-20.19 31.14 12.37 5.97 22.2 16.26 30.1 27.5 7.89 11.25 14.09 23.57 21.1 35.39 7.01-11.82 13.21-24.15 21.11-35.4 7.9-11.25 17.73-21.54 30.11-27.51-8.57-15.26-17.46-30.9-31.09-41.87-3.57-2.87-7.58-5.45-12.1-6.18-2.29-.37-4.64-.26-6.96-.15-.38.02-.74.03-1.11.05Z"),l.setAttribute("fill","#ffffff"),t.appendChild(l),t}();o.prepend(i);const c=document.createElement("div");c.style.display="flex",c.style.margin="0.5rem",c.style.alignItems="center",c.style.justifyContent="center",c.style.backgroundColor="#78788014",c.style.borderRadius="24px",c.style.width="fit-content",c.style.boxSizing="border-box",c.style.display="none";const l=document.createElement("img");l.src=e.getAvatar(),l.alt="Avatar",l.style.width="32px",l.style.height="32px",l.style.borderRadius="50%",l.style.marginRight="1rem",c.appendChild(l);const d=document.createElement("span");d.textContent=e.getUsername(),d.style.fontSize="20px",d.style.fontWeight="medium",d.style.marginRight="1rem",d.style.color="black",c.appendChild(d);const h=document.createElement("button");h.textContent="▼",h.style.background="none",h.style.border="none",h.style.cursor="pointer",h.style.fontSize="1rem",h.style.marginRight="0.5rem",h.style.color="black",c.appendChild(h);const u=document.createElement("div");u.style.position="absolute",u.style.backgroundColor="#ffffff",u.style.border="1px solid #ccc",u.style.borderRadius="8px",u.style.boxShadow="0 4px 6px rgba(0, 0, 0, 0.1)",u.style.padding="0.5rem",u.style.marginTop="0.5rem",u.style.display="none",u.style.zIndex="1000",u.style.top="100%",u.style.right="0",u.style.width="fit-content",u.style.boxSizing="border-box";const p=document.createElement("button");p.textContent="Logout",p.style.background="none",p.style.border="none",p.style.cursor="pointer",p.style.fontSize="1rem",p.style.color="red",p.style.padding="0.5rem 1rem",p.style.textAlign="left",p.style.width="100%",p.addEventListener("click",()=>{e.logout(),c.style.display="none",location.reload()}),u.appendChild(p),c.style.position="relative",c.appendChild(u),h.addEventListener("click",()=>{u.style.display="none"===u.style.display?"block":"none"}),document.addEventListener("click",e=>{c.contains(e.target)||(u.style.display="none")}),s.appendChild(o),s.appendChild(c);const m=document.createElement("span");return m.style.color="red",m.style.margin="0.5rem 0",m.style.fontSize="1rem",m.className="error-message",m.style.display="none",s.insertBefore(m,c),e.IsAuthenticated()&&(l.src=e.getAvatar(),d.textContent=e.getUsername(),c.style.display="flex"),o.addEventListener("click",async()=>{e.IsAuthenticated()||await e.Authenticate(),e.IsAuthenticated()&&(l.src=e.getAvatar(),d.textContent=e.getUsername(),c.style.display="flex",e.CloudhandsPurchase(t).then(e=>{e.success?(m.style.display="none",n(e)):(console.log(e),m.textContent=`${e.message||"Unknown error"}`,m.style.display="block",s.insertBefore(m,c))}))}),s}!function(e){e[e.Each=0]="Each",e[e.Monthly=1]="Monthly"}(e||(e={}));class n{constructor(e){this.clientId=void 0,this.codeVerifier=void 0,this.state=void 0,this.NEXT_PUBLIC_API_URL="http://api.localhost:5000",this.NEXT_PUBLIC_LIVE_URL="https://api.cloudhands.ai",this.SDK_VERSION="1.0.0",this.clientId=e,this.codeVerifier=this.getCodeVerifier(),this.state=this.getState()}async Authenticate(){const e=this.HandleRedirect();if(!e){const e=await this.GetAuthUrl(),t=window.open(e,"CloudhandsAuth","width=600,height=800,scrollbars=no,resizable=no");if(!t)throw new Error("Failed to open authentication popup.");const n=500;return new Promise((e,s)=>{const o=setInterval(()=>{try{if(!t||t.closed)return clearInterval(o),void s(new Error("Authentication popup was closed."));const n=new URLSearchParams(t.location.search),r=n.get("code"),a=n.get("state");if(r&&a){if(a!==this.state)return clearInterval(o),t.close(),void s(new Error("Invalid state parameter."));clearInterval(o),t.close(),this.ExchangeCodeForToken(r).then(e).catch(s)}}catch(e){}},n)})}{const{code:t}=e;await this.ExchangeCodeForToken(t)}}HandleRedirect(){const e=new URLSearchParams(window.location.search),t=e.get("code"),n=e.get("state");return t&&n?{code:t,state:n}:null}IsAuthenticated(){return!!this.getAccessToken()}CreateCharge(t,n,s=e.Each,o={}){return{author_id:this.clientId,sdk_version:this.SDK_VERSION,charge_type:s,charge:t,event_name:n,metadata:o}}async GetAuthUrl(){const e=`${window.location.href}`;this.state=this.generateRandomString(16,16);const t=await this.generateCodeChallenge(this.codeVerifier);return`https://cloudhands.ai/auth?response_type=code&client_id=${this.clientId}&redirect_uri=${e}&state=${this.state}&code_challenge=${t}&code_challenge_method=S256`}async ExchangeCodeForToken(e){const t=`${this.NEXT_PUBLIC_LIVE_URL}/token`,n=`${window.location.href}`;try{const s=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:e,clientId:this.clientId,redirectUri:n,code_Verifier:this.codeVerifier})});if(!s.ok)throw new Error(`Error fetching token: ${s.statusText}`);const o=await s.json();document.cookie=`cloudhands_access_token=${o.access_token}; path=/; max-age=3600`,document.cookie=`cloudhands_username=${o.username}; path=/; max-age=3600`,document.cookie=`cloudhands_avatar=https://cdn.cloudhands.ai/avatar/${o.avatar}; path=/; max-age=3600`}catch(e){console.error("Error exchanging code for token:",e)}}async CloudhandsPurchase(e){const t=`${this.NEXT_PUBLIC_LIVE_URL}/sdk/charge`;try{const n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.getAccessToken()}`},body:JSON.stringify(e)});if(!n.ok){console.log(n);const e=`Cloudhands response was not ok: ${n.statusText}`;return console.error(e),{success:!1,message:e}}const s=await n.json();if(s.isSuccessful)return{success:!0,message:"Purchase completed successfully."};{const e=`${s.errors[0]||"Unknown error"}`;return console.error(e),{success:!1,message:e}}}catch(e){const t=`Fetch error: ${e instanceof Error?e.message:String(e)}`;return console.error(t),{success:!1,message:t}}}generateRandomString(e,t){const n=Math.floor(Math.random()*(t-e+1))+e;let s="";for(let e=0;e<n;e++)s+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~".charAt(Math.floor(66*Math.random()));return s}async generateCodeChallenge(e){const t=(new TextEncoder).encode(e),n=await crypto.subtle.digest("SHA-256",t);return btoa(String.fromCharCode(...new Uint8Array(n))).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}getState(){let e=this.getCookie("cloudhands_state");if(!e){e=this.generateRandomString(16,16);const t=new Date(Date.now()+3e5).toUTCString();document.cookie=`cloudhands_state=${e}; expires=${t}; path=/`}return e}getCodeVerifier(){let e=this.getCookie("cloudhands_code_verifier");if(!e){e=this.generateRandomString(43,128);const t=new Date(Date.now()+3e5).toUTCString();document.cookie=`cloudhands_code_verifier=${e}; expires=${t}; path=/`}return e}getCookie(e){const t=document.cookie.match(new RegExp(`(?:^|; )${e}=([^;]*)`));return t?decodeURIComponent(t[1]):null}getAvatar(){return this.getCookie("cloudhands_avatar")||"https://cdn.cloudhands.ai/avatar/default.png"}getUsername(){return this.getCookie("cloudhands_username")||"Guest"}getAccessToken(){return this.getCookie("cloudhands_access_token")||null}logout(){["cloudhands_access_token","cloudhands_username","cloudhands_avatar","cloudhands_state","cloudhands_code_verifier"].forEach(e=>{document.cookie=`${e}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`});const e=new URL(window.location.href);e.searchParams.delete("code"),e.searchParams.delete("state"),window.history.replaceState({},document.title,e.toString())}}export{e as ChargeType,t as CloudhandsButton,n as default}; | |