expo-quanta
Version:
Quanta.js/Expo: Expo SDK for Quanta Tools
3 lines (2 loc) • 24.8 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("expo-secure-store"),e=require("expo-device"),n=require("expo-constants"),r=require("expo-localization"),i=require("expo-application"),s=require("react"),o=require("react-native"),a=function(t,e){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},a(t,e)};var u=function(){return u=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},u.apply(this,arguments)};function c(t,e,n,r){return new(n||(n=Promise))((function(i,s){function o(t){try{u(r.next(t))}catch(t){s(t)}}function a(t){try{u(r.throw(t))}catch(t){s(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,a)}u((r=r.apply(t,e||[])).next())}))}function l(t,e){var n,r,i,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=a(0),o.throw=a(1),o.return=a(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(u){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,r=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=e.call(t,s)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}function h(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function p(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,s=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=s.next()).done;)o.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=s.return)&&n.call(s)}finally{if(i)throw i.error}}return o}function f(t,e,n){if(n||2===arguments.length)for(var r,i=0,s=e.length;i<s;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var d="",g="",v=new(function(s){function o(){return null!==s&&s.apply(this,arguments)||this}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(o,s),o.prototype.init=function(){this.initializeAsync(void 0,!0).catch((function(){}))},o.prototype.makeAsyncStorage=function(){var e=this;return{setItem:function(n,r){return c(e,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,t.setItemAsync(n,r)];case 1:return e.sent(),[2]}}))}))},getItem:function(n){return c(e,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,t.getItemAsync(n)];case 1:return[2,e.sent()||""]}}))}))}}},o.prototype.setupUrlChangeListeners=function(){},o.prototype.handleUrlChange=function(){return c(this,void 0,void 0,(function(){return l(this,(function(t){return[2]}))}))},o.prototype.sendViewEvent=function(){return c(this,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return[4,this.logAsync("launch")];case 1:return t.sent(),[2]}}))}))},o.prototype.getScriptTag=function(){return null},o.prototype.getAppIdFromScriptTag=function(){var t,e,r;return null!==(r=null===(e=null===(t=n.expoConfig)||void 0===t?void 0:t.extra)||void 0===e?void 0:e.QuantaId)&&void 0!==r?r:null},o.prototype.isServerSide=function(){return!1},o.prototype.systemLanguageProvider=function(){return r.getLocales()[0].languageTag},o.prototype.getBundleId=function(){return i.applicationId||""},o.prototype.getVersion=function(){var t;return i.nativeApplicationVersion||(null===(t=n.expoConfig)||void 0===t?void 0:t.version)||"1.0.0"},o.prototype.getDeviceInfo=function(){return e.modelName||"Expo Device"},o.prototype.getOSInfo=function(){var t,n;return"".concat(null!==(t=e.osName)&&void 0!==t?t:"Expo"," ").concat(null!==(n=e.osVersion)&&void 0!==n?n:"?")},o.prototype.isDebug=function(){return null!==__DEV__&&void 0!==__DEV__&&__DEV__},o}(function(){function t(){this._initialized=!1,this._initializing=0,this._initializingPromise=null,this._id="",this._appId="",this._abLetters="",this._abDict={},this._queue=[],this._isProcessing=!1,this._installDate=0,this._skipFirstViewEvent=!1,this._skipNavigationViewEvents=!1,this._skipAllViewEvents=!1,this._isFirstViewEvent=!0,this._currentPath=null,this.asyncStorage=this.makeAsyncStorage(),this.loggingEnabled=null}return t.prototype.getAppId=function(){return this._appId},t.prototype.initialize=function(t){this.initializeAsync(t).catch(console.error)},t.prototype.loadAppId=function(){return c(this,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return[4,this.asyncStorage.getItem("tools.quanta.appId")];case 1:return[2,t.sent()]}}))}))},t.prototype.setAppId=function(t){return c(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return[4,this.asyncStorage.setItem("tools.quanta.appId",t)];case 1:return[2,e.sent()]}}))}))},t.prototype.initializeAsync=function(t){return c(this,arguments,void 0,(function(t,e){var n,r,i,s,o,a,u,c;return void 0===e&&(e=!1),l(this,(function(l){switch(l.label){case 0:return this._initialized?t?(this._appId=t,[4,this.setAppId(t)]):[3,2]:[3,3];case 1:l.sent(),l.label=2;case 2:return[2];case 3:if(0==this._initializing++)return[3,9];l.label=4;case 4:return this._initializingPromise?[3,6]:[4,new Promise((function(t){return setTimeout(t,100)}))];case 5:return l.sent(),[3,4];case 6:return[4,this._initializingPromise];case 7:return l.sent(),[4,this.initializeAsync(t,e)];case 8:return l.sent(),[2];case 9:n=function(){},this._initializingPromise=new Promise((function(t){n=t})),l.label=10;case 10:return l.trys.push([10,,21,22]),this.isServerSide()?(e||console.info("[Quanta] Skipping client sdk call on server."),[2]):(this.parseScriptTagAttributes(),r=this,null===(u=null!=t?t:this.getAppIdFromScriptTag())||void 0===u?[3,11]:(i=u,[3,13]));case 11:return[4,this.loadAppId()];case 12:i=l.sent(),l.label=13;case 13:return r._appId=null!==(c=i)&&void 0!==c?c:"",this._appId?[4,this.setAppId(this._appId)]:(e||this.debugWarn("No Quanta app ID provided. Analytics will not be sent."),[2]);case 14:return l.sent(),s=this,[4,this.loadOrCreateId()];case 15:return s._id=l.sent(),o=this,[4,this.loadOrCreateInstallDate()];case 16:return o._installDate=l.sent(),[4,this.asyncStorage.getItem("tools.quanta.ab")];case 17:return a=l.sent()||"",this._abLetters=this.getAbLetters(a),this._abDict=this.getAbDict(a),this.setupUrlChangeListeners(),[4,this.loadQueue()];case 18:return l.sent(),[4,this.processQueue()];case 19:return l.sent(),this.isDebug()&&this.checkClaimed().catch(console.error),this.debugLog("Quanta initialized"),this._initialized=!0,[4,this.maybeSendViewEvent()];case 20:return l.sent(),[3,22];case 21:return this._initializing--,n(),[7];case 22:return[2]}}))}))},t.prototype.maybeSendViewEvent=function(){return c(this,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return this._initialized?[3,2]:[4,this.initializeAsync()];case 1:t.sent(),t.label=2;case 2:return this._skipAllViewEvents?[2]:this._skipFirstViewEvent&&this._isFirstViewEvent?(this._isFirstViewEvent=!1,[2]):this._skipNavigationViewEvents&&!this._isFirstViewEvent?[2]:[4,this.sendViewEvent()];case 3:return t.sent(),[2]}}))}))},t.prototype.parseScriptTagAttributes=function(){var t=this.getScriptTag();t&&(this._skipFirstViewEvent=t.hasAttribute("data-skip-first-view-event"),this._skipNavigationViewEvents=t.hasAttribute("data-skip-navigation-view-events"),this._skipAllViewEvents=t.hasAttribute("data-skip-all-view-events"),t.hasAttribute("data-enable-debug-logs")&&this.enableLogging())},t.prototype.log=function(t,e){void 0===e&&(e={}),this.logWithRevenue(t,0,e)},t.prototype.logAsync=function(t){return c(this,arguments,void 0,(function(t,e){return void 0===e&&(e={}),l(this,(function(n){switch(n.label){case 0:return[4,this.logWithRevenueAsync(t,0,e)];case 1:return n.sent(),[2]}}))}))},t.prototype.logWithRevenue=function(t,e,n,r){void 0===e&&(e=0),void 0===n&&(n={}),void 0===r&&(r=new Date),this.logWithRevenueAsync(t,e,n,r).catch(console.error)},t.prototype.logWithRevenueAsync=function(t){return c(this,arguments,void 0,(function(t,e,n,r){var i,s,o,a,u,c,p,f,d,v,y;return void 0===e&&(e=0),void 0===n&&(n={}),void 0===r&&(r=new Date),l(this,(function(l){switch(l.label){case 0:if(this.isServerSide())return console.info("[Quanta] Skipping client sdk call on server."),[2];if(this._initialized||this.initialize(),t.length>200&&(this.debugWarn("Event name is too long. Event name + args should be 200 characters or less. It will be truncated."),t=t.substring(0,200)),i="","string"==typeof n)i=this.safe(n,!0);else{s=Object.keys(n).sort();try{for(o=h(s),a=o.next();!a.done;a=o.next())u=a.value,c=this.safe(u,!1),p=this.safe(n[u],!1),i+="".concat(c).concat(g).concat(p).concat(g)}catch(t){v={error:t}}finally{try{a&&!a.done&&(y=o.return)&&y.call(o)}finally{if(v)throw v.error}}i.length>0&&(i=i.substring(0,i.length-1))}return t.length+i.length>200&&(this.debugWarn("Added arguments are too long. Event name + args should be 200 characters or less. They will be truncated."),i=i.substring(0,200-t.length)),f=this.getUserData(),d=this.stringForDouble(e),[4,this.enqueueEvent({appId:this._appId,userData:f,event:this.safe(t),revenue:d,addedArguments:this.safe(i,!0),time:r,abLetters:this._abLetters})];case 1:return l.sent(),[2]}}))}))},t.prototype.abTest=function(t){return this._initialized?this._abDict[t.toLowerCase()]||"A":(console.error("[Quanta] Ab test called in sync method before initialization. Please make sure to call initialize() first, or use abTestAsync() instead."),this.initialize(),"A")},t.prototype.abTestAsync=function(t){return c(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return this._initialized?[3,2]:[4,this.initializeAsync()];case 1:e.sent(),e.label=2;case 2:return[2,this._abDict[t.toLowerCase()]||"A"]}}))}))},t.prototype.setId=function(t){this.setIdAsync(t).catch(console.error)},t.prototype.setIdAsync=function(t){return c(this,void 0,void 0,(function(){var e;return l(this,(function(n){switch(n.label){case 0:return""!==this._id?[2]:(e=t,this.isValidUUID(e)&&(e=this.shortenUuid(e)),this._id=e,[4,this.asyncStorage.setItem("tools.quanta.id",this._id)]);case 1:return n.sent(),22!==this._id.length&&this.debugWarn("The ID ".concat(this._id," does not look like a valid UUID or Quanta ID. Only use UUIDs or shortened Quanta IDs as user IDs.")),[2]}}))}))},t.prototype.getId=function(){return this._id},t.prototype.loadOrCreateId=function(){return c(this,void 0,void 0,(function(){var t,e;return l(this,(function(n){switch(n.label){case 0:return[4,this.asyncStorage.getItem("tools.quanta.id")];case 1:return(t=n.sent())?[2,t]:(e=this.shortenUuid(this.generateUuid()),[4,this.asyncStorage.setItem("tools.quanta.id",e)]);case 2:return n.sent(),[2,e]}}))}))},t.prototype.loadOrCreateInstallDate=function(){return c(this,void 0,void 0,(function(){var t,e;return l(this,(function(n){switch(n.label){case 0:return[4,this.asyncStorage.getItem("tools.quanta.install")];case 1:return(t=n.sent())?[2,parseInt(t,10)]:(e=Math.floor(Date.now()/1e3),[4,this.asyncStorage.setItem("tools.quanta.install",e.toString())]);case 2:return n.sent(),[2,e]}}))}))},t.prototype.getUserData=function(){var t=this.getDeviceInfo(),e=this.getOSInfoSafe(),n=this.getBundleId(),r=this.isDebug()?1:0,i=this.getVersion(),s=this.systemLanguageProvider().replace("-","_"),o="";return o+="".concat(this._id),o+="".concat(d).concat(this.safe(t)),o+="".concat(d).concat(this.safe(e)),o+="".concat(d).concat(this.safe(n)),o+="".concat(d).concat(r),o+="".concat(d).concat(this.safe(i)),o+="".concat(d).concat(s),o+="".concat(d).concat(this._installDate)},t.prototype.getOSInfoSafe=function(){return this.getOSInfo().slice(0,25)},t.prototype.safe=function(t,e){return void 0===e&&(e=!1),e?t.replace(new RegExp(d,"g"),""):t.replace(new RegExp(d,"g"),"").replace(new RegExp(g,"g"),"")},t.prototype.stringForDouble=function(t){if(t>999999.99)return this.debugWarn("Value exceeds maximum allowed revenue of 999,999.99. Will be capped."),this.stringForDouble(999999.99);if(t<-999999.99)return this.debugWarn("Value is below minimum allowed revenue of -999,999.99. Will be capped."),this.stringForDouble(-999999.99);var e=t.toFixed(2);return e.endsWith(".00")?e.substring(0,e.length-3):e},t.prototype.enqueueEvent=function(t){return c(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return this._queue.push(t),[4,this.saveQueue()];case 1:return e.sent(),this._isProcessing?[3,3]:[4,this.processQueue()];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.processQueue=function(){return c(this,void 0,void 0,(function(){var t,e,n;return l(this,(function(r){switch(r.label){case 0:if(this._isProcessing||0===this._queue.length)return[2];this._isProcessing=!0,t=0,e=function(){var e,r,i;return l(this,(function(s){switch(s.label){case 0:return t>0?(e=500*Math.pow(1.5,t-1),[4,new Promise((function(t){return setTimeout(t,e)}))]):[3,2];case 1:s.sent(),s.label=2;case 2:return[4,n.sendEvent(n._queue[0])];case 3:return r=s.sent(),i=(Date.now()-n._queue[0].time.getTime())/36e5,r||t>=27||i>48?(n._queue.shift(),t=0,[4,n.saveQueue()]):[3,5];case 4:return s.sent(),[3,6];case 5:t++,s.label=6;case 6:return[4,new Promise((function(t){return setTimeout(t,100)}))];case 7:return s.sent(),[2]}}))},n=this,r.label=1;case 1:return this._queue.length>0?[5,e()]:[3,3];case 2:return r.sent(),[3,1];case 3:return this._isProcessing=!1,[2]}}))}))},t.prototype.sendEvent=function(t){return c(this,void 0,void 0,(function(){var e,n,r,i,s,o,a,u;return l(this,(function(c){switch(c.label){case 0:return c.trys.push([0,12,,13]),e="https://analytics-ingress.quanta.tools/ee/",n="",n+=t.appId,n+="".concat(d).concat(Math.floor(t.time.getTime()/1e3)),n+="".concat(d).concat(t.event),n+="".concat(d).concat(t.revenue),n+="".concat(d).concat(t.addedArguments),n+="".concat(d).concat(t.userData),t.abLetters&&(n+="".concat(d).concat(t.abLetters)),r={"Content-Type":"text/plain"},[4,this.asyncStorage.getItem("tools.quanta.ab.version")];case 1:return(i=c.sent())&&(r["X-AB-Version"]=i),[4,fetch(e,{method:"POST",headers:r,body:n})];case 2:if(!(s=c.sent()).ok)return[3,11];c.label=3;case 3:return c.trys.push([3,9,,10]),[4,s.text()];case 4:return(o=c.sent())?[4,this.asyncStorage.setItem("tools.quanta.ab",o)]:[3,6];case 5:c.sent(),this.setAbJson(o),c.label=6;case 6:return(a=s.headers.get("X-AB-Version"))?[4,this.asyncStorage.setItem("tools.quanta.ab.version",a)]:[3,8];case 7:c.sent(),c.label=8;case 8:return[3,10];case 9:return c.sent(),[3,10];case 10:return[2,!0];case 11:return[2,!1];case 12:return u=c.sent(),this.debugError("Failed to send event:",u),[2,!1];case 13:return[2]}}))}))},t.prototype.saveQueue=function(){return c(this,void 0,void 0,(function(){var t;return l(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,this.asyncStorage.setItem("tools.quanta.queue.tasks",JSON.stringify(this._queue))];case 1:return e.sent(),[3,3];case 2:return t=e.sent(),this.debugWarn("Failed to save queue to storage:",t),[3,3];case 3:return[2]}}))}))},t.prototype.loadQueue=function(){return c(this,void 0,void 0,(function(){var t,e,n;return l(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,this.asyncStorage.getItem("tools.quanta.queue.tasks")];case 1:return(t=r.sent())&&(e=JSON.parse(t),this._queue=e.map((function(t){return u(u({},t),{time:new Date(t.time)})}))),[3,3];case 2:return n=r.sent(),this.debugWarn("Failed to load queue from storage:",n),[3,3];case 3:return[2]}}))}))},t.prototype.setAbJson=function(t){this._abLetters=this.getAbLetters(t),this._abDict=this.getAbDict(t)},t.prototype.getAbLetters=function(t){var e,n;if(!t)return"";try{var r=JSON.parse(t),i="";try{for(var s=h(r),o=s.next();!o.done;o=s.next())for(var a=o.value,u="".concat(this._id,".").concat(a.name[a.name.length-1]||""),c=this.stringToNumber(u),l=0,p=0;p<a.variants.length;p++)if((l+=a.variants[p])>c){i+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"[p];break}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=s.return)&&n.call(s)}finally{if(e)throw e.error}}return i}catch(t){return this.debugWarn("Failed to parse AB test JSON:",t),""}},t.prototype.getAbDict=function(t){var e,n,r={};if(!t)return r;try{for(var i=JSON.parse(t),s=this._abLetters,o=0;o<i.length&&!(o>=s.length);o++){var a=i[o];try{for(var u=(e=void 0,h(a.name)),c=u.next();!c.done;c=u.next()){r[c.value.toLowerCase()]=s.substring(o,o+1)}}catch(t){e={error:t}}finally{try{c&&!c.done&&(n=u.return)&&n.call(u)}finally{if(e)throw e.error}}}return r}catch(t){return this.debugWarn("Failed to parse AB test JSON:",t),r}},t.prototype.stringToNumber=function(t){for(var e=0,n=0;n<t.length;n++){e=(e<<5)-e+t.charCodeAt(n),e|=0}return Math.abs(e%100)},t.prototype.generateUuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(t){var e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)}))},t.prototype.shortenUuid=function(t){for(var e=t.replace(/-/g,""),n=new Uint8Array(16),r=0;r<16;r++)n[r]=parseInt(e.substring(2*r,2*r+2),16);return btoa(String.fromCharCode.apply(null,Array.from(n))).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")},t.prototype.isValidUUID=function(t){return/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(t)},t.prototype.shouldLog=function(){return null===this.loggingEnabled?this.isDebug():this.loggingEnabled},t.prototype.enableLogging=function(){this.loggingEnabled=!0},t.prototype.disableLogging=function(){this.loggingEnabled=!1},t.prototype.debugLog=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.shouldLog()&&console.info.apply(console,f([],p(t),!1))},t.prototype.debugWarn=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.shouldLog()&&console.warn.apply(console,f([],p(t),!1))},t.prototype.debugError=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.shouldLog()&&console.error.apply(console,f([],p(t),!1))},t.prototype.checkClaimed=function(){return c(this,void 0,void 0,(function(){var t;return l(this,(function(e){switch(e.label){case 0:if(!this._appId)return[2];e.label=1;case 1:return e.trys.push([1,4,,5]),[4,fetch("https://quanta.tools/api/claimed/".concat(this._appId))];case 2:return(t=e.sent()).ok?[4,t.json()]:[2];case 3:return e.sent().unClaimed?(console.info("%c\n :@@@ +@@+ @@@ \n @@ @: @@ @ @ @@ \n @@ @@ @@ @@ @@ @ \n @ @@ = @@ @ @ @@ = +\n @@@@:@@ @@ @@ @ @= @@ @@ @@@ @@=@@ \n :@@ @ @@ @@ @@@ @@@@ @@ @@ \n @@@@@@ @@ @@@@ @@@ @@ @@@ @@ \n @ \\@@@@ \\@@@@@ \\@@@@@ \\@@@@@ \\@@@@ \n","font-family:monospace;font-weight:600"),console.info("Welcome to Quanta! 🥳"),console.info("Your analytics are fully set up."),console.info("See your first events coming in and attach this app to your Quanta account at"),console.info("https://quanta.tools/setup/".concat(this._appId)),console.info(""),console.info("Once your app is attached to an account, this welcome message won't show up anymore. 🚮"),[3,5]):[2];case 4:return e.sent(),[3,5];case 5:return[2]}}))}))},t}())),y="tools.quanta.sessions",b=function(){function t(){}return t.persistSessions=function(t){return c(this,void 0,void 0,(function(){var e,n;return l(this,(function(r){switch(r.label){case 0:if(0===t.length&&this.wasEmpty)return[2];r.label=1;case 1:return r.trys.push([1,3,,4]),e=JSON.stringify(t),[4,v.asyncStorage.setItem(y,e)];case 2:return r.sent(),this.wasEmpty=0===t.length,[3,4];case 3:return n=r.sent(),console.error("Failed to persist sessions:",n),[3,4];case 4:return[2]}}))}))},t.getStoredSessions=function(){return c(this,void 0,void 0,(function(){var t,e;return l(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,v.asyncStorage.getItem(y)];case 1:return(t=n.sent())?[2,JSON.parse(t)]:[2,[]];case 2:return e=n.sent(),console.error("Failed to retrieve stored sessions:",e),[2,[]];case 3:return[2]}}))}))},t.get=function(){return c(this,void 0,void 0,(function(){var t,e;return l(this,(function(n){switch(n.label){case 0:return null!==this.sessions?[2,this.sessions]:(t=this,[4,this.getStoredSessions()]);case 1:return t.sessions=n.sent(),[2,null!==(e=this.sessions)&&void 0!==e?e:{}]}}))}))},t.set=function(t){return c(this,void 0,void 0,(function(){return l(this,(function(e){switch(e.label){case 0:return this.sessions=t,JSON.stringify(t),[4,this.persistSessions(t)];case 1:return e.sent(),[2]}}))}))},t.sessions=null,t.wasEmpty=!1,t}();function m(t){if(Math.abs(t)>9999)return t.toExponential(2).replace(/e[\+\-]/,"e");if(0===t||Math.abs(t)<.001)return"0";var e=Math.floor(Math.abs(t)).toString().length,n=e>=4?2:Math.min(4-e,2);return t.toLocaleString("en-US",{style:"decimal",maximumFractionDigits:n}).replace(/,/g,"")}var w={current:{}},_={current:o.AppState.currentState},S={current:null},I={current:!0},x=function(t){return c(void 0,void 0,void 0,(function(){return l(this,(function(e){return b.set(Object.values(t).map((function(t){return{screenId:t.screenId,args:t.args,startTime:t.oldStartTime,duration:Math.max(A(t),5e3)}}))).catch(console.error),[2]}))}))},A=function(t){return t.pausedAtTime?t.pausedAtTime-t.startTime:Date.now()-t.startTime},E=function(){return c(void 0,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return S.current&&clearTimeout(S.current),I.current?[4,c(void 0,void 0,void 0,(function(){return l(this,(function(t){switch(t.label){case 0:return!v.getAppId()&&__DEV__&&console.warn("Make sure your Quanta AppId is set! Check https://github.com/Quanta-Tools/Quanta.js/tree/main/expo#set-appid for more information."),I.current=!1,o.AppState.addEventListener("change",k),[4,b.get()];case 1:return t.sent().forEach((function(t){D(t.startTime,t.screenId,t.duration,t.args)})),[2]}}))}))]:[3,2];case 1:t.sent(),t.label=2;case 2:return x(w.current),S.current=setInterval((function(){x(w.current)}),1e4),[2]}}))}))},D=function(t,e,n,r){v.logWithRevenue("view",0,u({screen:e,seconds:m(n/1e3)},r||{}),new Date(t))},O=function(t,e){void 0===e&&(e={});var n=v.generateUuid(),r=Date.now();return w.current[n]={screenId:t,args:e,startTime:r,oldStartTime:r},E().catch(console.error),n},T=function(t){var e=w.current[t];if(e){JSON.stringify(e);var n=A(e);n>=500&&D(e.oldStartTime,e.screenId,n,e.args),delete w.current[t],E().catch(console.error)}},q=function(){var t=Date.now();w.current=Object.fromEntries(Object.entries(w.current).map((function(e){var n=p(e,2),r=n[0],i=n[1];if(void 0===i.pausedAtTime)return[r,i];var s=i.pausedAtTime,o=function(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}(i,["pausedAtTime"]);return o.startTime+=t-s,[r,o]}))),E().catch(console.error)},k=function(t){var e,n=_.current;"active"===t?q():"active"===n&&(e=Date.now(),w.current=Object.fromEntries(Object.entries(w.current).map((function(t){var n=p(t,2),r=n[0],i=n[1];return void 0!==i.pausedAtTime?[r,i]:[r,u(u({},i),{pausedAtTime:e})]}))),E().catch(console.error)),_.current=t},V=function(){return{startScreenView:O,endScreenView:T}};exports.Quanta=v,exports.default=v,exports.useQuanta=function(t,e){var n=V(),r=n.startScreenView,i=n.endScreenView;s.useEffect((function(){var n=r(t,e);return function(){i(n)}}),[])},exports.useScreenTracking=V;
//# sourceMappingURL=index.cjs.js.map