import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var vT="Unsupported unit",wT="clpp-text-plain",xT="clpp-text-ttml",yT="clpp.htmlcue.HtmlTextDisplayer",zT="initial",AT=function(a){return _.di(a,{Of:(b,c)=>b.Oc(c)})},BT=function(a,b){if(0===b.length)return a;let c=a;a=a.findIndex(d=>d.startTime>=b[0].startTime);0<=a&&(c=c.slice(0,a));return c.concat(b)},CT=function(a,b){let c=0,d=a.length-1;for(;c<=d;){const e=c+d>>1,f=b(a[e]);if(0===f)return e;0<f?c=e+1:d=e-1}return-1},DT=function(a){if(!a.input)return null;var b=a.input.match(/^([^<]*)(<[^>]+>?)?/);
b=b[1]?b[1]:b[2];a.input=a.input.substr(b.length);return b},FT=function(a){let b=null;for(;b=a.match(/&(amp|lt|gt|lrm|rlm|nbsp);/);)a=a.replace(b[0],c=>ET[c]);return a},HT=function(a,b){GT(a,b);Array.from(b.getElementsByTagName("*")).forEach(c=>{GT(a,c)})},GT=function(a,b){for(const c in a)null!=a[c]&&(b.style[c]=a[c])},IT=function(a,b){return 0===a?"0":a+b},JT=function(a,b){return 1>Math.abs(a-b)||a<b},KT=function(a,b){return a<b&&1<Math.abs(a-b)},LT=function(a,b){return a>b&&1<Math.abs(a-b)},MT=
function(a,b){return 1>Math.abs(a-b)||a>b},OT=function(a){const b=a.getBoundingClientRect();var c=getComputedStyle(a,null);const d=parseFloat(c.getPropertyValue("border-left-width"))||0,e=parseFloat(c.getPropertyValue("border-right-width"))||0;var f=parseFloat(c.getPropertyValue("border-top-width"))||0;c=parseFloat(c.getPropertyValue("border-bottom-width"))||0;const g=(b.top||a.offsetTop)+f;f=(b.height||a.offsetHeight)-f-c;return new NT(b.left+d,b.right+e,g,(b.bottom||g+f)+c,f,(b.width||a.offsetWidth)-
d-e)},PT=function(a){return new NT(a.left,a.f,a.top,a.a,a.height,a.width)},QT=function(a,b){return b.some(c=>KT(a.left,c.f)&&LT(a.f,c.left)&&KT(a.top,c.a)&&LT(a.a,c.top))},RT=function(a,b){return MT(a.top,b.top)&&JT(a.a,b.a)&&MT(a.left,b.left)&&JT(a.f,b.f)},ST=function(a,b,c){switch(c){case "+x":return KT(a.left,b.left);case "-x":return LT(a.f,b.f);case "+y":return KT(a.top,b.top);case "-y":return LT(a.a,b.a);default:throw Error("Unknown axis");}},TT=function(a){return a.f.writingMode===_.Rt},UT=
function(a){var b=a.a.childNodes[0],c=a.a.getBoundingClientRect();const d=b.getClientRects();b=c.height/d.length;c=c.width/d.length;return TT(a)?b:c},VT=function(a){switch(a.f.writingMode){case _.Rt:return"horizontal-tb";case _.mg:return _.mg;case _.ng:return _.ng;default:throw Error("Unknown writing mode");}},WT=function(a){switch(a.f.positionAlign){case _.he:return _.ee;case "auto":case _.Uc:return _.Qt;case _.ie:return _.wf}return a.f.textAlign},XT=function(a,b){if(typeof a.f.position===_.t&&typeof a.f.size===
_.t)switch(b){case _.he:return a.f.position;case "auto":case _.Uc:return a.f.position-a.f.size/2;case _.ie:return a.f.position-a.f.size}return 0},YT=function(a){if(!a.f.positionAlign)switch(a.f.textAlign){case _.Jf:case _.ee:return _.he;case _.Qt:return _.Uc;case _.Rd:case _.wf:return _.ie}return a.f.positionAlign},aU=function(a,b,c){return(ZT(a)||$T(a))&&_.dh(c)?IT(c,_.of):ZT(a)&&_.B(c)?IT(0,_.of):TT(a)&&_.dh(b)?IT(b,"%"):zT},bU=function(a,b,c){return TT(a)&&_.dh(c)?IT(c,_.of):(ZT(a)||$T(a))&&_.dh(b)?
IT(b,"%"):zT},$T=function(a){return a.f.writingMode===_.ng},ZT=function(a){return a.f.writingMode===_.mg},cU=function(a,b){if(a.j)switch(b.writingMode){case _.Rt:return["-y"];case _.mg:return["+x"];case _.ng:return["-x"]}a=[];switch(b.writingMode){case _.Rt:a=["+y","-y"];break;case _.mg:a=["+x","-x"];break;case _.ng:a=["-x","+x"]}0>b.line&&(a=a.reverse());return a},dU=function(a){for(a.h=[];0<a.f.childNodes.length;)a.f.removeChild(a.f.childNodes[0]);a.a=OT(a.l.a)},eU=function(a,b,c){a=a.f;const d=
a.viewportAnchorUnits;if(d===_.Yt)return a.viewportAnchorY/100*c-a.regionAnchorY/100*b;if(0===d)return a.viewportAnchorY-a.regionAnchorY;throw Error(vT);},gU=function(a,b,c,d){const e=document.createElement(_.Pf),f=e.dataset;f.name=b;f.for=c;d&&(f.name=`${b} (sandbox)`,f.sandbox=!0);c=document.head;c.appendChild(e);fU(a,c,b);a.g.debug(`Created HTML style element #${b}.`);return e.sheet},hU=function(a,b){a=`.${a.j} .clpp-text-container`;const c=`${a} .clpp-text-cue`;return b=b.replace(/::cue\((#[^)]+)\)/g,
`${a} $1`).replace(/::cue\(([^)]+)\)/g,`${c} $1`).replace(/::cue-region\(([^)]+)\)/g,`${a} $1 .clpp-text-cue`).replace(/::cue/g,c).replace(/::cue-region/g,`${a} .clpp-text-cue-region .clpp-text-cue`).replace(/::ttml/g,a)},iU=function(a,b){let c=!1;try{a.f.insertRule(b,0);const d=a.f.cssRules[0];d instanceof CSSStyleRule&&(c=d.selectorText.split(",").every(e=>0===e.trim().indexOf(`.${a.j} `)))}catch(d){}0<a.f.cssRules.length&&a.f.deleteRule(0);return c},jU=function(a){if(null!=a.a){for(;0<a.a.cssRules.length;)a.a.deleteRule(0);
a.o=[]}},fU=function(a,b,c){const d=new MutationObserver(e=>{e.forEach(f=>{f.removedNodes.forEach(g=>{g.id===c&&a.g.error(`PRESTOplay HTML style element #${c} has `+"been unexpectedly removed! Some styles will not work!")})})});d.observe(b,{subtree:!1,childList:!0});a.l.push(()=>d.disconnect())},kU=function(a){a=_.Xg(a.htmlcue||{});_.B(a.stretchSmpteImage)&&(a.stretchSmpteImage=!1);_.B(a.enableResizeObserver)&&(a.enableResizeObserver=!0);return a},qU=function(a,b){if((b||a.l)&&a.F){var c=a.m.getPosition();
if(b||a.J!==c&&a.m.getState()===_.Wo)if(a.J=c,c=lU(a,c),b||(b=a.o,b=!(null===b&&null===c||null!==b&&null!==c&&b.start===c.start&&b.end===c.end)),b&&(a.o=c,mU(a),c)){b={color:a.getFontColor()||void 0,backgroundColor:a.getBackgroundColor()||void 0,fontFamily:a.getFontFamily()||void 0,textShadow:_.Ft(a)||void 0};c=a.f.slice(c.start,c.end+1);for(const k of c){c=a;var d=k,e=b,f=c.a.a,g=null;switch(d.payloadType){case _.xg:g="clpp-text-vtt";break;case _.bg:g=xT;break;case _.Pt:g=wT}g&&!f.classList.contains(g)&&
_.pp(f,g);f=OT(c.a.a);if(0===f.width||0===f.height)c.g.warn("The height or width of the `.clpp-text-container` element is zero. Captions will not be displayed. Please check if the `clpp.styles.css` file has been included.");else{GT({fontSize:nU(c,d.containerRows)},c.a.a);f=null;g=d.payloadType===_.xg;var h=d.payloadType===_.bg;d.region&&(g||h||d.backgroundImage)&&(c.B.has(d.region.id)?f=c.B.get(d.region.id):(f=new oU(d.region,{}),c.B.set(d.region.id,f),c.a.appendChild(f)));d=new pU(d,c.L,e);f?f.appendChild(d):
c.a.appendChild(d)}}}}},rU=function(a){mU(a);a.J=null;a.o=null;a.f=[]},sU=function(a,b){return a.startTime<=b&&a.endTime>b},lU=function(a,b){var c=CT(a.f,d=>sU(d,b)?0:b!==d.startTime?b-d.startTime:b-d.endTime);if(-1===c)return null;{let d=c;for(;0<=d-1&&sU(a.f[d-1],b);)d--;for(;c+1<a.f.length&&sU(a.f[c+1],b);)c++;return{start:d,end:c}}},mU=function(a){a.B.clear();if(a.a){const b=a.a.a;b.classList.remove("clpp-text-vtt");b.classList.remove(xT);b.classList.remove(wT);a=a.a;dU(a.h);a.l=[]}},nU=function(a,
b){const c=OT(a.a.a).height;if(null!==a.I)return a.getFontSize();if(null!==a.C)return a=a.getFontSizePercent(),`${Math.round(c*a)}px`;if(typeof b===_.t&&0<b)return`${Math.round(c/b)}px`;a=a.getFontSizePercent();return`${Math.round(c*a)}px`},wU=class{parse(a){a={input:a};const b=_.rp(_.Cd),c=[];let d=b;for(var e;null!==(e=DT(a));)if("\x3c"===e[0])if("/"===e[1])c.length&&c[c.length-1]===e.substr(2).replace("\x3e","")&&(c.pop(),d=d.parentNode);else{if(e=e.match(/^<([^.\s/0-9>]+)(\.[^\s\\>]+)?([^>\\]+)?(\\?)>?$/)){var f=
e[1];var g=e[3];var h=tU[f];h?(h=_.rp(h),(f=uU[f])&&g&&h.setAttribute(f,g.trim()),g=h):g=null;!g||vU[g.localName]&&vU[g.localName]!==d.localName||(e[2]&&(g.className=e[2].substr(1).replace(/\./g," ")),c.push(e[1]),d.appendChild(g),d=g)}}else d.appendChild(document.createTextNode(FT(e)));return b}},tU={c:"c",i:"i",b:"b",u:"u",ruby:"ruby",rt:"rt",v:"v",lang:"lang"},uU={v:"voice",lang:"lang"},vU={rt:"ruby"},ET={"\x26amp;":"\x26","\x26lt;":"\x3c","\x26gt;":"\x3e","\x26lrm;":"\u200e","\x26rlm;":"\u200f",
"\x26nbsp;":"\u00a0"};var NT=class{constructor(a,b,c,d,e,f){this.left=a;this.f=b;this.top=c;this.a=d;this.height=e;this.width=f}};var xU=class{constructor(a,b,c){this.id=a;this.w=b;this.B=c;this.a=this.o();this.j=!1}init(){this.j=!0}A(){return!1}};var pU=class extends xU{constructor(a,b,c){super(a.id,"clpp-text-cue",{});this.l=b;this.f=a;this.h=!!this.f.backgroundImage;this.C=c}A(){return!this.h&&!(this.f.region&&this.f.region.displayAlign)}init(a){super.init(a);if(this.h)_.pp(this.a,"clpp-text-cue-image"),this.a.style.backgroundImage=`url("${this.f.backgroundImage}")`,this.l.stretchSmpteImage&&(this.a.style.backgroundSize="contain");else if(a){var b=this.f;if(b.payloadType===_.bg){var c=_.rp(_.Cd);c.innerHTML=b.payload;b=c}else b.payloadType===
_.xg?b=(new wU).parse(b.payload):(c=_.rp(_.Cd),c.textContent=b.payload,b=c);_.pp(b,this.w);this.id&&(b.id=this.id);for(d of this.f.cssClassList)_.pp(b,d);GT({writingMode:VT(this)},b);var d=b;this.a.appendChild(d);_.pp(this.a,"clpp-text-wrapper");for(var e of this.f.cssClassList)_.pp(this.a,`clpp-text-wrapper__${e}`);_.B(this.f.line)&&(this.f.lineInterpretation=_.St,this.f.line=TT(this)?-1:0);e=VT(this);GT({writingMode:e,webkitWritingMode:e,direction:zT,textAlign:WT(this)},this.a);if(!this.f.region||
!this.f.region.displayAlign){e=XT(this,YT(this));GT({position:"absolute",left:aU(this,e,null),right:$T(this)&&_.B(null)?IT(0,_.of):zT,top:bU(this,e,null),width:TT(this)?IT(this.f.size,"%"):zT,height:ZT(this)||$T(this)?IT(this.f.size,"%"):zT},this.a);b=this.f;a=TT(this)?a.height:a.width;c=UT(this);if(b.lineInterpretation===_.St){var f=c*Math.round(b.line);Math.abs(f)>a&&(f=c*(0>b.line?0:-1));0>f&&(f+=a)}else{f=c/a*100;let g=b.line;switch(b.lineAlign){case _.Uc:g-=f/2;break;case _.Rd:g-=f}f=Math.round(g/
100*a)}$T(this)&&(f=a-f-c);a=f;GT({left:aU(this,e,a),right:$T(this)&&_.B(a)?IT(0,_.of):zT,top:bU(this,e,a)},this.a)}HT(this.C,d)}}o(){return _.rp(_.Cd)}};var yU=class{constructor(a,b){this.l=a;this.f=a.a;this.j=void 0===b?!1:b;this.a=OT(a.a);this.h=[]}append(a){const b=a.a;b.style.visibility=_.$d;this.f.appendChild(b);a.j||a.init(this.a);if(a.A()){a:{var c=OT(a.a);var d=cU(this,a.f),e=UT(a);const g=PT(c);let h=null,k=0;for(const l of d){for(;ST(c,this.a,l)||RT(c,this.a)&&QT(c,this.h);){d=c;var f=e;switch(l){case "+x":d.left+=f;d.f+=f;break;case "-x":d.left-=f;d.f-=f;break;case "+y":d.top+=f;d.a+=f;break;case "-y":d.top-=f,d.a-=f}}if(this.j||RT(c,this.a))break a;
d=this.a;d=Math.max(0,Math.min(c.f,d.f)-Math.max(c.left,d.left))*Math.max(0,Math.min(c.a,d.a)-Math.max(c.top,d.top))/(c.height*c.width);d>k&&(h=PT(c),k=d);c=PT(g)}c=h||g}GT({top:IT(c.top-this.a.top,_.of),bottom:IT(this.a.a-c.a,_.of),left:IT(c.left-this.a.left,_.of),right:IT(this.a.f-c.f,_.of),height:IT(c.height,_.of),width:IT(c.width,_.of)},a.a)}b.style.visibility="visible";this.h.push(OT(a.a))}};var zU=class extends xU{constructor(a,b,c){super(a,b,c);this.h=null;this.l=[]}init(){this.h=new yU(this);this.j=!0}o(){const a=_.rp(_.Cd);_.pp(a,this.w);this.id&&(a.id=this.id);return a}appendChild(a){dU(this.h);this.l.unshift(a);for(const b of this.l)this.h.append(b)}};var oU=class extends zU{constructor(a,b){super(a.id,"clpp-text-cue-region",b);this.f=a}init(a){a:{var b=this.f;switch(b.widthUnits){case 0:b=b.width;break a;case _.Yt:b=b.width/100*a.width;break a;default:throw Error(vT);}}var c=this.f;var d=c.viewportAnchorUnits;if(d===_.Yt)c=c.viewportAnchorX/100*a.width-c.regionAnchorX/100*b;else if(0===d)c=c.viewportAnchorX-c.regionAnchorX;else throw Error(vT);d=a.height;const e=eU(this,d,a.height);GT(this.B,this.a);GT({left:IT(c,_.of),top:IT(e,_.of),width:IT(b,
_.of),height:IT(d,_.of)},this.a);this.f.writingMode&&GT({display:"flex","flex-direction":this.f.writingMode},this.a);this.f.displayAlign&&GT({"justify-content":this.f.displayAlign},this.a);for(var f of this.f.cssClassList)_.pp(this.a,f);a:switch(f=this.f,f.heightUnits){case 0:f=f.height;break a;case _.Yt:f=f.height/100*a.height;break a;case 2:f=f.height;c=new pU(new _.Vt(0,1,"Lorem ipsum",_.Pt),{},{});b=c.a;this.a.appendChild(b);c.init(OT(this.a));c=b.getBoundingClientRect().height;this.a.removeChild(b);
b=c;f*=b;break a;default:throw Error(vT);}a=eU(this,f,a.height);GT({top:IT(a,_.of),height:IT(f,_.of)},this.a);this.h=new yU(this,!0);this.j=!0}getId(){return this.f.id}};var AU=class{constructor(a,b){this.g=new _.S("clpp.htmlcue.StyleManager");this.l=[];this.j=a;this.h=b;this.o=[];this.a=gU(this,this.h,this.j);this.f=gU(this,this.h,this.j,!0);this.f.disabled=!0;this.g.debug(`Stylesheet ${this.h} has been created.`)}appendRule(a){if(null!=this.a&&!this.o.some(b=>b===a)){const b=hU(this,a);if(iU(this,b)){try{this.a.insertRule(b,this.a.cssRules.length),this.g.debug(`Added CSS rule ${b}`)}catch(c){this.g.error(`An error occurred when adding a CSS rule: ${b}`)}this.o.push(a)}else this.g.warn(`Stylesheet ${this.h} ignoring the CSS rule `+
`because it's not safe: ${b}`)}}destroy(){const a=this;return _.x(function*(){null!=a.a&&(a.l.forEach(b=>b()),a.l=[],jU(a),document.head.removeChild(a.a.ownerNode),document.head.removeChild(a.f.ownerNode),a.a=null,a.f=null,a.g.debug(`Stylesheet ${a.h} has been destroyed.`))})}};var BU=class extends _.Mv{constructor(a){super(a);this.g=new _.S(yT);this.m=a;this.L=kU(a.getConfiguration());this.l=!1;this.f=[];this.F=a.getSurface().getContainer();this.j=new AU(a.getSurface().getUniqueCssClass(),"CLPP Subtitle Styles");this.a=new zU(null,"clpp-text-container",{});this.A=new _.Fv(a.getSurface(),this.S.bind(this));this.J=null;this.F?(this.F.appendChild(this.a.a),this.a.init(null)):this.g.warn("No container element! Subtitles will not appear. Please check your player surface configuration or switch to NativeTextDisplayer.");
this.G=new _.rk(()=>{qU(this)});this.B=new Map;this.o=null}append(a,b){if(null!=this.j&&0!==a.length){for(const c of b)this.j.appendRule(c);a.sort((c,d)=>c.startTime!==d.startTime?c.startTime-d.startTime:c.endTime-d.endTime);this.f=BT(this.f,AT(a));this.o=null;qU(this,!1)}}h(){null==this.j||qU(this,!0)}destroy(){const a=this;return _.x(function*(){null!=a.j&&(rU(a),a.G.stop(),yield a.j.destroy(),a.j=null,a.F.removeChild(a.a.a),a.a=null,a.A.stop(),a.A=null,a.l=!1)})}remove(a,b){if(null==this.j)return!1;
this.f=this.f.filter(c=>c.startTime<a||c.endTime>=b);this.o=null;qU(this,!0);return!0}isTextVisible(){return this.l}setTextVisibility(a){null!=this.j&&this.l!==a&&((this.l=a)?(this.G.Bb(.25),this.L.enableResizeObserver&&this.A.start(),qU(this,!0)):(this.G.stop(),this.A.stop(),rU(this)))}S(){qU(this,!0)}};_.z(yT,BU);BU.prototype.setTextVisibility=BU.prototype.setTextVisibility;BU.prototype.isTextVisible=BU.prototype.isTextVisible;BU.prototype.remove=BU.prototype.remove;BU.prototype.destroy=BU.prototype.destroy;
BU.prototype.append=BU.prototype.append;var CU=class{a(a){return!!a.enableHtmlCue&&!_.Hk()}create(a){return new BU(a)}};_.z("clpp.htmlcue.HtmlCueComponent",class extends _.Av{f(){_.Co(_.ae,new CU)}a(){_.Ao.has(_.ae)&&(_.Ao.delete(_.ae),_.Bo.info("Removed factory: 'html'"))}id(){return _.Qf}});};f.call(g, window);