import{l,n as m,i as u,q as C,d as S,D as c,a as p,I as g}from"./vidstack-DLVdcWrK.js";import{g as T}from"./vidstack-DKqYI_HJ.js";const b=Symbol(0),E=Symbol(0),A=Symbol(0),w=Symbol(0),O=Symbol(0),x=Symbol(0),N=Symbol(0),a={crossOrigin:b,readyState:E,updateActiveCues:A,canLoad:w,onModeChange:O,native:x,nativeHLS:N};function f(h,t){return t>=h.startTime&&t<h.endTime}function v(h,t,s){let e=null,i=C();function n(){const r=u(t)?[t]:t,o=h.toArray().find(d=>r.includes(d.kind)&&d.mode==="showing");if(o!==e){if(!o){s(null),e=null;return}o.readyState==2?s(o):(s(null),m(()=>{const d=l(o,"load",()=>{s(o),d()},{once:!0})},i)),e=o}}return n(),l(h,"mode-change",n)}function k(h,t,s){v(h,t,e=>{if(!e){s("");return}const i=()=>{const n=e?.activeCues[0];s(n?.text||"")};i(),l(e,"cue-change",i)})}class I extends S{static createId(t){return`vds-${t.type}-${t.kind}-${t.src??t.label??"?"}`}src;content;type;encoding;id="";label="";language="";kind;default=!1;#h=!1;#e=0;#i="disabled";#o={};#n=[];#t=[];#s=[];#r=new Set;[a.readyState]=0;[a.crossOrigin];[a.onModeChange]=null;[a.native]=null;get metadata(){return this.#o}get regions(){return this.#n}get cues(){return this.#t}get activeCues(){return this.#s}get readyState(){return this[a.readyState]}get mode(){return this.#i}set mode(t){this.setMode(t)}constructor(t){super();for(const s of Object.keys(t))this[s]=t[s];this.type||(this.type="vtt"),t.content?this.#f(t):t.src||(this[a.readyState]=2)}addCue(t,s){if(t.id){if(this.#r.has(t.id))return;this.#r.add(t.id)}let e=0,i=this.#t.length;for(e=0;e<i&&!(t.endTime<=this.#t[e].startTime);e++);e===i?this.#t.push(t):this.#t.splice(e,0,t),t instanceof TextTrackCue||this[a.native]?.track.addCue(t),this.dispatchEvent(new c("add-cue",{detail:t,trigger:s})),f(t,this.#e)&&this[a.updateActiveCues](this.#e,s)}removeCue(t,s){const e=this.#t.indexOf(t);if(e>=0){const i=this.#s.includes(t);t.id&&this.#r.delete(t.id),this.#t.splice(e,1),this[a.native]?.track.removeCue(t),this.dispatchEvent(new c("remove-cue",{detail:t,trigger:s})),i&&this[a.updateActiveCues](this.#e,s)}}setMode(t,s){this.#i!==t&&(this.#i=t,t==="disabled"?(this.#s=[],this.#u()):this.readyState===2?this[a.updateActiveCues](this.#e,s):this.#c(),this.dispatchEvent(new c("mode-change",{detail:this,trigger:s})),this[a.onModeChange]?.())}[a.updateActiveCues](t,s){if(this.#e=t,this.mode==="disabled"||!this.#t.length)return;const e=[];for(let n=0,r=this.#t.length;n<r;n++){const o=this.#t[n];f(o,t)&&e.push(o)}let i=e.length!==this.#s.length;if(!i){for(let n=0;n<e.length;n++)if(!this.#s.includes(e[n])){i=!0;break}}this.#s=e,i&&this.#u(s)}[a.canLoad](){this.#h=!0,this.#i!=="disabled"&&this.#c()}#f(t){import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js").then(({parseText:s,VTTCue:e,VTTRegion:i})=>{if(!u(t.content)||t.type==="json")this.#l(t.content,e,i),this.readyState!==3&&this.#a();else{const n=t.content.split(`
`).map(r=>r.trim()).join(`
`).trim();s(n,{type:t.type}).then(({cues:r,regions:o})=>{this.#t=r,this.#n=o,this.#a()})}})}async#c(){if(!(!this.#h||this[a.readyState]>0)){if(this[a.readyState]=1,this.dispatchEvent(new c("load-start")),!this.src){this.#a();return}try{const{parseResponse:t,VTTCue:s,VTTRegion:e}=await import("https://cdn.jsdelivr.net/npm/media-captions@next/dist/prod.js"),i=this[a.crossOrigin]?.(),n=fetch(this.src,{headers:this.type==="json"?{"Content-Type":"application/json"}:void 0,credentials:T(i)});if(this.type==="json")this.#l(await(await n).text(),s,e);else{const{errors:r,metadata:o,regions:d,cues:y}=await t(n,{type:this.type,encoding:this.encoding});if(r[0]?.code===0)throw r[0];this.#o=o,this.#n=d,this.#t=y}this.#a()}catch(t){this.#d(t)}}}#a(){if(this[a.readyState]=2,!this.src||this.type!=="vtt"){const s=this[a.native];if(s&&!s.managed)for(const e of this.#t)s.track.addCue(e)}const t=new c("load");this[a.updateActiveCues](this.#e,t),this.dispatchEvent(t)}#d(t){this[a.readyState]=3,this.dispatchEvent(new c("error",{detail:t}))}#l(t,s,e){try{const{regions:i,cues:n}=j(t,s,e);this.#n=i,this.#t=n}catch(i){this.#d(i)}}#u(t){this.dispatchEvent(new c("cue-change",{trigger:t}))}}const M=/captions|subtitles/;function _(h){return M.test(h.kind)}function j(h,t,s){const e=u(h)?JSON.parse(h):h;let i=[],n=[];return e.regions&&s&&(i=e.regions.map(r=>Object.assign(new s,r))),(e.cues||p(e))&&(n=(p(e)?e:e.cues).filter(r=>g(r.startTime)&&g(r.endTime)).map(r=>Object.assign(new t(0,0,""),r))),{regions:i,cues:n}}export{a as T,I as a,k as b,f as c,_ as i,v as w};
