import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var Dz="apiUrl config is missing",Ez=function(a,b){a.j=a.j.filter(c=>c!==b)},Fz=function(a){let b=0>a;a=Math.abs(a);let c=a>>>0;a=Math.floor((a-c)/4294967296);a>>>=0;b&&(a=~a>>>0,c=(~c>>>0)+1,4294967295<c&&(c=0,a++,4294967295<a&&(a=0)));return[c,a]},Kz=function(a,b,c,d){if(!(null===c||void 0===c||typeof c===_.t&&0===c||typeof c===_.Nf&&0===c.length||Array.isArray(c)&&0===c.length))if(Array.isArray(c)){Gz(a,b<<3|2);b=new Hz;for(let e=0;e<c.length;e++)d(b,c[e]);Iz(a,b.length());Jz(a,b.end())}else Gz(a,
b<<3|0),d(a,c)},Iz=function(a,b){if(0<=b)Gz(a,b);else{for(let c=0;9>c;c++)a.a.push(b&127|128),b>>=7;a.a.push(1)}},Lz=function(a,b,c){Kz(a,b,c,(d,e)=>{Iz(d,e)})},Gz=function(a,b){for(;127<b;)a.a.push(b&127|128),b>>>=7;a.a.push(b)},Nz=function(a,b,c){Kz(a,b,c,(d,e)=>{if(0<=e){{const [f,g]=Fz(e);Mz(d,f,g)}}else{{const [f,g]=Fz(e);Mz(d,f,g)}}})},Oz=function(a,b,c,d){if(!(null===c||void 0===c||typeof c===_.t&&0===c||typeof c===_.Nf&&0===c.length||Array.isArray(c)&&0===c.length))if(Array.isArray(c))for(let e=
0;e<c.length;e++){Gz(a,b<<3|2);const f=new Hz;d(f,c[e]);Iz(a,f.length());Jz(a,f.end())}else Gz(a,b<<3|2),b=new Hz,d(b,c),Iz(a,b.length()),Jz(a,b.end())},Pz=function(a,b,c){Oz(a,b,c,(d,e)=>{for(let f=0;f<e.length;f++){let g=e.charCodeAt(f);if(128>g)d.a.push(g);else if(2048>g)d.a.push(g>>6|192),d.a.push(g&63|128);else if(65536>g)if(55296<=g&&56319>=g&&f+1<e.length){let h=e.charCodeAt(f+1);56320<=h&&57343>=h&&(g=1024*(g-55296)+h-56320+65536,d.a.push(g>>18|240),d.a.push(g>>12&63|128),d.a.push(g>>6&63|
128),d.a.push(g&63|128),f++)}else d.a.push(g>>12|224),d.a.push(g>>6&63|128),d.a.push(g&63|128)}})},Jz=function(a,b){a.a.push.apply(a.a,b)},Qz=function(a,b,c){Oz(a,b,c,(d,e)=>{Jz(d,e.a())})},Rz=function(a,b,c){Oz(a,b,c,(d,e)=>{Jz(d,e)})},Mz=function(a,b,c){for(;0<c||127<b;)a.a.push(b&127|128),b=(b>>>7|c<<25)>>>0,c>>>=7;a.a.push(b)},Tz=function(a){const b=new Sz;b.h=Math.floor(a/1E3);b.f=a%1E3*1E6;return b},Xz=function(a){return _.x(function*(){if(!a.j&&!a.f){0===a.a.length&&Uz(a,new Vz);var b=()=>
{};a.f=new Promise(c=>{b=c});try{const c=new Wz;c.f=a.o;c.events.push(...a.a);const d=[...a.a],e=yield fetch(a.l,{method:_.cb,headers:{"Content-Type":"application/proto"},body:new Uint8Array(c.a())});if(e.ok)a.a=a.a.filter(f=>!d.includes(f));else throw Error("An error occurred while sending telemetry events to the API, "+`status='${e.status}' statusText='${e.statusText}'`);}finally{b(),a.f=null}}})},$z=function(a,b,c,d,e,f,g){const h=new Yz;h.headers=[new Zz("CL-BROWSER-NAME",b),new Zz("CL-BROWSER-VERSION",
c),new Zz("CL-OS-FAMILY",d),new Zz("CL-OS-VERSION",e),new Zz("CL-SDK-NAME",f),new Zz("CL-SDK-NAME",f),new Zz("CL-SDK-VERSION",g)];Uz(a,h)},Uz=function(a,b){if(!a.j){var c=new aA(a.w);c.sessionId=a.A;{var d=Date.now();const e=new bA;e.h=Math.floor(d/1E3);e.f=d%1E3*1E6;d=e}c.time=d;c.h=b.getId();c.f=new Uint8Array(b.a());a.a.push(c)}},cA=function(a){return _.x(function*(){a.h&&(clearInterval(a.h),a.h=null);a.f&&(yield a.f);0<a.a.length&&(yield Xz(a),a.a=[]);a.j=!0})},dA=function(a){_.x(function*(){a.g.info("Destroy the session and stop sending events.");
_.Dh(a.a);a.Z=!1;const b=a.m.getNetworkEngine();b.me(a.l);b.xd(a.h);Ez(b,a.h);a.f&&(yield cA(a.f),a.f=null)})},lA=function(a){switch(a){case _.To:return eA;case _.Uo:return fA;case _.Po:return gA;case _.Wo:return hA;case _.Vo:return iA;case _.Lo:return jA;case _.Mo:return kA;default:return null}},mA=function(a,b){a.g.debug("Sending event",b);Uz(a.f,b)},uA=function(a,b){switch(a){case 1:return nA;case 2:return b===_.w?oA:b===_.p?pA:b===_.H?qA:b===_.be?rA:sA;case 3:return tA;default:return sA}};var Hz=class{constructor(){this.a=[]}length(){return this.a.length}end(){const a=this.a;this.a=[];return a}};var vA=class{constructor(a,b,c,d){this.downloadId=a;this.h=b;this.duration=c;this.j=d;this.f=[]}a(){const a=new Hz;Qz(a,1,this.downloadId);Lz(a,2,this.h);Qz(a,3,this.duration);Lz(a,4,this.j);Qz(a,5,this.f);return a.end()}getId(){return 4}};var wA=class{constructor(a,b){this.f=a;this.url=b;this.downloadId=null;this.headers=[];this.size=this.duration=null}a(){const a=new Hz;Lz(a,1,this.f);Qz(a,2,this.downloadId);Pz(a,3,this.url);Qz(a,4,this.headers);Pz(a,5,null);Qz(a,6,null);Qz(a,7,this.duration);Nz(a,8,this.size);return a.end()}getId(){return 2}};var xA=class{constructor(a,b){this.h=a;this.f=b}a(){const a=new Hz;Lz(a,1,this.h);Rz(a,2,this.f);return a.end()}};var yA=class{constructor(a){this.cause=a;this.f=this.message=this.severity=this.errorCode=null}a(){const a=new Hz;Lz(a,1,this.errorCode);Lz(a,2,this.severity);Pz(a,3,this.message);Pz(a,4,this.f);Qz(a,5,this.cause);return a.end()}getId(){return 7}};var Zz=class{constructor(a,b){this.key=a;this.value=b}a(){const a=new Hz;Pz(a,1,this.key);Pz(a,2,this.value);return a.end()}};var zA=class{constructor(){this.cause=this.severity=this.code=null}a(){const a=new Hz;Lz(a,1,this.code);Lz(a,2,this.severity);Qz(a,3,this.cause);return a.end()}getId(){return 12}};var AA=class{constructor(){this.data=null}a(){const a=new Hz;Rz(a,1,this.data);return a.end()}};var BA=class extends AA{constructor(){super();var a=new Uint8Array(16);crypto.getRandomValues(a);a[6]=a[6]&15|64;a[8]=a[8]&63|128;this.data=a}toString(){const a=[];for(let b=0;256>b;++b)a.push((b+256).toString(16).slice(1));return a[this.data[0]]+a[this.data[1]]+a[this.data[2]]+a[this.data[3]]+"-"+a[this.data[4]]+a[this.data[5]]+"-"+a[this.data[6]]+a[this.data[7]]+"-"+a[this.data[8]]+a[this.data[9]]+"-"+a[this.data[10]]+a[this.data[11]]+a[this.data[12]]+a[this.data[13]]+a[this.data[14]]+a[this.data[15]]}};var Yz=class{constructor(){this.headers=[]}a(){const a=new Hz;Qz(a,1,this.headers);return a.end()}getId(){return 1}};var Vz=class{constructor(){}a(){return(new Hz).end()}getId(){return 6}};var aA=class{constructor(a){this.j=a;this.f=this.h=this.time=this.sessionId=null}a(){const a=new Hz;Qz(a,1,this.j);Qz(a,2,this.sessionId);Qz(a,3,this.time);Nz(a,4,null);Pz(a,5,null);Lz(a,6,this.h);Rz(a,7,this.f);return a.end()}};var Wz=class{constructor(){this.f=null;this.events=[]}a(){const a=new Hz;Rz(a,1,this.f);Qz(a,2,this.events);return a.end()}};var bA=class{constructor(){this.f=this.h=null}a(){const a=new Hz;Nz(a,1,this.h);Lz(a,2,this.f);return a.end()}};var Sz=class{constructor(){this.f=this.h=null}a(){const a=new Hz;Nz(a,1,this.h);Lz(a,2,this.f);return a.end()}};var CA=class{constructor(a,b,c,d,e,f,g,h,k){k=void 0===k?2500:k;this.o=a;this.l=h;this.w=new BA;this.A=new BA;this.a=[];this.h=setInterval(()=>Xz(this),k);this.f=null;this.j=!1;$z(this,b,c,d,e,f,g)}};var DA=class{constructor(){this.reason=this.currentState=this.previousState=null}a(){const a=new Hz;Lz(a,1,this.previousState);Lz(a,2,this.currentState);Lz(a,3,this.reason);Qz(a,4,null);Qz(a,5,null);Qz(a,6,null);return a.end()}getId(){return 5}};var HA=class extends _.Pv{constructor(){super();this.m=null;this.Z=this.j=!1;this.a=new _.Nt;this.f=null;this.g=new _.S("clpp.sessions.SessionsPlugin");this.l=this.F.bind(this);this.h=this.C.bind(this)}onPlayerCreated(a){this.m=a}onContentWillLoad(a){var b=a.getConfiguration().sessions;(b=!(!b||typeof b!==_.Ce))||this.g.debug("Sessions plugin is loaded but not configured. Will do nothing.");if(this.j=b)b=a.getConfiguration().sessions,b.apiUrl?(this.g.debug(_.ra),this.g.info("Create a session and start sending events."),
a=_.Tk(),this.f=new CA(new Uint8Array([]),a.browser,a.browserVersion.name,a.os,a.osVersion.name,"prestoplay-web",_.ea,b.apiUrl),this.a.on(this.m,_.Kf,this.B.bind(this)),this.a.on(this.m,_.Cf,this.A.bind(this)),this.a.on(this.m,_.Bf,this.w.bind(this)),this.a.on(this.m,_.q,this.o.bind(this)),a=this.m.getNetworkEngine(),a.Jd(this.l),a.Gc(this.h),a.j.push(this.h)):(this.g.warn(Dz),a.onError(new _.I(1,9,15E3,Dz)))}onPlayerWillRelease(){dA(this)}id(){return"sessions"}B(a){if(!this.Z){a=a.detail;var b=a.currentState,
c=new DA;c.previousState=lA(a.previousState);c.currentState=lA(b);mA(this,c)}}A(){if(!this.Z){var a=new DA;a.previousState=lA(this.m.getState());a.currentState=EA;mA(this,a);this.Z=!0}}w(){if(this.Z){var a=new DA;a.previousState=EA;a.currentState=lA(this.m.getState());mA(this,a);this.Z=!1}}o(a){a=a.detail;var b=new zA;b=new yA(new xA(b.getId(),new Uint8Array(b.a())));b.errorCode=a.code;b.message=a.message;b.f=a.stack;b.severity=a.severity===_.G?FA:GA;mA(this,b)}F(a){const b=new BA;a.downloadId=b;
const c=new wA(uA(a.type,a.contentType),a.uris[0]);c.downloadId=b;c.headers=Object.keys(a.headers).map(d=>new Zz(d,a.headers[d]));mA(this,c)}C(a){var b=a.request.downloadId;b?(b=new vA(b,a.data.byteLength,Tz(a.timeMs||0),a.status||-1),b.f=Object.keys(a.headers).map(c=>new Zz(c,a.headers[c])),mA(this,b)):this.g.warn("Download ID is missing on download completed event.")}};_.Ir(new class{create(){return new HA}});var GA=1,FA=2;var eA=0,fA=2,kA=3,EA=5,gA=6,hA=8,iA=10,jA=11;var nA=0,oA=1,pA=2,qA=3,rA=4,tA=5,sA=9;};f.call(g, window);