import{h}from"@stencil/core";import{isIOS}from"../../../../util";export class PrestoYoutube{handlePlayerChange(){this.player&&(this.fixes(),this.setPoster())}fixes(){this.player.once("statechange",e=>{var t,r,l,o,a;1===e.detail.code&&((null===(a=null===(o=null===(l=null===(r=null===(t=null==e?void 0:e.detail)||void 0===t?void 0:t.plyr)||void 0===r?void 0:r.config)||void 0===l?void 0:l.blockAttributes)||void 0===o?void 0:o.mutedPreview)||void 0===a?void 0:a.enabled)||(this.player.muted=!1))})}getId(e){var t;const r=(e||"").match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=|shorts\/)([^#&?]*).*/);return r&&11===(null===(t=null==r?void 0:r[2])||void 0===t?void 0:t.length)?r[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,r=window.navigator.userAgent.toLowerCase(),l=/safari/.test(r),o=/iphone|ipod|ipad/.test(r);this.isWebView=o?!t&&!l:r.includes("wv")}loadImage(e,t=1){return new Promise((r,l)=>{const o=new Image,a=()=>{delete o.onload,delete o.onerror,(o.naturalWidth>=t?r:l)(e)};Object.assign(o,{onload:a,onerror:a,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 is(){return"presto-youtube"}static get originalStyleUrls(){return{$:["presto-youtube.css"]}}static get styleUrls(){return{$:["presto-youtube.css"]}}static get properties(){return{src:{type:"string",attribute:"src",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},getter:!1,setter:!1,reflect:!1},poster:{type:"string",attribute:"poster",mutable:!0,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},getter:!1,setter:!1,reflect:!1},lazyLoad:{type:"boolean",attribute:"lazy-load",mutable:!0,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},getter:!1,setter:!1,reflect:!1},player:{type:"any",attribute:"player",mutable:!1,complexType:{original:"any",resolved:"any",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},getter:!1,setter:!1,reflect:!1},getRef:{type:"unknown",attribute:"get-ref",mutable:!0,complexType:{original:"(elm?: HTMLIFrameElement | HTMLVideoElement | HTMLDivElement) => void",resolved:"(elm?: HTMLDivElement | HTMLVideoElement | HTMLIFrameElement) => void",references:{HTMLIFrameElement:{location:"global",id:"global::HTMLIFrameElement"},HTMLVideoElement:{location:"global",id:"global::HTMLVideoElement"},HTMLDivElement:{location:"global",id:"global::HTMLDivElement"}}},required:!1,optional:!0,docs:{tags:[],text:""},getter:!1,setter:!1}}}static get states(){return{reloadPlayer:{},isWebView:{}}}static get events(){return[{method:"reload",name:"reload",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Events"},complexType:{original:"string",resolved:"string",references:{}}}]}static get watchers(){return[{propName:"player",methodName:"handlePlayerChange"}]}}