import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var Ly="Thumbnail URL is mandatory",My="Wrong gridSize format",Ny="clpp.thumbnails",Py=function(a,b){b.reduce((c,d)=>{let e=d,f=!1;typeof d===_.Ce&&(e=d.step,f=d.preloadWhileBuffering);return c.then(()=>Oy(a,0,e,!!f))},Promise.resolve())},Oy=function(a,b,c,d){if(a.w||b>=a.a.length)return Promise.resolve();const e=b+c;return Qy(a,d).then(()=>a.a[b].load()).then(()=>Oy(a,e,c,d))},Qy=function(a,b){return new Promise(c=>{if(b||a.player.getState()!==_.Po)c();else a.player.one(_.Mc,()=>{c()})})},
Ry=function(a){const b=a.a.byteLength;a.f.forEach(c=>{c.offset+c.length<b&&c.Tc.resolve()})},Uy=function(a,b){return Sy(a,b).then(c=>new Ty(c,b.time,b.duration))},Sy=function(a,b){return b.load().then(()=>{const c=_.K(a.a,b.offset,b.length);return"data:image/jpeg;base64,"+_.Hl(c)})},Vy=function(a){a=a.split(/\.|:/).map(parseFloat);let b=0,c;const d={3:.001,2:1,1:60,0:3600};for(c=a.length-1;0<=c;c--)b+=a[c]*d[c];return b},Wy=function(a,b){let c=null;for(const d of a)d.startTime<=b&&(c=d);return c},
Xy=function(a,b){const c=a.horizontalTiles||b.horizontalTiles||1,d=a.verticalTiles||b.verticalTiles||1,e=c*d;return{od:c,uf:d,fg:e,durationPerTile:a.durationPerTile||b.durationPerTile||(a.endTime-a.startTime)/e}},Yy=function(a){return a.some(b=>0<b.thumbStreams.length)},fz=function(a){return a?a===Zy||a===$y?new az:a===bz?new cz:a===dz?new ez:null:null},gz=function(a){if(!a)return null;a=new _.mm(a);return a.path.match(/.*\.vtt$/i)?bz:a.path.match(/.*\.bif$/i)?dz:a.path.match(/.*\.(png|jpg|jpeg)$/i)?
Zy:null};var hz=class{constructor(){this.log=new _.S(Ny);this.player=null;this.a=[];this.w=!1}init(a,b){this.player=a;a.one(_.sf,()=>{this.w=!0});a.isLive()||this.l().then(()=>{b&&b.preload&&Py(this,b.preload)})}};var Ty=class{constructor(a,b,c,d,e,f,g,h,k,l,m,n){d=void 0===d?0:d;e=void 0===e?0:e;f=void 0===f?0:f;g=void 0===g?0:g;h=void 0===h?1:h;k=void 0===k?1:k;l=void 0===l?0:l;m=void 0===m?0:m;n=void 0===n?new Image:n;this.time=b;this.src=a;this.duration=c;this.x=d;this.y=e;this.width=f;this.height=g;this.sa=n;this.ih=h;this.ri=k;this.Kh=l;this.Lh=m;this.Ie=null}raw(){return this.sa.cloneNode(!1)}element(a,b){let c=this.width,d=this.height;const e=this.width/this.height;void 0!==a&&void 0===b&&(b=0);null!==
a&&void 0!==a&&(c=a);null!==b&&void 0!==b&&(d=b);c||(c=Math.ceil(d*e));d||(d=Math.ceil(c/e));a=document.createElement(_.Cd);a.style.width=c+_.of;a.style.height=d+_.of;a.style.backgroundColor="#000";a.style.backgroundRepeat="no-repeat";a.style.backgroundImage=`url("${this.src}")`;a.style.backgroundSize=`${Math.ceil(c/this.width*(this.sa.naturalWidth||this.sa.width))}px ${Math.ceil(d/this.height*(this.sa.naturalHeight||this.sa.height))}px`;b=this.y/this.height;a.style.backgroundPositionX="-"+this.x/
this.width*c+_.of;a.style.backgroundPositionY="-"+b*d+_.of;return a}load(){this.Ie||(this.Ie=new Promise((a,b)=>{const c=()=>{this.width||(this.width=(this.sa.naturalWidth||this.sa.width)/this.ih);this.height||(this.height=(this.sa.naturalHeight||this.sa.height)/this.ri);this.x||(this.x=this.width*this.Kh);this.y||(this.y=this.height*this.Lh)};if(this.sa.src&&this.sa.complete)c(),a(this);else{var d=null,e=()=>{c();this.sa.removeEventListener(_.ke,e);this.sa.removeEventListener(_.q,d);a(this)};d=f=>
{this.sa.removeEventListener(_.ke,e);this.sa.removeEventListener(_.q,d);b(new _.I(1,9,2014,"Failed to load thumbnail.",f))};this.sa.addEventListener(_.ke,e);this.sa.addEventListener(_.q,d);this.sa.src=this.src}}));return this.Ie}};_.z("clpp.thumbnails.Thumbnail",Ty);Ty.prototype.load=Ty.prototype.load;Ty.prototype.element=Ty.prototype.element;Ty.prototype.raw=Ty.prototype.raw;var az=class extends hz{constructor(){super();this.f=null;this.o=this.h=1;this.j=0}init(a,b){if(!b||!b.url)throw new _.I(1,9,7100,Ly);if("GRID"===b.mode&&!b.gridSize)throw new _.I(1,9,7100,"Grid size is mandatory for GRID mode");if(!b.duration)throw new _.I(1,9,7100,"Duration is mandatory");this.f=b;this.j=this.f.duration;if(this.f.gridSize)try{const c=this.f.gridSize.split(/x/).map(d=>parseInt(d,10)).filter(d=>0<d);if(2===c.length)this.h=c[0],this.o=c[1];else throw new _.I(1,9,7100,My);}catch(c){throw this.log.error("Error during parsing gridSize",
c),new _.I(1,9,7100,My);}super.init(a,b)}get(a){const b=this;return _.x(function*(){var c=b.player.getDuration();if(0>a||a>c)throw new _.I(1,9,2014,`Position is out of range: ${a}`);c=Math.floor(a/b.f.duration);const d=b.a[c];if(!d)throw new _.I(1,9,2014,"No thumbnail found for position "+a+" at index "+c);yield d.load();return d})}l(){if(0<this.a.length)return Promise.resolve();var a=this.player.getDuration();const b=this.h*this.o;a=Math.ceil(Math.ceil(a/this.j)/b);const c=this.f.templateKey||"$index$";
for(let d=0;d<a;d++){const e=this.f.url.replace(c,String(d+1)),f=new Image;for(let g=0;g<b;g++)this.a.push(new Ty(e,this.j*(d*b+g),this.j,0,0,0,0,this.h,this.o,g%this.h,Math.floor(g/this.h),f))}return Promise.resolve()}};var iz=class{constructor(a,b){this.time=a;this.duration=0;this.offset=b;this.length=0;this.Tc=new _.bn}load(){return this.Tc}},ez=class{constructor(){this.g=new _.S(Ny);this.a=new ArrayBuffer(0);this.f=[];this.h=new _.bn}init(a,b){if(!b||!b.url)throw new _.I(1,9,7100,Ly);const c=b.url;b=_.mj(c);b.type=_.lj;a.getNetworkEngine().fetch(b).T.then(d=>{var e=d.data;d=_.K(this.a);e=_.K(e);var f=d.byteLength+e.byteLength,g=new ArrayBuffer(f),h=new DataView(g),k=0;for(let l=0,m=d.byteLength;l<m;l++)h.setUint8(k,
d[l]),k+=1;for(let l=0,m=e.byteLength;l<m;l++)h.setUint8(k,e[l]),k+=1;this.a=g;this.g.info(`Appended ${e.byteLength} bytes.`+`Total data now ${f} bytes.`);f=this.a;d=_.K(f);h=String.fromCharCode.apply(null,jz);k=Array.prototype.slice.call(d,0,8);d=64;e=0;g=!1;if(h===String.fromCharCode.apply(null,k))for(f=new DataView(f,16,20),f=f.getUint32(0,!0),0===f&&(f=1E3);!g;){e+=1;k=f;h=new DataView(this.a,d);g=h.getUint32(0,!0);k=g*k/1E3;h=h.getUint32(4,!0);h=new iz(k,h);if(k=this.f.length)k=this.f[k-1],k.length=
h.offset-k.offset,k.duration=h.time-k.time;this.f.push(h);d+=8;(g=4294967295===g)&&this.h.resolve();if(1E5<e)throw Error("There was a problem during reading BIF entries");}else throw Error("The file is not a valid BIF format");Ry(this)}).catch(d=>{this.g.error(`[BIF] Error while loading thumbnails '
            + 'URL: ${c}`,d);a.onError(new _.I(1,9,2013,"Error while loading and parsing BIF thumbnails.",d))})}get(a){const b=this;return _.x(function*(){if(0>a)throw new _.I(1,9,2014,`Position is out of range: ${a}`);yield b.h;var c=b.f.find(d=>a>=d.time&&a<d.time+d.duration);if(!c)throw new _.I(1,9,2014,`No thumbnail found for position: ${a}`);c=yield Uy(b,c);yield c.load();return c})}},jz=[137,66,73,70,13,10,26,10];var cz=class extends hz{constructor(){super();this.f=null}init(a,b){if(!b||!b.url)throw new _.I(1,9,7100,Ly);const c=b.url,d=_.mj(c);d.type=_.lj;this.f=a.getNetworkEngine().fetch(d).T.then(e=>{e=e.data;if(!e)throw Error("No data in the response");e=_.Ui(e).split(/\n/);for(let m=0;m<e.length;m++){var f=e[m],g=f.split(" --\x3e ");if(2===g.length){f=e[++m];var h=f.indexOf("\x3d");var k=h=0>h?[]:f.substr(h+1).split(/,/).map(parseFloat);h=k[0];const n=k[1],r=k[2];k=k[3];var l=f.indexOf("#");f=0>l?f:f.substr(0,
l);f.match(/^http|^\/\//)||(f=c.substr(0,c.lastIndexOf("/"))+"/"+f);l=Vy(g[0]);g=Vy(g[1])-l;this.a.push(new Ty(f,l,g,h,n,r,k,0,0))}}}).catch(e=>{this.log.error("[VTT] Error while loading thumbnails "+`URL: ${c}`,e);a.onError(new _.I(1,9,2013,"Error while loading VTT thumbnails.",e))});super.init(a,b)}get(a){const b=this;return _.x(function*(){if(0>a)throw new _.I(1,9,2014,`Position is out of range: ${a}`);yield b.f;const c=b.a.find(d=>a>=d.time&&a<d.time+d.duration);if(!c)throw new _.I(1,9,2014,`No thumbnail found for position: ${a}`);
yield c.load();return c})}l(){return this.f}};var kz=class extends hz{get(a){const b=this;return _.x(function*(){function c(){return new _.I(1,9,2014,`No thumbnail found for position: ${a}`)}if(0>a)throw b.log.error("Position must be greater than 0 but is "+a),new _.I(1,9,2014,`Position is out of range: ${a}`);var d=b.player.getPeriods();d=Wy(d,a);if(!d)throw b.log.error("Could not find period for specified position",a),c();var e=d.thumbStreams.sort(b.f)[0];if(!e)throw b.log.error("Could not find thumb stream for specified position",a),c();d=
e.findSegmentPosition(a-d.startTime);if(null===d||void 0===d)throw b.log.error("Could not find segment index for specified position",a),c();var f=e.getSegmentReference(d);if(f){const h=Xy(f,e),k=Math.floor((a-f.startTime)/h.durationPerTile);var g;if(0<b.a.length)f=e.findSegmentPosition(0),null!==f&&(g=b.a[(d-f)*h.fg+k]);else if((e=f.Ya())&&0<e.length){let l=g=0;f=f.startTime;1<h.fg&&(g=k%h.od,l=Math.floor(k/h.od),f+=k*h.durationPerTile);g=new Ty(e[0],f,h.durationPerTile,void 0,void 0,void 0,void 0,
h.od,h.uf,g,l)}if(g)return yield g.load(),g}b.log.error("segment reference for specified segment index does not exist",d);throw c();})}f(a,b){return void 0!==a.bandwidth&&void 0!==b.bandwidth?a.bandwidth-b.bandwidth:0}l(){if(0<this.a.length)return Promise.resolve();var a=this.player.getPeriods();for(const b of a)if(a=b.thumbStreams.sort(this.f)[0]){let c=a.findSegmentPosition(0),d;for(;null!==c&&!_.B(d=a.getSegmentReference(c++));){const e=Xy(d,a),f=d.Ya()[0],g=new Image;let h=d.startTime;for(let k=
0;k<e.uf;++k)for(let l=0;l<e.od;++l)this.a.push(new Ty(f,h,e.durationPerTile,void 0,void 0,void 0,void 0,e.od,e.uf,l,k,g)),h+=e.durationPerTile}}return Promise.resolve()}};var lz=class{constructor(){this.g=new _.S(Ny);this.a=this.m=null}onPlayerCreated(a){this.m=a}onPlayerWillDestroy(){this.m=null}onPlayerWillRelease(){this.a=null}onContentWillLoad(a){const b=a.getConfiguration().thumbnails;if(b)if(!1===b.enabled)this.g.info("Thumbnails disabled.");else{var c=b.mode||null;c||(c=gz(b.url||null));(this.a=fz(c))?(this.g.info("Loading thumbs configuration",b),this.m.one(_.me,()=>{this.g.info("Initialize thumbnails provider");try{this.a.init(a,b)}catch(d){this.m.onError(d)}})):
this.g.warn("Unknown thumbnail mode",c)}else this.a=null}onContentLoaded(a){if(!this.a){var b=a.getConfiguration();b=b&&b.thumbnails;if(!b||void 0===b.enabled||b.enabled){var c=a.getPeriods();if(Yy(c)){this.a=new kz;try{this.a.init(a,b)}catch(d){this.m.onError(d)}}}}}get(a){return this.a?this.a.get(a):Promise.reject(new _.I(1,9,2014,"Thumbnail format is not supported"))}id(){return"thumbnails"}};_.z("clpp.thumbnails.ThumbnailsPlugin",lz);lz.prototype.get=lz.prototype.get;lz.Id="thumbnails";_.Ir(new class{create(){return new lz}});
var Zy="SINGLE",$y="GRID",bz="WEBVTT",dz="BIF";};f.call(g, window);