import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var eB=function(a){switch(a.type){case 1:return bB;case 2:return cB;case 3:return dB}},gB=function(a){const b=Object.keys(a).find(c=>fB.includes(c.toLowerCase()));return void 0!==b?a[b]:void 0},iB=function(a){let b={debug:!0,data:hB(a)};a.f&&a.f.muxOptionsOverride&&_.$g(a.f.muxOptionsOverride)&&(b=_.Wg(b,a.f.muxOptionsOverride));b=_.Wg(b,{getPlayheadTime:()=>1E3*a.m.getPosition(),getStateData:()=>{{a.g.debug("getStateData()");var c=a.m.getLoadedSource(),d=a.m.getTrackManager();const f=
a.m.getSurface();var e={player_is_paused:a.m.isPaused(),player_is_fullscreen:f.isFullscreen(),video_source_duration:1E3*a.m.getDuration(),player_autoplay_on:!!a.m.getConfiguration().autoplay};if(c){const k=new _.mm(c.url);e.video_source_url=_.Kj(k);e.video_source_mime_type=c.type;c.name&&(e.video_title=c.name)}if(c=f.getContainer())e.player_width=c.offsetWidth,e.player_height=c.offsetHeight;(c=a.m.getSurface().getMedia())?c={videoWidth:c.videoWidth,videoHeight:c.videoHeight}:(c=a.m.getTrackManager().na(),
c={videoWidth:c.width,videoHeight:c.height});const {videoWidth:g,videoHeight:h}=c;a.g.debug(`Video dimensions: ${g}x${h}`);g&&h&&(e.video_source_height=g,e.video_source_width=h);(d=d.qa())&&d.language&&(e.player_language_code=d.language)}return e}});a.g.debug("Mux SDK options",b);return b},jB=function(a){_.Dh(a.a);a.j.xd(a.F);a.j.me(a.C)},hB=function(a){const b={env_key:a.f.envKey,video_title:a.o&&a.o.name||"",player_name:_.qa,player_software_name:_.qa,player_software_version:_.ea,player_mux_plugin_name:"CastlabsPlayer built-in Mux Plugin",
player_mux_plugin_version:_.ea};(a=a.m.getConfiguration().viewerId)&&(b.viewer_user_id=a);return b},kB=function(a){a.a.on(a.m,_.Zf,()=>{const b={player_playhead_time:1E3*a.m.getPosition()};a.emit(_.Zf,b)});a.a.on(a.m,_.Re,()=>a.emit(_.Re));a.a.on(a.m,_.Kc,a.G.bind(a));a.a.on(a.m,_.Kf,a.I.bind(a));a.a.on(a.m,_.jg,()=>a.emit(_.Cf));a.a.on(a.m,_.ig,()=>a.emit(_.Bf));a.a.on(a.m,_.q,a.H.bind(a));a.a.on(a.m,_.Rb,()=>a.emit("adbreakstart"));a.a.on(a.m,_.Sb,()=>a.emit("adbreakend"));a.a.on(a.m,_.jc,()=>{a.emit("adplay");
a.emit("adplaying")});a.a.on(a.m,_.cc,()=>a.emit("adpause"));a.a.on(a.m,_.ec,()=>{a.emit("adplay");a.emit("adplaying")});a.a.on(a.m,_.lc,()=>a.emit("adended"));a.a.on(a.m,_.Wb,()=>a.emit("adfirstquartile"));a.a.on(a.m,_.bc,()=>a.emit("admidpoint"));a.a.on(a.m,_.mc,()=>a.emit("adthirdquartile"));a.j.Gc(a.F);a.j.Jd(a.C);a.a.on(a.m,_.q,b=>{if((b=b.detail)&&1===b.category){var c=b.data;c=c&&c.request;if(void 0!==c&&!0===(1===c.type||2===c.type||3===c.type))if(7001===b.code){b=c;c=b.requestStartTimestamp;
var d=new _.mm(b.uris[b.uriIndex]);b={request_start:c,request_type:eB(b),request_hostname:d.a};a.g.debug("onRequestCanceled()","Track event: requestcanceled",b);a.emit("requestcanceled",b)}else{{d=c.requestStartTimestamp;const f=new _.mm(c.uris[c.uriIndex]);var e=b.data;e=void 0===e?-1:void 0!==e.response?e.response.status:void 0!==e.status?e.status:-1;b={request_start:d,request_type:eB(c),request_hostname:f.a,request_error:`${eB(c)}${"LoadError"}`,request_error_code:e,request_error_text:b.toString()};
a.g.debug("onRequestFailed()","Track event: requestfailed",b);a.emit("requestfailed",b)}}}})},lB=function(a,b){a.trigger(new _.F(_.q,{detail:b}))},mB=function(a){return!_.B(a)&&typeof a.init===_.Yd&&typeof a.emit===_.Yd},bB="manifest",cB="media",dB="encryption",fB=["x-request-id","cf-ray","x-amz-cf-id","x-akamai-request-id"];var nB=class{constructor(a,b){this.m=a;this.o=this.f=null;this.B=("000000"+(Math.random()*Math.pow(36,6)<<0).toString(36)).slice(-6);this.a=new _.Nt;this.g=b;this.A=this.l=this.h=!1;this.w=null;this.j=a.getNetworkEngine();this.F=this.K.bind(this);this.C=this.J.bind(this)}initialize(a,b,c){this.f=a;this.o=b;this.w=c;if(!this.h){try{this.h=!0,this.w.init(this.B,iB(this))}catch(d){throw this.h=!1,jB(this),new _.I(1,9,9302,null,d);}this.emit("playerready")}this.l&&!this.A&&(a=hB(this),this.emit("videochange",
a),this.l=!1);kB(this)}release(){this.g.debug("release()");this.l?this.g.debug(_.ia):(jB(this),this.l=!0,this.o=this.f=null)}destroy(){this.g.debug(_.Ad);this.A?this.g.debug(_.ha):(this.A=!0,this.emit("destroy"))}emit(a,b){if(null===this.w)throw new _.I(1,9,9301);this.w.emit(this.B,a,b)}G(a){this.g.debug(_.De);var b=a.detail;a=this.m.getTrackManager();b&&b.bandwidth&&(b=b.bandwidth,(a=a.Ma())&&a.bandwidth&&(b+=a.bandwidth),this.emit("renditionchange",{video_source_bitrate:b}))}I(a){this.g.debug(_.Ie);
switch(a.detail.currentState){case _.Uo:this.m.getConfiguration().autoplay&&this.emit(_.Re);break;case _.Wo:this.emit(_.bf);break;case _.Vo:this.emit(_.Ne);break;case _.Lo:this.emit(_.Sd)}}H(a){this.g.debug(_.Fe);if(this.h&&(a=a.detail||null))if(10003===a.code)this.emit("aderror");else if(a.severity===_.G){var b=a.message||"Player error";if(1===a.category||6100===a.code||6007===a.code)b="Download error",6100===a.code?b="Certificate request error":6007===a.code&&(b="License request error"),b=`${b} castlabs-error-data:${JSON.stringify(a.data)}`;
this.emit(_.q,{player_error_code:a.code,player_error_message:b})}}K(a){var b=a.request;if(b=void 0!==b&&!0===(1===b.type||2===b.type||3===b.type))b=a.status,b=void 0!==b&&200<=b&&299>=b;if(b){{b=a.request;const e=a.headers,f=b.bytesLoaded,g=b.requestStartTimestamp;var c=b.startTime,d=b.endTime;const h=g+a.responseStartMs;a=g+a.timeMs;c=c&&d?d-c:void 0;d=new _.mm(b.uris[b.uriIndex]);a={request_start:g,request_type:eB(b),request_hostname:d.a,request_id:gB(e),request_bytes_loaded:f,request_response_start:h,
request_response_end:a,request_response_headers:e,request_media_duration:c,request_url:_.Kj(d)};this.g.debug("onRequestCompleted()","Track event: requestcompleted",a);this.emit("requestcompleted",a)}}}J(a){a.requestStartTimestamp=Date.now()}};var oB=class extends _.Pv{constructor(){super();this.f=this.m=null;this.a=!1;this.g=new _.S("clpp.muxdata")}onPlayerCreated(a){this.m=a;this.f=new nB(a,this.g)}onContentWillLoad(a,b){var c=a.getConfiguration();c=_.Xg(c.muxdata);try{var d=[];typeof c===_.Ce&&(c.envKey||d.push("envKey"));if(0<d.length)throw new _.I(1,9,9300,{missingKeys:d});{const f=a.getConfiguration();d=null;if(this.a=!_.B(f.muxdata)&&typeof f.muxdata===_.Ce){const g=f.muxdata;d=mB(g.muxLib)?g.muxLib:window.mux;if(!mB(d)){this.g.warn("Mux SDK is not loaded. Will do nothing.");
this.a=!1;const h=new _.I(1,9,9301);lB(a,h)}}else this.g.debug("Mux plugin is loaded but not configured. Will do nothing.");var e=d}null!==e&&this.f.initialize(c,b,e)}catch(f){this.a=!1,lB(a,f)}}onContentLoaded(){}onPlayerWillDestroy(){this.a&&(this.f.destroy(),this.m=this.f=null)}onPlayerWillRelease(){this.a&&this.f.release()}id(){return"mux"}emit(a,b){try{this.f.emit(a,b)}catch(c){throw null!==this.m&&lB(this.m,c),c;}}};_.z("clpp.muxdata.MuxDataPlugin",oB);oB.prototype.emit=oB.prototype.emit;
oB.Id="mux";_.Ir(new class{create(){return new oB}});};f.call(g, window);