import{proxyCustomElement,HTMLElement,createEvent,h,Host}from"@stencil/core/internal/client";import{d as createOrUpdateCheckout}from"./index4.js";import{b as getValueFromUrl}from"./util.js";import{s as state$1}from"./store4.js";import{s as state,o as onChange}from"./mutations2.js";import{d as defineCustomElement$4}from"./sc-form-control2.js";import{d as defineCustomElement$3}from"./sc-input2.js";import{d as defineCustomElement$2}from"./sc-visually-hidden2.js";const scCustomerEmailCss=":host{display:block}a{color:var(--sc-color-primary-500)}a.customer-email__login-link{color:var(--sc-customer-login-link-color, var(--sc-input-placeholder-color));text-decoration:none;font-size:var(--sc-font-size-small)}.tracking-confirmation-message{font-size:var(--sc-font-size-xx-small)}.tracking-confirmation-message span{opacity:0.75}",ScCustomerEmailStyle0=scCustomerEmailCss,ScCustomerEmail$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.scChange=createEvent(this,"scChange",7),this.scClear=createEvent(this,"scClear",7),this.scInput=createEvent(this,"scInput",7),this.scFocus=createEvent(this,"scFocus",7),this.scBlur=createEvent(this,"scBlur",7),this.scUpdateOrderState=createEvent(this,"scUpdateOrderState",7),this.scUpdateAbandonedCart=createEvent(this,"scUpdateAbandonedCart",7),this.scLoginPrompt=createEvent(this,"scLoginPrompt",7),this.trackingConfirmationMessage=void 0,this.size="medium",this.value=getValueFromUrl("email"),this.pill=!1,this.label=void 0,this.showLabel=!0,this.help="",this.placeholder=void 0,this.disabled=!1,this.readonly=!1,this.required=!1,this.invalid=!1,this.autofocus=void 0,this.hasFocus=void 0}async handleChange(){this.value=this.input.value,this.scChange.emit();try{state.checkout=await createOrUpdateCheckout({id:state.checkout.id,data:{email:this.input.value}})}catch(e){console.log(e)}}async reportValidity(){var e,t;return null===(t=null===(e=this.input)||void 0===e?void 0:e.reportValidity)||void 0===t?void 0:t.call(e)}handleSessionChange(){var e,t,s,i,a,o;if(this.value&&!state$1.loggedIn)return;if(state$1.loggedIn)return void(this.value=state$1.email||(null===(t=null===(e=null==state?void 0:state.checkout)||void 0===e?void 0:e.customer)||void 0===t?void 0:t.email)||(null===(s=null==state?void 0:state.checkout)||void 0===s?void 0:s.email));const n=getValueFromUrl("email");state$1.loggedIn||!n?this.value=(null===(i=null==state?void 0:state.checkout)||void 0===i?void 0:i.email)||(null===(o=null===(a=null==state?void 0:state.checkout)||void 0===a?void 0:a.customer)||void 0===o?void 0:o.email):this.value=n}componentWillLoad(){this.handleSessionChange(),this.removeCheckoutListener=onChange("checkout",(()=>this.handleSessionChange()))}disconnectedCallback(){this.removeCheckoutListener()}renderOptIn(){return this.trackingConfirmationMessage?!1!==state.abandonedCheckoutEnabled?h("div",{class:"tracking-confirmation-message"},h("span",null,this.trackingConfirmationMessage)," ",h("a",{href:"#",onClick:e=>{e.preventDefault(),this.scUpdateAbandonedCart.emit(!1)}},wp.i18n.__("No Thanks","surecart"))):h("div",{class:"tracking-confirmation-message"},h("span",null," ",wp.i18n.__("You won't receive further emails from us.","surecart"))):null}render(){var e;return h(Host,{key:"bc967aac9ef137a1aa3934eb69af1baea87c7179"},h("sc-input",{key:"28c179e737e0286dda531acdf90c26531301d7de",exportparts:"base, input, form-control, label, help-text, prefix, suffix",type:"email",name:"email",ref:e=>this.input=e,value:this.value,help:this.help,label:this.label,autocomplete:"email",placeholder:this.placeholder,disabled:this.disabled||!!state$1.loggedIn&&!!(null===(e=this.value)||void 0===e?void 0:e.length)&&!this.invalid,readonly:this.readonly,required:!0,invalid:this.invalid,autofocus:this.autofocus,hasFocus:this.hasFocus,onScChange:()=>this.handleChange(),onScInput:()=>this.scInput.emit(),onScFocus:()=>this.scFocus.emit(),onScBlur:()=>this.scBlur.emit()}),this.renderOptIn())}static get style(){return ScCustomerEmailStyle0}},[1,"sc-customer-email",{trackingConfirmationMessage:[1,"tracking-confirmation-message"],size:[513],value:[1025],pill:[516],label:[1],showLabel:[4,"show-label"],help:[1],placeholder:[1],disabled:[516],readonly:[516],required:[516],invalid:[1540],autofocus:[4],hasFocus:[1540,"has-focus"],reportValidity:[64]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-customer-email","sc-form-control","sc-input","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-customer-email":customElements.get(e)||customElements.define(e,ScCustomerEmail$1);break;case"sc-form-control":customElements.get(e)||defineCustomElement$4();break;case"sc-input":customElements.get(e)||defineCustomElement$3();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$2()}}))}const ScCustomerEmail=ScCustomerEmail$1,defineCustomElement=defineCustomElement$1;export{ScCustomerEmail,defineCustomElement};