import{M as d,a as c,b as l,c as u}from"./fluent-player.js?ver=1.3.1";const o=3e4;class y{constructor(i,s,e={}){this.player=i,this.mediaId=s,this.nonce=(e==null?void 0:e.nonce)||"";const t=e==null?void 0:e.watchTracker;this._ownsWatchTracker=!t,this.watchTracker=t||new d(i,{completionThreshold:100,mediaId:s,segmentUpdateInterVal:5}),this.dirty=!1,this.flushTimer=null,this.flushing=!1,this.lastSentPercentage=0,this.lastSentDuration=0,this._onPlay=this._handlePlay.bind(this),this._onPause=this._handlePause.bind(this),this._onEnded=this._handleEnded.bind(this),this._onVisibilityChange=this._handleVisibilityChange.bind(this),this._onBeforeUnload=this._handleBeforeUnload.bind(this),this._attachListeners()}_attachListeners(){this.player.addEventListener("play",this._onPlay),this.player.addEventListener("pause",this._onPause),this.player.addEventListener("ended",this._onEnded),document.addEventListener("visibilitychange",this._onVisibilityChange),window.addEventListener("beforeunload",this._onBeforeUnload)}_handlePlay(){this.dirty=!0,this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null)}_handlePause(){this.scheduleFlush()}_handleEnded(){this.dirty=!0,Promise.resolve().then(()=>this.flush())}_handleVisibilityChange(){document.visibilityState==="hidden"&&this._finalizeAndFlush(!0)}_handleBeforeUnload(){this._finalizeAndFlush(!0)}_finalizeAndFlush(i){this.dirty&&(this.watchTracker&&(this.watchTracker.endTime=this.watchTracker.getPlayerCurrentTime(),this.watchTracker.addWatchedSegment()),this.flush(i))}scheduleFlush(){this.flushTimer&&clearTimeout(this.flushTimer),this.flushTimer=setTimeout(()=>{this.flush()},o)}flush(i=!1){var r;if(!this.dirty||this.flushing)return;if(!this.mediaId||!this.nonce){!this.nonce&&!this._warnedMissingNonce&&(this._warnedMissingNonce=!0,console.debug("FluentPlayer: analytics beacon skipped — no per-media nonce for media",this.mediaId));return}if(this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.flushing=!0,!((r=this.player)!=null&&r.duration)){this.flushing=!1;return}let s=Math.round(this.watchTracker.getWatchedPercentage()),e=Math.round(this.watchTracker.getTotalWatchedTime());if(s<=this.lastSentPercentage&&e<=this.lastSentDuration){this.flushing=!1,this.dirty=!1;return}const t={nonce:this.nonce,media_id:this.mediaId,duration:e,percentage:s},h=c();if(!h){this.flushing=!1;return}this.dirty=!1;const n=()=>{this.lastSentPercentage=s,this.lastSentDuration=e};if(i&&navigator.sendBeacon){const a=l("fluent_player_track_event",t);navigator.sendBeacon(h,a)?n():(this.dirty=!0,this._keepaliveFlush(h,t,n)),this.flushing=!1}else u("fluent_player_track_event",t).then(a=>{a&&a.success?n():this.dirty=!0}).catch(()=>{this.dirty=!0}).finally(()=>{this.flushing=!1})}_keepaliveFlush(i,s,e){try{const t=l("fluent_player_track_event",s);fetch(i,{method:"POST",body:t,keepalive:!0}).then(h=>h.json()).then(h=>{h&&h.success&&(e(),this.dirty=!1)}).catch(()=>{})}catch{}}destroy(){this.dirty&&this._finalizeAndFlush(!0),this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null),this.player.removeEventListener("play",this._onPlay),this.player.removeEventListener("pause",this._onPause),this.player.removeEventListener("ended",this._onEnded),document.removeEventListener("visibilitychange",this._onVisibilityChange),window.removeEventListener("beforeunload",this._onBeforeUnload),this.watchTracker&&(this._ownsWatchTracker&&this.watchTracker.destroy(),this.watchTracker=null)}}export{y as default};
