import{debounce as t,DEFAULT_DEBOUNCE_TIME_MS as e}from"../../utils/debounce.js";import{processAnalyticsData as s}from"./utils.js";import{AnalyticsEventCategory as n}from"./events/AbstractAnalyticsEvent.js";import i from"../../utils/Storage.js";import{LIBRARY_BUNDLE_TYPE as o,LIBRARY_VERSION as r}from"../config.js";function a(t,e,s){return e in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}const c=e;class d{get checkoutAttemptId(){return this.capturedCheckoutAttemptId}async setUp({sessionId:t,checkoutStage:e,locale:n}={}){try{const i=this.storage.get(),c=function(t){if(!(null==t?void 0:t.timestamp))return!1;const e=Date.now()-9e5;return t.timestamp>e}(i),{applicationInfo:d,checkoutAttemptId:u}=s(this.analyticsData),h=c?i.id:u,l=function(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{},n=Object.keys(s);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(s).filter(function(t){return Object.getOwnPropertyDescriptor(s,t).enumerable}))),n.forEach(function(e){a(t,e,s[e])})}return t}({version:r,buildType:o,channel:"Web",platform:"Web",locale:n,referrer:window.location.href,screenWidth:window.screen.width,checkoutStage:e||"checkout",level:this.enabled?"all":"initial"},d&&{applicationInfo:d},t&&{sessionId:t},h&&{checkoutAttemptId:h});this.capturedCheckoutAttemptId=await this.service.requestCheckoutAttemptId(l),this.storage.set({id:this.capturedCheckoutAttemptId,timestamp:c?i.timestamp:Date.now()})}catch(t){this.enabled=!1,console.warn("Analytics: Error setting up",t)}}sendAnalytics(t){if(this.enabled)try{this.addEventToQueue(t)}catch(t){console.warn("Analytics: Error adding event to queue",t)}}async sendFlavor(t){if(this.capturedCheckoutAttemptId&&!this.isFlavorReported){this.isFlavorReported=!0;try{await this.service.reportIntegrationFlavor(t,this.capturedCheckoutAttemptId)}catch(t){console.warn("Analytics: Error reporting flavor",t)}}}flush(){this.sendEvents()}addEventToQueue(t){this.eventsQueue.add(t),t.getEventCategory()===n.info?this.debouncedSendInfoEvents():this.debouncedSendEvents()}async sendEvents(){if(!this.capturedCheckoutAttemptId||!this.enabled)return;const t={channel:"Web",platform:"Web",info:this.eventsQueue.infoEvents,errors:this.eventsQueue.errorEvents,logs:this.eventsQueue.logEvents};this.eventsQueue.clear();try{await this.service.sendEvents(t,this.capturedCheckoutAttemptId)}catch(t){console.warn("Analytics: Error sending events",t)}}constructor({service:e,eventQueue:s,enabled:n,analyticsData:o}){a(this,"analyticsData",void 0),a(this,"service",void 0),a(this,"eventsQueue",void 0),a(this,"storage",new i("checkout-attempt-id","sessionStorage")),a(this,"debouncedSendInfoEvents",void 0),a(this,"debouncedSendEvents",void 0),a(this,"enabled",!0),a(this,"capturedCheckoutAttemptId",void 0),a(this,"isFlavorReported",!1),this.service=e,this.eventsQueue=s,this.debouncedSendInfoEvents=t(this.sendEvents.bind(this),1e4),this.debouncedSendEvents=t(this.sendEvents.bind(this),c),void 0!==n&&(this.enabled=n),o&&(this.analyticsData=o)}}export{d as default};
//# sourceMappingURL=Analytics.js.map
