import{proxyCustomElement,HTMLElement,createEvent,h}from"@stencil/core/internal/client";import{t as timePassed}from"./util.js";import{d as defineCustomElement$2}from"./presto-email-overlay-ui2.js";import{d as defineCustomElement$1}from"./presto-spinner.js";const prestoEmailOverlayControllerCss=":host{display:block}",PrestoEmailOverlayController=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.playVideo=createEvent(this,"playVideo",7),this.pauseVideo=createEvent(this,"pauseVideo",7),this.restartVideo=createEvent(this,"restartVideo",7),this.emailStateChange=createEvent(this,"emailStateChange",7),this.percentagePassed=0}setStorage(e){window.localStorage.setItem("presto.videos.email_collection",JSON.stringify({[this.videoId]:e}))}getStorage(){return window.localStorage.getItem("presto.videos.email_collection")}componentWillLoad(){this.handleDuration()}handleDuration(){var e;this.enabled=!this.getStorage()&&(null===(e=null==this?void 0:this.emailCollection)||void 0===e?void 0:e.enabled),this.handleTimeCheck()}handleTimeCheck(){this.enabled&&(this.getStorage()||this.checkTime())}handleShowChange(){this.show&&this.pauseVideo.emit(!0)}checkTime(){var e;this.show=timePassed({current:this.currentTime,duration:this.duration,showAfter:(null===(e=null==this?void 0:this.emailCollection)||void 0===e?void 0:e.percentage)||0})}async getNonce(){var e;return fetch(`${null===(e=null===window||void 0===window?void 0:window.prestoPlayer)||void 0===e?void 0:e.ajaxurl}?action=presto_refresh_progress_nonce`)}async submit(e){var t;this.loading=!0,this.error="";const i=await this.getNonce(),{data:o}=await i.json();try{let i=await fetch(null===(t=null===window||void 0===window?void 0:window.prestoPlayer)||void 0===t?void 0:t.ajaxurl,{method:"post",body:new URLSearchParams({action:"presto_player_email_submit",nonce:o,preset_id:this.presetId,video_id:this.videoId,provider:this.provider,...(null==e?void 0:e.detail)||{}})});const{success:l,data:s}=await i.json();if(!l)throw s;this.setStorage("collected"),this.show=!1,this.playVideo.emit()}catch(e){const t=null==e?void 0:e[0];t&&"string"==typeof t&&(this.error=t)}finally{this.loading=!1}}skip(){this.setStorage("skipped"),this.show=!1,this.playVideo.emit()}handleEmailStateChange(e){this.emailStateChange.emit(e)}render(){var e,t,i,o,l,s,n,r;if(this.show)return h("presto-email-overlay-ui",{style:{...(null===(e=null==this?void 0:this.emailCollection)||void 0===e?void 0:e.button_color)?{"--presto-player-button-color":`${null===(t=null==this?void 0:this.emailCollection)||void 0===t?void 0:t.button_color}`}:{},...(null===(i=null==this?void 0:this.emailCollection)||void 0===i?void 0:i.button_text_color)?{"--presto-player-button-text":`${null===(o=null==this?void 0:this.emailCollection)||void 0===o?void 0:o.button_text_color}`}:{}},direction:this.direction,class:"email-overlay",headline:null===(l=null==this?void 0:this.emailCollection)||void 0===l?void 0:l.headline,bottomText:null===(s=null==this?void 0:this.emailCollection)||void 0===s?void 0:s.bottom_text,allowSkip:null===(n=null==this?void 0:this.emailCollection)||void 0===n?void 0:n.allow_skip,buttonText:null===(r=null==this?void 0:this.emailCollection)||void 0===r?void 0:r.button_text,isLoading:this.loading,errorMessage:this.error,onSubmitForm:e=>this.submit(e),onSkip:()=>this.skip(),i18n:this.i18n,provider:this.provider})}static get watchers(){return{duration:["handleDuration"],currentTime:["handleTimeCheck","handleShowChange"],show:["handleEmailStateChange"]}}static get style(){return":host{display:block}"}},[256,"presto-email-overlay-controller",{ended:[4],currentTime:[514,"current-time"],duration:[514],direction:[1],emailCollection:[16,"email-collection"],i18n:[16,"i-1-8n"],videoId:[2,"video-id"],presetId:[2,"preset-id"],provider:[1],enabled:[32],show:[32],loading:[32],error:[32],percentagePassed:[32]},void 0,{duration:["handleDuration"],currentTime:["handleTimeCheck","handleShowChange"],show:["handleEmailStateChange"]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["presto-email-overlay-controller","presto-email-overlay-ui","presto-player-spinner"].forEach(e=>{switch(e){case"presto-email-overlay-controller":customElements.get(e)||customElements.define(e,PrestoEmailOverlayController);break;case"presto-email-overlay-ui":customElements.get(e)||defineCustomElement$2();break;case"presto-player-spinner":customElements.get(e)||defineCustomElement$1()}})}export{PrestoEmailOverlayController as P,defineCustomElement as d};