function h(){return/iPhone|iPad|iPod/i.test(navigator.userAgent)||m()}function p(){return/Android/i.test(navigator.userAgent)}function m(){return navigator.userAgent.toLowerCase().indexOf("macintosh")!==-1&&!!navigator.maxTouchPoints&&navigator.maxTouchPoints>2}var u=class{constructor(){this.loadedScripts={},this.disposed=!1}load(t,e){const i=this.loadedScripts[t];if(i)return i;const s=new Promise((o,a)=>{if(e.test()){o();return}this.renderScriptTag(t,e);const r=Date.now(),c=e.timeout||5e3,l=()=>{if(this.disposed){a("script loader is disposed");return}if(e.test()){o();return}if(Date.now()-r>c){a(`failed to load ${t} due to timeout`);return}window.requestAnimationFrame(l)};window.requestAnimationFrame(l)});return this.loadedScripts[t]=s,s.catch(o=>{console.error(`failed to load ${t}`),console.error(o),delete this.loadedScripts[t]}),s}renderScriptTag(t,e){const i=document.createElement("script");if(e?.attrs)for(const s in e.attrs){const o=e.attrs[s];i.setAttribute(s,o)}i.src=t,document.body.appendChild(i)}renderDom(t){this.renderScriptTag(t)}dispose(){this.loadedScripts={},this.disposed=!0}},f="https://www.youtube.com/iframe_api?trustedtypes=1",y={namespace:"degu-youtube-modal",useHandlerOnMobile:!0,transitionDuration:300,parentSelector:"body",playerVars:{autohide:1,autoplay:1,fs:1,modestbranding:1,rel:0,showinfo:0,iv_load_policy:3},scriptLoader:new u},n={ENTER:"Enter",ESC:"Escape",SPACE:" "},g=class{constructor(t){this.isVisible=!1,this.config=Object.assign({},y,t),this.container=document.querySelector(this.config.parentSelector);const e=this.createDom("div",this.config.namespace);e.setAttribute("aria-modal","true"),e.setAttribute("role","dialog");const i=this.createDom("button",`${this.config.namespace}__x`);i.setAttribute("aria-label","Close video player"),i.setAttribute("tabindex","0"),i.addEventListener("click",()=>{this.setActive(!1)}),this.closeEl=i;const s=this.createDom("div",`${this.config.namespace}__attribution`);this.attributionEl=s;const o=this.createDom("div",`${this.config.namespace}__player`);this.playerEl=o;const a=this.createDom("div",`${this.config.namespace}__mask`);e.appendChild(i),e.appendChild(s),e.appendChild(o),e.appendChild(a),this.modalEl=e,this.container.appendChild(e),document.addEventListener("click",this.handleEvent.bind(this)),document.addEventListener("keydown",this.handleKeyboardEvent.bind(this))}async play(t,e){const i=e||{};if(this.config.useHandlerOnMobile&&(h()||p())){let o=`https://m.youtube.com/watch?v=${t}`;i.startTime&&(o=`${o}&t=${i.startTime}s`),window.location.href=o;return}await this.config.scriptLoader.load(f,{test:()=>window.YT&&window.YT.loaded===1}),i.attribution&&(this.attributionEl.textContent=i.attribution);const s=i.orientation||"landscape";if(this.modalEl.setAttribute("data-orientation",s),this.setActive(!0),this.player){if(t===this.activeVideoId)return;this.player.loadVideoById(t,i.startTime||0,"large"),this.activeVideoId=t}else{const o=Object.assign({},this.config.playerVars);i.startTime&&(o.start=i.startTime),o.origin=location.protocol+"//"+location.host;const a={videoId:t,playerVars:o,events:{onReady:r=>{r.target.playVideo()}}};this.player=new YT.Player(this.playerEl,a)}}getPlayOptionsFromAttrs(t){const e={},i=+(t.getAttribute(`data-${this.config.namespace}-video-start-seconds`)||0);i>0&&(e.startTime=i);const s=t.getAttribute(`data-${this.config.namespace}-attribution`);s&&(e.attribution=s);const o=t.getAttribute(`data-${this.config.namespace}-orientation`);return o&&(e.orientation=o),e}setActive(t){t?(this.lastScrollY=window.pageYOffset,this.lastFocusedEl=document.activeElement,window.setTimeout(()=>{this.closeEl.focus(),this.config.onModalOpen&&this.config.onModalOpen(this)},this.config.transitionDuration+100)):window.setTimeout(()=>{this.lastFocusedEl&&(this.lastFocusedEl.focus(),this.lastFocusedEl=void 0),window.scrollTo({left:0,top:this.lastScrollY}),this.config.onModalClose&&this.config.onModalClose(this)},this.config.transitionDuration+100),this.setVisible(t)}getPlayer(){return this.player}dispose(){document.removeEventListener("click",this.handleEvent.bind(this)),document.removeEventListener("keydown",this.handleKeyboardEvent.bind(this)),this.modalEl&&this.container.removeChild(this.modalEl)}createDom(t,e,...i){const s=document.createElement(t);return s.className=e,i&&i.length&&i.forEach(o=>s.appendChild(o)),s}handleEvent(t){let e=t.target;for(;e;){const i=`data-${this.config.namespace}-video-id`,s=e.getAttribute(i);if(s){t.preventDefault();const o=this.getPlayOptionsFromAttrs(e);this.play(s,o);return}e=e.parentElement}}handleKeyboardEvent(t){if(this.isVisible){if(!this.player)return;t.key===n.ESC?this.setActive(!1):t.key===n.SPACE&&(this.player.getPlayerState()===YT.PlayerState.PLAYING?this.player.pauseVideo():this.player.playVideo())}else(t.key===n.ENTER||t.key===n.SPACE)&&this.handleEvent(t)}setVisible(t){window.setTimeout(()=>{this.player&&(t?this.player.getPlayerState()!==YT.PlayerState.PLAYING&&this.player.playVideo():this.player.pauseVideo())},100),window.setTimeout(()=>{this.modalEl.classList.toggle(`${this.config.namespace}--enabled`,t)},t?0:this.config.transitionDuration),window.setTimeout(()=>{this.modalEl.classList.toggle(`${this.config.namespace}--visible`,t),this.isVisible=t},t?this.config.transitionDuration:0)}};const b='.yt-modal{position:absolute;top:0;left:0;width:100%;height:100%;display:none;opacity:0;position:fixed;transform:scale(1.15);transition:.3s all cubic-bezier(.83,0,.17,1);visibility:hidden;z-index:10000}.yt-modal--enabled{display:block!important}.yt-modal--visible{opacity:1;transform:scale(1);visibility:visible}.yt-modal__x{position:absolute;top:0;right:0;width:64px;height:64px;background:#3c4043;border:none;cursor:pointer;text-align:center;transition:.3s all;z-index:10003}.yt-modal__x:hover{mix-blend-mode:invert}@media (min-width: 1024px){.yt-modal__x{position:absolute;top:48px;right:48px}}.yt-modal__x:before,.yt-modal__x:after{content:"";display:block;position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:3px;height:32px;background:#fff}.yt-modal__x:before{transform:translate(-50%,-50%) rotate(45deg)}.yt-modal__x:after{transform:translate(-50%,-50%) rotate(-45deg)}.yt-modal__x:focus{outline:none}.yt-modal__mask{position:absolute;top:0;left:0;width:100%;height:100%;background:#202124cc;z-index:10001}.yt-modal__player{border-radius:10px;height:calc(100vh - 7.39707vw);left:50%;max-height:43.767444375vw;max-width:calc((100vh - 7.39707vw) * 16 / 9);position:absolute;top:50%;transform:translate(-50%) translateY(-50%);width:77.80879vw;z-index:10002}',E=new g({namespace:"yt-modal",useHandlerOnMobile:!1,onModalClose:t=>{document.documentElement.removeAttribute("data-yt-modal-open"),t.getPlayer().stopVideo()}});let d=!1;class w{constructor(e){(this.host=e).addController(this)}init({videoId:e}){this.videoId=e}hostConnected(){this.host.requestUpdate()}addStylesToPage(){this.styleElement=this.styleElement||this.createStyleElement(),d=!0}open(){d||this.addStylesToPage(),this.modal=E,document.documentElement.setAttribute("data-yt-modal-open","true"),this.modal.play(this.videoId)}createStyleElement(){const e=document.createElement("style");return e.textContent=b,document.head.appendChild(e),e}hostDisconnected(){this.modal?.dispose(),this.styleElement?.remove()}}export{w as Y};