import{proxyCustomElement,HTMLElement,createEvent,h}from"@stencil/core/internal/client";import{i 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)}",PrestoYoutubeStyle0=prestoYoutubeCss,PrestoYoutube=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.reload=createEvent(this,"reload",7),this.src=void 0,this.poster=void 0,this.lazyLoad=void 0,this.player=void 0,this.getRef=void 0,this.reloadPlayer=void 0,this.isWebView=void 0}handlePlayerChange(){this.player&&(this.fixes(),this.setPoster())}fixes(){this.player.once("statechange",(e=>{var t,o,i,a,s;1===e.detail.code&&((null===(s=null===(a=null===(i=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===i?void 0:i.blockAttributes)||void 0===a?void 0:a.mutedPreview)||void 0===s?void 0:s.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(),i=/safari/.test(o),a=/iphone|ipod|ipad/.test(o);this.isWebView=a?!t&&!i:o.includes("wv")}loadImage(e,t=1){return new Promise(((o,i)=>{const a=new Image,s=()=>{delete a.onload,delete a.onerror,(a.naturalWidth>=t?o:i)(e)};Object.assign(a,{onload:s,onerror:s,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 PrestoYoutubeStyle0}},[0,"presto-youtube",{src:[1],poster:[1025],lazyLoad:[1028,"lazy-load"],player:[8],getRef:[1040],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};