import PrestoPlayer from"./presto-player";const loadHLS=({config:e,selector:t,src:n,preload:o})=>new Promise((async(r,a)=>{if(!window.Hls)for(;!window.hasOwnProperty("Hls");)await new Promise((e=>setTimeout(e,50)));if(window.Hls.isSupported()){let a=!["metadata","none"].includes(o);wp?.blocks&&(a=!0);var l=new window.Hls({autoStartLoad:a});return l.loadSource(n),l.on(window.Hls.Events.LEVEL_SWITCHED,((e,n)=>{var o=t.closest(".presto-player__wrapper").querySelector(".plyr__menu__container [data-plyr='quality'][value='0'] span");l.autoLevelEnabled?o.innerHTML=`AUTO (${l.levels[n.level].height}p)`:o.innerHTML="AUTO"})),void l.on(window.Hls.Events.MANIFEST_PARSED,(function(n,o){var a=l.levels.map((function(e){return e.height}));a.unshift(0);const s=(a||[]).findIndex((e=>e===parseInt(window?.prestoPlayer?.hls_start_level)));l.startLevel=s?s-1:2,e.quality={default:0,options:a,forced:!0,onChange:function(e){0===e?prestoHLS.currentLevel=-1:prestoHLS.levels.forEach((function(t,n){t.height===e&&(console.log("Found quality match with "+e),prestoHLS.currentLevel=n)}))}},l.attachMedia(t),window.prestoHLS=l;const i=new PrestoPlayer(t,{...e});i.hls=l;const c=()=>{l.startLoad(-1),i.off("waiting",c)};return i.on("waiting",c),i.on("languagechange",(()=>{setTimeout((()=>l.subtitleTrack=i.currentTrack),50)})),r(i)}))}return t.canPlayType("application/vnd.apple.mpegurl"),r(new PrestoPlayer(t,{...e}))}));export default loadHLS;