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