import{r as t,h as e,H as i,d as s}from"./p-3d23a42a.js";const n=class{constructor(e){t(this,e),this.allowFullscreen=!0,this.instant=!1,this.loading=!1,this.loaded=!1}async componentWillLoad(){await this.addPreconnectLink()}async componentDidLoad(){this.instant&&await this.lazyLoadContent()}async updateIFrame(t,e){const i=this.el.shadowRoot.querySelector("iframe");i&&(i.width=""+t,i.height=""+e)}lazyLoadContent(){return this.createIFrame()}async onSrcUpdate(){await this.createIFrame()}play(){return this.playPauseVideo(!0)}pause(){return this.playPauseVideo(!1)}addPreconnectLink(){return new Promise((t=>{if(!this.src||!document)return void t();const e=document.head.querySelectorAll("link[rel='preconnect'][youtube]");if(e&&e.length>0)return void t();const i=document.createElement("link");i.rel="preconnect",i.href="https://www.youtube.com",i.setAttribute("youtube",""),document.head.appendChild(i),t()}))}createIFrame(){return new Promise((async t=>{if(!this.src)return void t();if(this.loading)return void t();const e=this.el.shadowRoot.querySelector("iframe");if(e&&!e.parentElement)return void t();this.loading=!0,this.loaded=!1,e&&e.parentElement.removeChild(e);const i=document.createElement("iframe"),s=document.createAttribute("allow");if(s.value="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",this.setAttributeNode(i,s),this.allowFullscreen){const t=document.createAttribute("allowfullscreen");t.value="",this.setAttributeNode(i,t)}let n=await this.formatSrc();if(!this.allowFullscreen){const t=document.createAttribute("playsinline");t.value="1",this.setAttributeNode(i,t);const e=n.split("?");n+=(e&&e.length>0?"&":"?")+"playsinline=1"}i.src=n,i.width=""+this.width,i.height=""+this.height,i.frameBorder="0",i.title=this.frameTitle;const o=this.el.shadowRoot.querySelector("div");o?(o.appendChild(i),this.loading=!1,this.loaded=!0,t()):t()}))}setAttributeNode(t,e){"function"===t.setAttributeNode&&t.setAttributeNode(e)}formatSrc(){return new Promise((async t=>{const e=await this.findVideoId();if(e)t("https://www.youtube.com/embed/"+e+"?enablejsapi=1");else{if(!this.src)return void t(this.src);const e=new URL(this.src);e.searchParams.get("enablejsapi")||e.searchParams.append("enablejsapi","1"),t(e.href)}}))}findVideoId(){return new Promise((t=>{const e=new URL(this.src);let i=e.searchParams.get("v");if(!i&&"youtu.be"===e.host&&e.pathname){const t=e.pathname.split("/");t&&t.length>=2&&(i=e.pathname.split("/")[1])}t(i)}))}playPauseVideo(t){return new Promise((async e=>{const i=this.el.shadowRoot.querySelector("iframe");i?(i.contentWindow.postMessage(JSON.stringify({event:"command",func:t?"playVideo":"pauseVideo",args:""}),"*"),e()):e()}))}render(){return e(i,{class:this.loaded?"loaded":""},e("div",{class:"youtube-container"}))}get el(){return s(this)}static get watchers(){return{src:["onSrcUpdate"]}}};n.style=":host(.loaded) div.youtube-container{opacity:var(--sparkle-youtube-opacity-loaded, 1)}div.youtube-container{opacity:var(--sparkle-youtube-opacity-not-loaded, 0);transition:var(--sparkle-youtube-transition, opacity 0.15s linear)}";export{n as sparkle_youtube}