import{h}from"@stencil/core";import{isIOS}from"../../../../util";export class PrestoYoutube{constructor(){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,l,r,i;1===e.detail.code&&((null===(i=null===(r=null===(l=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===l?void 0:l.blockAttributes)||void 0===r?void 0:r.mutedPreview)||void 0===i?void 0:i.enabled)||(this.player.muted=!1))}))}getId(e){var t;const o=(e||"").match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/);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(),l=/safari/.test(o),r=/iphone|ipod|ipad/.test(o);this.isWebView=r?!t&&!l:o.includes("wv")}loadImage(e,t=1){return new Promise(((o,l)=>{const r=new Image,i=()=>{delete r.onload,delete r.onerror,(r.naturalWidth>=t?o:l)(e)};Object.assign(r,{onload:i,onerror:i,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",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"src",reflect:!1},poster:{type:"string",mutable:!0,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"poster",reflect:!1},lazyLoad:{type:"boolean",mutable:!0,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"lazy-load",reflect:!1},player:{type:"any",mutable:!1,complexType:{original:"any",resolved:"any",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"player",reflect:!1},getRef:{type:"unknown",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:""}}}}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"}]}}