import{proxyCustomElement,HTMLElement,createEvent,h}from"@stencil/core/internal/client";import{b as isIOS}from"./util.js";import{d as defineCustomElement$1}from"./presto-video2.js";const prestoYoutubeCss=":host{display:block}.fallback-container{position:relative;padding-bottom:56.25%;padding-top:30px;height:0;overflow:hidden}.fallback-container iframe,.fallback-container object,.fallback-container embed{position:absolute;top:0;left:0;width:100%;height:100%}@supports not (aspect-ratio: 16/9){.plyr__video-embed,.plyr__video-wrapper--fixed-ratio{height:0 !important;padding-bottom:calc(100% / (var(--presto-player-aspect-ratio, 16/9)));position:relative}}.plyr--youtube{aspect-ratio:var(--presto-player-aspect-ratio, auto)}",PrestoYoutube=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.reload=createEvent(this,"reload",7)}handlePlayerChange(){this.player&&(this.fixes(),this.setPoster())}fixes(){this.player.once("statechange",e=>{var t,o,a,i,r;1===e.detail.code&&((null===(r=null===(i=null===(a=null===(o=null===(t=null==e?void 0:e.detail)||void 0===t?void 0:t.plyr)||void 0===o?void 0:o.config)||void 0===a?void 0:a.blockAttributes)||void 0===i?void 0:i.mutedPreview)||void 0===r?void 0:r.enabled)||(this.player.muted=!1))})}getId(e){var t;const o=(e||"").match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=|shorts\/)([^#&?]*).*/);return o&&11===(null===(t=null==o?void 0:o[2])||void 0===t?void 0:t.length)?o[2]:null}loadPlayer(){this.lazyLoad=!1,this.reloadPlayer=!0}componentDidRender(){this.reloadPlayer&&(this.reloadPlayer=!1,this.reload.emit("play"))}setWebView(){var e;let t=null===(e=window.navigator)||void 0===e?void 0:e.standalone,o=window.navigator.userAgent.toLowerCase(),a=/safari/.test(o),i=/iphone|ipod|ipad/.test(o);this.isWebView=i?!t&&!a:o.includes("wv")}loadImage(e,t=1){return new Promise((o,a)=>{const i=new Image,r=()=>{delete i.onload,delete i.onerror,(i.naturalWidth>=t?o:a)(e)};Object.assign(i,{onload:r,onerror:r,src:e})})}setPoster(){if(!this.player.poster){let e=e=>`https://i.ytimg.com/vi/${this.getId(this.src)}/${e}default.jpg`;this.loadImage(e("maxres"),121).catch(()=>this.loadImage(e("sd"),121)).catch(()=>this.loadImage(e("hq"),121)).then(e=>this.player.poster=e).catch(()=>{})}}componentWillLoad(){this.setWebView(),this.lazyLoad&&isIOS()&&(this.lazyLoad=!1)}render(){return this.isWebView?h("div",{class:"fallback-container"},h("iframe",{src:this.src,allowFullScreen:!0,allowtransparency:!0,allow:"autoplay"})):this.lazyLoad?h("div",null,h("presto-video",{part:"video",getRef:this.getRef,src:"",provider:"youtube"}),h("div",{class:"presto-player__play-cover",onClick:()=>this.loadPlayer()})):h("div",{class:"plyr__video-embed",part:"embed",ref:this.getRef,"data-plyr-provider":"youtube","data-plyr-embed-id":this.getId(this.src)})}static get watchers(){return{player:["handlePlayerChange"]}}static get style(){return prestoYoutubeCss}},[256,"presto-youtube",{src:[1],poster:[1025],lazyLoad:[1028,"lazy-load"],player:[8],getRef:[1040,"get-ref"],reloadPlayer:[32],isWebView:[32]},void 0,{player:["handlePlayerChange"]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["presto-youtube","presto-video"].forEach(e=>{switch(e){case"presto-youtube":customElements.get(e)||customElements.define(e,PrestoYoutube);break;case"presto-video":customElements.get(e)||defineCustomElement$1()}})}export{PrestoYoutube as P,defineCustomElement as d};