!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).cloudhands={})}(this,function(e){var t;function n(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}e.ChargeType=void 0,(t=e.ChargeType||(e.ChargeType={}))[t.Each=0]="Each",t[t.Monthly=1]="Monthly";var r=/*#__PURE__*/function(){function t(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()}var r=t.prototype;return r.Authenticate=function(){try{var e=this,t=e.HandleRedirect();return Promise.resolve(t?Promise.resolve(e.ExchangeCodeForToken(t.code)).then(function(){}):Promise.resolve(e.GetAuthUrl()).then(function(t){var n=window.open(t,"CloudhandsAuth","width=600,height=800,scrollbars=no,resizable=no");if(!n)throw new Error("Failed to open authentication popup.");return new Promise(function(t,r){var o=setInterval(function(){try{if(!n||n.closed)return clearInterval(o),void r(new Error("Authentication popup was closed."));var a=new URLSearchParams(n.location.search),s=a.get("code"),i=a.get("state");if(s&&i){if(i!==e.state)return clearInterval(o),n.close(),void r(new Error("Invalid state parameter."));clearInterval(o),n.close(),e.ExchangeCodeForToken(s).then(t).catch(r)}}catch(e){}},500)})}))}catch(e){return Promise.reject(e)}},r.HandleRedirect=function(){var e=new URLSearchParams(window.location.search),t=e.get("code"),n=e.get("state");return t&&n?{code:t,state:n}:null},r.IsAuthenticated=function(){return!!this.getAccessToken()},r.CreateCharge=function(t,n,r,o){return void 0===r&&(r=e.ChargeType.Each),void 0===o&&(o={}),{author_id:this.clientId,sdk_version:this.SDK_VERSION,charge_type:r,charge:t,event_name:n,metadata:o}},r.GetAuthUrl=function(){try{var e=this,t=""+window.location.href;return e.state=e.generateRandomString(16,16),Promise.resolve(e.generateCodeChallenge(e.codeVerifier)).then(function(n){return"https://cloudhands.ai/auth?response_type=code&client_id="+e.clientId+"&redirect_uri="+t+"&state="+e.state+"&code_challenge="+n+"&code_challenge_method=S256"})}catch(e){return Promise.reject(e)}},r.ExchangeCodeForToken=function(e){try{var t=this,r=t.NEXT_PUBLIC_LIVE_URL+"/token",o=""+window.location.href;return Promise.resolve(n(function(){return Promise.resolve(fetch(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({code:e,clientId:t.clientId,redirectUri:o,code_Verifier:t.codeVerifier})})).then(function(e){if(!e.ok)throw new Error("Error fetching token: "+e.statusText);return Promise.resolve(e.json()).then(function(e){document.cookie="cloudhands_access_token="+e.access_token+"; path=/; max-age=3600",document.cookie="cloudhands_username="+e.username+"; path=/; max-age=3600",document.cookie="cloudhands_avatar=https://cdn.cloudhands.ai/avatar/"+e.avatar+"; path=/; max-age=3600"})})},function(e){console.error("Error exchanging code for token:",e)}))}catch(e){return Promise.reject(e)}},r.CloudhandsPurchase=function(e){try{var t=this,r=t.NEXT_PUBLIC_LIVE_URL+"/sdk/charge";return Promise.resolve(n(function(){return Promise.resolve(fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer "+t.getAccessToken()},body:JSON.stringify(e)})).then(function(e){if(!e.ok){console.log(e);var t="Cloudhands response was not ok: "+e.statusText;return console.error(t),{success:!1,message:t}}return Promise.resolve(e.json()).then(function(e){if(e.isSuccessful)return{success:!0,message:"Purchase completed successfully."};var t=""+(e.errors[0]||"Unknown error");return console.error(t),{success:!1,message:t}})})},function(e){var t="Fetch error: "+(e instanceof Error?e.message:String(e));return console.error(t),{success:!1,message:t}}))}catch(e){return Promise.reject(e)}},r.generateRandomString=function(e,t){for(var n=Math.floor(Math.random()*(t-e+1))+e,r="",o=0;o