import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var uw=" The setting will be ignored.",vw="VTT parser encountered an invalid VTT setting: ",ww="application/x-subrip",xw="text/srt",yw='text/vtt; codecs\x3d"vtt"',zw='text/vtt; codecs\x3d"wvtt"',Aw=function(a){return a.a===a.f.length?null:(a=_.Ei(a,/[^ \t\n]*/gm))?a[0]:null},Bw=function(a){_.Ei(a,/[ \t]+/gm)},Cw=function(a){let b="";for(a=a.split(/\n/);3<a.length;){for(var c=3;c<a.length;++c)a[2]+=`\n${a[c]}`;a.splice(3,a.length-3)}if(3!==a.length)return"";c=0;!a[0].match(/\d+:\d+:\d+/)&&
a[1].match(/\d+:\d+:\d+/)&&(b+=`${a[0].match(/\w+/)}\n`,++c);if(a[c].match(/\d+:\d+:\d+/)){const d=a[1].match(/(\d+):(\d+):(\d+)(?:,(\d+))?\s*--?>\s*(\d+):(\d+):(\d+)(?:,(\d+))?/);if(d)b+=`${d[1]}:${d[2]}:${d[3]}.${d[4]} --> `,b+=`${d[5]}:${d[6]}:${d[7]}.${d[8]}\n`,++c;else return""}else return"";a[c]&&(b+=`${a[c]}\n\n`);return b},Dw=function(a){a=_.Ei(a,/(?:(\d{1,}):)?(\d{2}):(\d{2})\.(\d{2,3})/g);if(null==a)return null;const b=Number(a[2]),c=Number(a[3]);return 59<b||59<c?null:Number(a[4])/1E3+
c+60*b+3600*(Number(a[1])||0)},Fw=function(a,b,c){let d;if(d=/^align:(start|middle|center|end|left|right)$/.exec(b))b=d[1],"middle"===b?a.textAlign=_.Qt:a.textAlign=_.Wt[b.toUpperCase()];else if(d=/^vertical:(lr|rl)$/.exec(b))a.writingMode="lr"===d[1]?_.mg:_.ng;else if(d=/^size:([\d.]+)%$/.exec(b))a.size=Number(d[1]);else if(d=/^position:([\d.]+)%(?:,(line-left|line-right|center|start|end))?$/.exec(b))a.position=Number(d[1]),d[2]&&(b=d[2],a.positionAlign=b===_.he||b===_.Jf?_.he:b===_.ie||b===_.Rd?
_.ie:_.Uc);else if(d=/^region:(.*)$/.exec(b)){if(b=Ew(c,d[1]))a.region=b}else{a:{if(c=/^line:([\d.]+)%(?:,(start|end|center))?$/.exec(b))a.lineInterpretation=1,a.line=Number(c[1]),c[2]&&(a.lineAlign=_.Xt[c[2].toUpperCase()]);else if(c=/^line:(-?\d+)(?:,(start|end|center))?$/.exec(b))a.lineInterpretation=_.St,a.line=Number(c[1]),c[2]&&(a.lineAlign=_.Xt[c[2].toUpperCase()]);else{a=!1;break a}a=!0}return a}return!0},Ew=function(a,b){a=a.filter(c=>c.id===b);return a.length?a[0]:(_.C("VTT parser could not find a region with id: ",
b," The region will be ignored."),null)},Hw=function(a,b,c){let d,e,f;(new _.om).N("payl",_.jm(g=>{d=_.Ui(g)})).N("iden",_.jm(g=>{e=_.Ui(g)})).N("sttg",_.jm(g=>{f=_.Ui(g)})).parse(a);return d?Gw(d,e,f,b,c):null},Gw=function(a,b,c,d,e){a=new _.Vt(d,e,a,_.xg);b&&(a.id=b);if(c)for(b=new _.Fi(c),c=Aw(b);c;)Fw(a,c,[])||_.C(vw,c,uw),Bw(b),c=Aw(b);return a},Iw=class{a(a){a=_.Ui(a);a=a.replace(/\r+/g,"");a=a.replace(/^\s+|\s+$/g,"");a=a.replace(/<[a-zA-Z\\/][^>]*>/g,"");a=a.split("\n\n");let b="";if(0<a.length){b+=
"WEBVTT\n\n";for(const c of a)b+=Cw(c)}return _.Yi(b)}},Jw=class{create(){return new Iw}probe(a,b){return(a===xw||a===ww)&&b===_.Sf}};var Kw=class{constructor(a){this.a=a}parseInit(){}parseMedia(a,b){a=_.Ui(a);a=a.replace(/\r\n|\r(?=[^\n]|$)/gm,"\n");var c=a.split(/\n{2,}/m);if(!/^WEBVTT($|[ \t\n])/m.test(c[0]))throw new _.I(_.G,2,2E3);a=b.periodStart;if(c[0].includes("X-TIMESTAMP-MAP")){var d=c[0].match(/LOCAL:((?:(\d{1,}):)?(\d{2}):(\d{2})\.(\d{3}))/m),e=c[0].match(/MPEGTS:(\d+)/m);if(d&&e){a=Dw(new _.Fi(d[1]));if(null==a)throw new _.I(_.G,2,2E3);e=Number(e[1]);for(d=b.segmentStart;95443.7176888889<=d;)d-=95443.7176888889,e+=
8589934592;a=b.periodStart+e/9E4-a}}else this.a.streaming.relativeTextTimestamp&&null!==b.segmentStart&&(a=b.segmentStart);b=[];for(var f of c)if(/^region/.test(f.toLowerCase())){e=new _.Fi(f.replace(/\n/g," "));d=new _.$t;Aw(e);Bw(e);for(var g=Aw(e);g;){a:{var h;var k=d;var l=g;if(h=/^id:(.*)$/.exec(l))k.id=h[1];else if(h=/^width:(\d{1,2}|100)%$/.exec(l))k.width=Number(h[1]);else if(h=/^lines:(\d+)$/.exec(l))k.height=Number(h[1]),k.heightUnits=2;else if(h=/^regionanchor:(\d{1,2}|100)%,(\d{1,2}|100)%$/.exec(l))k.regionAnchorX=
Number(h[1]),k.regionAnchorY=Number(h[2]);else if(h=/^viewportanchor:(\d{1,2}|100)%,(\d{1,2}|100)%$/.exec(l))k.viewportAnchorX=Number(h[1]),k.viewportAnchorY=Number(h[2]);else if(/^scroll:up$/.exec(l))k.scroll="up";else{k=!1;break a}k=!0}k||_.C("VTT parser encountered an invalid VTTRegion setting: ",g,uw);Bw(e);g=Aw(e)}e=d;b.push(e)}f=[];for(var m of c)if(/^style/.test(m.toLowerCase())){e=m.replace(/^style/i,"").replace(/\n/g," ");d=/[^{}]+{[^{}]*}/g;do(g=d.exec(e))&&f.push(g[0]);while(g)}m=[];for(const n of c)if(n.includes("--\x3e")){a:{l=
n.split("\n");h=a;c=b;if(1===l.length&&!l[0]||/^NOTE($|[ \t])/.test(l[0])||"STYLE"===l[0]){c=null;break a}e=null;l[0].includes("--\x3e")||(e=l[0],l.splice(0,1));d=new _.Fi(l[0]);g=Dw(d);const r=_.Ei(d,/[ \t]+--\x3e[ \t]+/g);k=Dw(d);if(null==g||null==r||null==k)throw new _.I(_.G,2,2001);g+=h;k+=h;l=l.slice(1).join("\n").trim();g=new _.Vt(g,k,l,_.xg);e&&(g.id=e);Bw(d);for(k=Aw(d);k;)Fw(g,k,c)||_.C(vw,k,uw),Bw(d),k=Aw(d);null!=e&&(g.id=e);c=g}c&&m.push(c)}return{cues:m,styles:f}}};var Lw=class extends Kw{parseMedia(a,b){return super.parseMedia((new Iw).a(a),b)}};var Mw=class{constructor(){this.a=null;this.g=new _.S("clpp.vtt.mp4")}parseInit(a){let b=!1;(new _.om).N(_.ye,_.hm).N(_.ag,_.hm).N(_.qe,_.hm).ea("mdhd",c=>{0===c.version?(c.reader.skip(4),c.reader.skip(4),this.a=_.P(c.reader),c.reader.skip(4)):(c.reader.skip(8),c.reader.skip(8),this.a=_.P(c.reader),c.reader.skip(8));c.reader.skip(4)}).N(_.xe,_.hm).N(_.Mf,_.hm).ea(_.Of,_.im).N("wvtt",()=>{b=!0}).parse(a);if(!this.a)throw new _.I(_.G,2,2008);if(!b)throw new _.I(_.G,2,2008);}parseMedia(a,b){if(!this.a)throw this.g.error("No init segment for MP4+VTT!"),
new _.I(_.G,2,2008);let c=0,d=[],e;const f=[];let g=!1,h=!1,k=!1,l=null;(new _.om).N("moof",_.hm).N("traf",_.hm).ea("tfdt",r=>{g=!0;c=0===r.version?_.P(r.reader):_.cm(r.reader)}).ea("tfhd",r=>{var u=r.flags;r=r.reader;r.skip(4);u&1&&r.skip(8);u&2&&r.skip(4);l=u&8?_.P(r):null}).ea("trun",r=>{h=!0;{var u=r.version,v=r.flags;r=r.reader;const y=_.P(r);v&1&&r.skip(4);v&4&&r.skip(4);const A=[];for(let D=0;D<y;D++){const E={duration:null,sampleSize:null,lf:null};v&256&&(E.duration=_.P(r));v&512&&(E.sampleSize=
_.P(r));v&1024&&r.skip(4);v&2048&&(E.lf=0===u?_.P(r):_.bm(r));A.push(E)}d=A}}).N(_.pe,_.jm(r=>{k=!0;e=r})).parse(a);if(!k&&!g&&!h)throw new _.I(_.G,2,2008);a=c;var m=new DataView(e.buffer,e.byteOffset,e.byteLength);m=new _.ln(m,!1);for(const r of d){const u=r.duration||l,v=r.lf?c+r.lf:a;a=v+(u||0);let y=0;do{const A=_.P(m);y+=A;var n=_.P(m);n=_.gm(n);let D=null;"vttc"===n?8<A&&(D=_.em(m,A-8)):("vtte"!==n&&this.g.error("Unknown box "+n+"! Skipping!"),m.skip(A-8));u?D&&f.push(Hw(D,b.periodStart+v/this.a,
b.periodStart+a/this.a)):this.g.error("WVTT sample duration unknown, and no default found!")}while(r.sampleSize&&y<r.sampleSize)}return{cues:f.filter(_.ii),styles:[]}}};_.z("clpp.vtt.VttComponent",class extends _.Av{constructor(){super();this.j=new Jw}f(){_.yt(_.Sf,Kw);_.yt(yw,Kw);_.yt(zw,Kw);_.yt(_.wc,Mw);_.yt(xw,Lw);_.yt(ww,Lw);_.ck.push(this.j)}a(){_.At(_.Sf);_.At(yw);_.At(zw);_.At(_.wc);_.At(xw);_.At(ww);_.ek(this.j)}id(){return"subtitles-vtt"}});};f.call(g, window);