expo-quanta
Version:
Quanta.js/Expo: Expo SDK for Quanta Tools
3 lines (2 loc) • 25.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");function a(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var c=a(t),u=a(e),l=a(r),h=a(i),p=function(t,e){return p=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])},p(t,e)};var f=function(){return f=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},f.apply(this,arguments)};function d(t,e,n,r){return new(n||(n=Promise))((function(i,s){function o(t){try{c(r.next(t))}catch(t){s(t)}}function a(t){try{c(r.throw(t))}catch(t){s(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,a)}c((r=r.apply(t,e||[])).next())}))}function g(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(c){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,c])}}}function v(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 y(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 b(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 m="",w="",_=new(function(t){function e(){var e=t.apply(this,b([],y(arguments),!1))||this;return e._isTestFlight=!1,e}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}p(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(e,t),e.prototype.init=function(){var t,e,r=this;__DEV__&&console.warn("[Quanta] init called, appId from config:",null===(e=null===(t=n.expoConfig)||void 0===t?void 0:t.extra)||void 0===e?void 0:e.QuantaId),d(r,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return[4,this.detectTestFlight()];case 1:return t.sent(),[4,this.initializeAsync(void 0,!0)];case 2:return t.sent(),[2]}}))})).catch((function(t){__DEV__&&console.error("[Quanta] Init failed:",t)}))},e.prototype.detectTestFlight=function(){return d(this,void 0,void 0,(function(){var t;return g(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,h.getIosApplicationReleaseTypeAsync()];case 1:return t=e.sent(),this._isTestFlight=3===t||4===t,[3,3];case 2:return e.sent(),[3,3];case 3:return[2]}}))}))},e.prototype.makeAsyncStorage=function(){var t=this;return{setItem:function(e,n){return d(t,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return[4,c.setItemAsync(e,n)];case 1:return t.sent(),[2]}}))}))},getItem:function(e){return d(t,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return[4,c.getItemAsync(e)];case 1:return[2,t.sent()||""]}}))}))}}},e.prototype.setupUrlChangeListeners=function(){},e.prototype.handleUrlChange=function(){return d(this,void 0,void 0,(function(){return g(this,(function(t){return[2]}))}))},e.prototype.sendViewEvent=function(){return d(this,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return[4,this.logAsync("launch")];case 1:return t.sent(),[2]}}))}))},e.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},e.prototype.isServerSide=function(){return!1},e.prototype.systemLanguageProvider=function(){return l.getLocales()[0].languageTag},e.prototype.getBundleId=function(){return h.applicationId||""},e.prototype.getVersion=function(){var t;return h.nativeApplicationVersion||(null===(t=n.expoConfig)||void 0===t?void 0:t.version)||"1.0.0"},e.prototype.getDeviceInfo=function(){return u.modelName||"Expo Device"},e.prototype.getOSInfo=function(){var t,e;return"".concat(null!==(t=u.osName)&&void 0!==t?t:"Expo"," ").concat(null!==(e=u.osVersion)&&void 0!==e?e:"?")},e.prototype.getDebugFlags=function(){var t=0;return __DEV__&&(t|=1),u.isDevice||(t|=2),this._isTestFlight&&(t|=4),t},e.prototype.parseScriptTagAttributes=function(){},e}(function(){function t(){this._initialized=!1,this._initializing=!1,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 d(this,void 0,void 0,(function(){return g(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 d(this,void 0,void 0,(function(){return g(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 d(this,arguments,void 0,(function(t,e){var n,r,i,s,o,a,c,u;return void 0===e&&(e=!1),g(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(!this._initializing)return[3,10];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(),!t||this._appId?[3,9]:(this._initialized=!1,[4,this.initializeAsync(t,e)]);case 8:l.sent(),l.label=9;case 9:return[2];case 10:this._initializing=!0,n=function(){},this._initializingPromise=new Promise((function(t){n=t})),l.label=11;case 11:return l.trys.push([11,,22,23]),this.isServerSide()?(e||console.info("[Quanta] Skipping client sdk call on server."),[2]):(this.parseScriptTagAttributes(),r=this,null===(c=null!=t?t:this.getAppIdFromScriptTag())||void 0===c?[3,12]:(i=c,[3,14]));case 12:return[4,this.loadAppId()];case 13:i=l.sent(),l.label=14;case 14:return r._appId=null!==(u=i)&&void 0!==u?u:"",this._appId?[4,this.setAppId(this._appId)]:(e||this.debugWarn("No Quanta app ID provided. Analytics will not be sent."),[2]);case 15:return l.sent(),s=this,[4,this.loadOrCreateId()];case 16:return s._id=l.sent(),o=this,[4,this.loadOrCreateInstallDate()];case 17:return o._installDate=l.sent(),[4,this.asyncStorage.getItem("tools.quanta.ab")];case 18:return a=l.sent()||"",this._abLetters=this.getAbLetters(a),this._abDict=this.getAbDict(a),this.setupUrlChangeListeners(),[4,this.loadQueue()];case 19:return l.sent(),[4,this.processQueue()];case 20:return l.sent(),this.isDebug()&&this.checkClaimed().catch(console.error),this.debugLog("Quanta initialized"),this._initialized=!0,[4,this.maybeSendViewEvent()];case 21:return l.sent(),[3,23];case 22:return this._initializing=!1,n(),[7];case 23:return[2]}}))}))},t.prototype.maybeSendViewEvent=function(){return d(this,void 0,void 0,(function(){return g(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.log=function(t,e){void 0===e&&(e={}),this.logWithRevenue(t,0,e)},t.prototype.logAsync=function(t){return d(this,arguments,void 0,(function(t,e){return void 0===e&&(e={}),g(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 d(this,arguments,void 0,(function(t,e,n,r){var i,s,o,a,c,u,l,h,p,f,d;return void 0===e&&(e=0),void 0===n&&(n={}),void 0===r&&(r=new Date),g(this,(function(g){switch(g.label){case 0:return this.isServerSide()?(console.info("[Quanta] Skipping client sdk call on server."),[2]):this._initialized?[3,2]:[4,this.initializeAsync()];case 1:g.sent(),g.label=2;case 2:if(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=v(s),a=o.next();!a.done;a=o.next())c=a.value,u=this.safe(c,!1),l=this.safe(n[c],!1),i+="".concat(u).concat(w).concat(l).concat(w)}catch(t){f={error:t}}finally{try{a&&!a.done&&(d=o.return)&&d.call(o)}finally{if(f)throw f.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)),h=this.getUserData(),p=this.stringForDouble(e),[4,this.enqueueEvent({appId:this._appId,userData:h,event:this.safe(t),revenue:p,addedArguments:this.safe(i,!0),time:r,abLetters:this._abLetters})];case 3:return g.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 d(this,void 0,void 0,(function(){return g(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 d(this,void 0,void 0,(function(){var e;return g(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 d(this,void 0,void 0,(function(){var t,e;return g(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 d(this,void 0,void 0,(function(){var t,e;return g(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.getDebugFlags(),i=this.getVersion(),s=this.systemLanguageProvider().replace("-","_"),o="";return o+="".concat(this._id),o+="".concat(m).concat(this.safe(t)),o+="".concat(m).concat(this.safe(e)),o+="".concat(m).concat(this.safe(n)),o+="".concat(m).concat(r),o+="".concat(m).concat(this.safe(i)),o+="".concat(m).concat(s),o+="".concat(m).concat(this._installDate)},t.prototype.getOSInfoSafe=function(){return this.getOSInfo().slice(0,25)},t.prototype.isDebug=function(){return!!(1&this.getDebugFlags())},t.prototype.safe=function(t,e){return void 0===e&&(e=!1),e?t.replace(new RegExp(m,"g"),""):t.replace(new RegExp(m,"g"),"").replace(new RegExp(w,"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 d(this,void 0,void 0,(function(){return g(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 d(this,void 0,void 0,(function(){var t,e,n;return g(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 g(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 d(this,void 0,void 0,(function(){var e,n,r,i,s,o,a,c;return g(this,(function(u){switch(u.label){case 0:return u.trys.push([0,12,,13]),e="https://analytics-ingress.quanta.tools/ee/",n="",n+=t.appId,n+="".concat(m).concat(Math.floor(t.time.getTime()/1e3)),n+="".concat(m).concat(t.event),n+="".concat(m).concat(t.revenue),n+="".concat(m).concat(t.addedArguments),n+="".concat(m).concat(t.userData),t.abLetters&&(n+="".concat(m).concat(t.abLetters)),r={"Content-Type":"text/plain"},[4,this.asyncStorage.getItem("tools.quanta.ab.version")];case 1:return(i=u.sent())&&(r["X-AB-Version"]=i),[4,fetch(e,{method:"POST",headers:r,body:n})];case 2:if(!(s=u.sent()).ok)return[3,11];u.label=3;case 3:return u.trys.push([3,9,,10]),[4,s.text()];case 4:return(o=u.sent())?[4,this.asyncStorage.setItem("tools.quanta.ab",o)]:[3,6];case 5:u.sent(),this.setAbJson(o),u.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:u.sent(),u.label=8;case 8:return[3,10];case 9:return u.sent(),[3,10];case 10:return[2,!0];case 11:return[2,!1];case 12:return c=u.sent(),this.debugError("Failed to send event:",c),[2,!1];case 13:return[2]}}))}))},t.prototype.saveQueue=function(){return d(this,void 0,void 0,(function(){var t;return g(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 d(this,void 0,void 0,(function(){var t,e,n;return g(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 f(f({},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=v(r),o=s.next();!o.done;o=s.next())for(var a=o.value,c="".concat(this._id,".").concat(a.name[a.name.length-1]||""),u=this.stringToNumber(c),l=0,h=0;h<a.variants.length;h++)if((l+=a.variants[h])>u){i+="ABCDEFGHIJKLMNOPQRSTUVWXYZ"[h];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 c=(e=void 0,v(a.name)),u=c.next();!u.done;u=c.next()){r[u.value.toLowerCase()]=s.substring(o,o+1)}}catch(t){e={error:t}}finally{try{u&&!u.done&&(n=c.return)&&n.call(c)}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"undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"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,b([],y(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,b([],y(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,b([],y(t),!1))},t.prototype.checkClaimed=function(){return d(this,void 0,void 0,(function(){var t;return g(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}()));_.init();var I="tools.quanta.sessions",S=function(){function t(){}return t.persistSessions=function(t){return d(this,void 0,void 0,(function(){var e,n;return g(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,_.asyncStorage.setItem(I,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 d(this,void 0,void 0,(function(){var t,e;return g(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,_.asyncStorage.getItem(I)];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 d(this,void 0,void 0,(function(){var t,e;return g(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 d(this,void 0,void 0,(function(){return g(this,(function(e){switch(e.label){case 0:return this.sessions=t,[4,this.persistSessions(t)];case 1:return e.sent(),[2]}}))}))},t.sessions=null,t.wasEmpty=!1,t}();function x(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 A={current:{}},D={current:o.AppState.currentState},E={current:null},O={current:!0},T=function(t){return d(void 0,void 0,void 0,(function(){return g(this,(function(e){return S.set(Object.values(t).map((function(t){return{screenId:t.screenId,args:t.args,startTime:t.oldStartTime,duration:Math.max(q(t),5e3)}}))).catch(console.error),[2]}))}))},q=function(t){return t.pausedAtTime?t.pausedAtTime-t.startTime:Date.now()-t.startTime},V=function(){return d(void 0,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return E.current&&clearInterval(E.current),O.current?[4,d(void 0,void 0,void 0,(function(){return g(this,(function(t){switch(t.label){case 0:return!_.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."),O.current=!1,o.AppState.addEventListener("change",P),[4,S.get()];case 1:return t.sent().forEach((function(t){k(t.startTime,t.screenId,t.duration,t.args)})),[2]}}))}))]:[3,2];case 1:t.sent(),t.label=2;case 2:return T(A.current),E.current=setInterval((function(){T(A.current)}),1e4),[2]}}))}))},k=function(t,e,n,r){_.logWithRevenue("view",0,f({screen:e,seconds:x(n/1e3)},r||{}),new Date(t))},z=function(t,e){void 0===e&&(e={});var n=_.generateUuid(),r=Date.now();return A.current[n]={screenId:t,args:e,startTime:r,oldStartTime:r},V().catch(console.error),n},F=function(t){var e=A.current[t];if(e){var n=q(e);n>=500&&k(e.oldStartTime,e.screenId,n,e.args),delete A.current[t],V().catch(console.error)}},L=function(){var t=Date.now();A.current=Object.fromEntries(Object.entries(A.current).map((function(e){var n=y(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]}))),V().catch(console.error)},P=function(t){var e,n=D.current;"active"===t?L():"active"===n&&(e=Date.now(),A.current=Object.fromEntries(Object.entries(A.current).map((function(t){var n=y(t,2),r=n[0],i=n[1];return void 0!==i.pausedAtTime?[r,i]:[r,f(f({},i),{pausedAtTime:e})]}))),V().catch(console.error)),D.current=t},Q=function(){return{startScreenView:z,endScreenView:F}};exports.Quanta=_,exports.default=_,exports.useQuanta=function(t,e){var n=Q(),r=n.startScreenView,i=n.endScreenView;s.useEffect((function(){var n=r(t,e);return function(){i(n)}}),[])},exports.useScreenTracking=Q;
//# sourceMappingURL=index.cjs.js.map