customElements.whenDefined("dialog-component").then(()=>{const DialogComponent=customElements.get("dialog-component");class RegistrationModal extends DialogComponent{#triggerElement=null;connectedCallback(){super.connectedCallback(),this.addEventListener("dialog:close",this.#onClose)}disconnectedCallback(){super.disconnectedCallback(),this.removeEventListener("dialog:close",this.#onClose),this.#form?.removeEventListener("submit",this.#onSubmit),this.#removeBlurListeners()}get#submitBtn(){return this.querySelector(".registration-modal__submit")}get#form(){return this.querySelector(".registration-modal__form")}get#firstField(){return this.querySelector(".registration-modal__field input")}get#successPanel(){return this.querySelector(".registration-modal__success")}get#formWrapper(){return this.querySelector(".registration-modal__form-wrapper")}get#syncEndpoint(){return this.dataset.syncEndpoint?.trim()||""}showDialog(){this.#triggerElement=document.activeElement,this.#resetState(),this.#form?.addEventListener("submit",this.#onSubmit),this.#addBlurListeners(),this.#addInputListeners(),super.showDialog(),requestAnimationFrame(()=>requestAnimationFrame(()=>this.#firstField?.focus()))}#onClose=()=>{this.#triggerElement instanceof HTMLElement&&this.#triggerElement.focus(),this.#triggerElement=null,this.#form?.removeEventListener("submit",this.#onSubmit),this.#removeBlurListeners(),this.#removeInputListeners()};#addInputListeners(){for(const field of this.querySelectorAll("[required]"))field.addEventListener("input",this.#checkFormValidity),field.addEventListener("change",this.#checkFormValidity)}#removeInputListeners(){for(const field of this.querySelectorAll("[required]"))field.removeEventListener("input",this.#checkFormValidity),field.removeEventListener("change",this.#checkFormValidity)}#checkFormValidity=()=>{const form=this.#form,btn=this.#submitBtn;!form||!btn||(btn.disabled=!form.checkValidity())};#addBlurListeners(){for(const field of this.querySelectorAll("[required]"))field.name!=="inp_4[]"&&field.addEventListener("blur",this.#onFieldBlur)}#removeBlurListeners(){for(const field of this.querySelectorAll("[required]"))field.name!=="inp_4[]"&&field.removeEventListener("blur",this.#onFieldBlur)}#onFieldBlur=event=>{const field=event.currentTarget,wrapper=field.closest(".registration-modal__field, .registration-modal__legal");this.#clearError(field,wrapper),field.checkValidity()||this.#showError(field,wrapper)};#onSubmit=async event=>{if(event.preventDefault(),!this.#validateForm())return;const form=this.#form;if(!form)return;const btn=this.querySelector(".registration-modal__submit");btn&&btn.setAttribute("aria-busy","true");const iframe=this.querySelector("iframe[data-registration-target]");try{await new Promise(resolve=>{let done=!1;const finish=()=>{done||(done=!0,resolve(void 0))};iframe&&iframe.addEventListener("load",finish,{once:!0}),setTimeout(finish,6e3),form.submit()});const payload=this.#buildSyncPayload(form);this.#showSuccess(),this.#dispatchRegistrationSuccess(payload),this.#sendSyncPayload(payload)}finally{btn&&btn.removeAttribute("aria-busy")}};#buildSyncPayload(form){const formData=new FormData(form),fields={};for(const[key,value]of formData.entries()){const v=typeof value=="string"?value.trim():"";if(Object.prototype.hasOwnProperty.call(fields,key)){const existing=fields[key];Array.isArray(existing)?existing.push(v):fields[key]=[existing,v]}else fields[key]=v}return{fields,meta:{source:"registration-modal",submitted_at:new Date().toISOString(),shopify_form:"emarsys"}}}#dispatchRegistrationSuccess(payload){this.dispatchEvent(new CustomEvent("registration-modal:registration-success",{detail:payload,bubbles:!0,composed:!0}))}#sendSyncPayload(payload){const endpoint=this.#syncEndpoint;endpoint&&fetch(endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(payload),keepalive:!0}).catch(error=>{console.warn("Registration modal sync failed",error)})}#validateForm(){const form=this.#form;if(!form)return!0;let firstInvalid=null;const dobInvalid=this.#validateDOB();dobInvalid instanceof HTMLElement&&(firstInvalid=dobInvalid);for(const field of form.querySelectorAll("[required]")){if(field.name==="inp_4[]")continue;const wrapper=field.closest(".registration-modal__field, .registration-modal__legal");this.#clearError(field,wrapper),field.checkValidity()||(this.#showError(field,wrapper),firstInvalid===null&&(firstInvalid=field))}return firstInvalid!==null&&firstInvalid.focus(),firstInvalid===null}#validateDOB(){const form=this.#form;if(!form)return null;const wrapper=form.querySelector("[data-dob-group]");if(!wrapper)return null;const inputs=form.querySelectorAll('[name="inp_4[]"]'),existingError=wrapper.querySelector(".registration-modal__error");existingError&&existingError.remove();for(const input of inputs)input.removeAttribute("aria-invalid"),input.removeAttribute("aria-describedby");let firstEmpty=null;for(const input of inputs)input.value.trim()||(input.setAttribute("aria-invalid","true"),firstEmpty===null&&(firstEmpty=input));if(firstEmpty===null)return null;const errorId="registration-dob-error",msg=document.createElement("span");msg.id=errorId,msg.setAttribute("role","alert"),msg.className="registration-modal__error",msg.textContent="Complet\xE1 d\xEDa, mes y a\xF1o",wrapper.appendChild(msg);for(const input of inputs)input.getAttribute("aria-invalid")==="true"&&input.setAttribute("aria-describedby",errorId);return firstEmpty}#showError(field,wrapper){const errorId=field.id+"-error";field.setAttribute("aria-invalid","true"),field.setAttribute("aria-describedby",errorId);let msg=wrapper?wrapper.querySelector("#"+errorId):null;msg||(msg=document.createElement("span"),msg.id=errorId,msg.setAttribute("role","alert"),msg.className="registration-modal__error",wrapper&&wrapper.appendChild(msg)),msg.textContent=field.validationMessage}#clearError(field,wrapper){if(field.removeAttribute("aria-invalid"),field.removeAttribute("aria-describedby"),wrapper){const msg=wrapper.querySelector(".registration-modal__error");msg&&msg.remove()}}#showSuccess(){const success=this.#successPanel,formWrapper=this.#formWrapper;!success||!formWrapper||(formWrapper.classList.add("is-hiding"),formWrapper.addEventListener("transitionend",()=>{formWrapper.hidden=!0,formWrapper.classList.remove("is-hiding"),success.hidden=!1,this.classList.add("is-success");const focusTarget=success.querySelector("img, [tabindex]")||success;focusTarget instanceof HTMLElement&&focusTarget.focus()},{once:!0}))}#resetState(){const success=this.#successPanel,formWrapper=this.#formWrapper;if(!success||!formWrapper)return;success.hidden=!0,formWrapper.hidden=!1,formWrapper.classList.remove("is-hiding"),this.classList.remove("is-success");const form=this.#form;if(form){form.reset();for(const field of form.querySelectorAll("[required]")){const wrapper=field.closest(".registration-modal__field, .registration-modal__legal");this.#clearError(field,wrapper)}}}}customElements.get("registration-modal")||customElements.define("registration-modal",RegistrationModal),document.addEventListener("click",event=>{if(!event.target.closest("[data-open-newsletter-modal]"))return;const modal=document.querySelector("registration-modal");modal instanceof RegistrationModal&&modal.showDialog()})}); //# sourceMappingURL=/cdn/shop/t/35/assets/gco-registration-modal.js.map?v=167096070493188138911792161662