1if(!customElements.get("bottom-sheet-lead-form")){class BottomSheetLeadForm extends HTMLElement{static selectors=Object.freeze({form:".bottom-sheet-lead-form__form",modeSwitch:"[data-lead-mode-switch]",offerStateDescription:"[data-offer-state-description]",discountCodeMessage:"[data-success-message]",termsLabel:".bottom-sheet-lead-form__terms-label",errorOutput:".bottom-sheet-lead-form__output",submitButton:".bottom-sheet-lead-form__button--submit"});static buttonVariants=Object.freeze({default:"default",success:"success",error:"error"});static controls=Object.freeze({languageIso:"language_iso",languageRootUrl:"language_root_url",channel:"channel",phone:"phone",email:"email",terms:"agree-to-terms",trackingConsent:"agree-to-tracking"});static modes=Object.freeze({emailWhatsapp:"email_whatsapp",emailOnly:"email_only"});static offerStates=Object.freeze({emailWhatsapp:"email_whatsapp",emailOnlyWithPhoneAvailable:"email_only_with_phone_available",emailOnlyWhenPhoneHidden:"email_only_when_phone_hidden"});static events=Object.freeze({leadSubmitSuccess:"lead-submit-success",leadSubmitError:"lead-submit-error"});static timeouts=Object.freeze({buttonMessageDuration:3e3,buttonMessageDurationExtended:5e3});static klaviyo=Object.freeze({companyId:"RH49u6",listId:"S8i8a7",revision:"2026-01-15",consentVersion:"popup-bottom-sheet-v1-2026-08-14"});static consentLog=Object.freeze({endpoint:"https://erebor.beglarianfabrics.com/webhook/bf/email-consent"});static errorTypes=Object.freeze({validation:"validation",network:"network",server:"server",client:"client"});#isSending=!1;#isCopyMode=!1;#buttonMessageTimeout=null;connectedCallback(){const form=this.querySelector(BottomSheetLeadForm.selectors.form);this.elements={form,languageIsoInput:form?.elements[BottomSheetLeadForm.controls.languageIso],languageRootUrlInput:form?.elements[BottomSheetLeadForm.controls.languageRootUrl],channelInput:form?.elements[BottomSheetLeadForm.controls.channel],phoneInput:form?.elements[BottomSheetLeadForm.controls.phone],emailInput:form?.elements[BottomSheetLeadForm.controls.email],termsInput:form?.elements[BottomSheetLeadForm.controls.terms],termsLabel:form?.querySelector(BottomSheetLeadForm.selectors.termsLabel),trackingConsentInput:form?.elements[BottomSheetLeadForm.controls.trackingConsent],submitButton:form?.querySelector(BottomSheetLeadForm.selectors.submitButton),output:form?.querySelector(BottomSheetLeadForm.selectors.errorOutput),successMessageElement:this.querySelector(BottomSheetLeadForm.selectors.discountCodeMessage),modeSwitchButtons:Array.from(this.querySelectorAll(BottomSheetLeadForm.selectors.modeSwitch)),offerStateDescriptionElements:Array.from(this.querySelectorAll(BottomSheetLeadForm.selectors.offerStateDescription))},this.hidePhone=this.dataset.hidePhone==="true";const requiredElements=[this.elements.form,this.elements.languageIsoInput,this.elements.languageRootUrlInput,this.elements.channelInput,this.elements.emailInput,this.elements.termsInput,this.elements.termsLabel,this.elements.submitButton,this.elements.output];if(this.hidePhone||requiredElements.push(this.elements.phoneInput),!requiredElements.every(element=>element!==null))return;this.supportedPhoneCodes=(this.dataset.supportedPhoneCodes||"").split(",").map(code=>code.trim()).filter(Boolean),this.offerConfig={[BottomSheetLeadForm.offerStates.emailWhatsapp]:{submitLabel:this.dataset.submitLabelDual||"",successMessage:this.dataset.successMessageDual||""},[BottomSheetLeadForm.offerStates.emailOnlyWithPhoneAvailable]:{submitLabel:this.dataset.submitLabelEmailOnlyWithPhoneAvailable||"",successMessage:this.dataset.successMessageEmailOnlyWithPhoneAvailable||""},[BottomSheetLeadForm.offerStates.emailOnlyWhenPhoneHidden]:{submitLabel:this.dataset.submitLabelEmailOnlyWhenPhoneHidden||"",successMessage:this.dataset.successMessageEmailOnlyWhenPhoneHidden||""}},this.messages={errorPhone:this.dataset.formErrorPhone,errorEmail:this.dataset.formErrorEmail,errorTerms:this.dataset.formErrorTerms,errorGeneric:this.dataset.formError,errorCountryNotSupported:this.dataset.countryNotSupported},this.onModeSwitchClick=this.onModeSwitchClick.bind(this),this.onSubmit=this.onSubmit.bind(this),this.elements.form.addEventListener("click",this.onModeSwitchClick),this.elements.form.addEventListener("submit",this.onSubmit);const initialMode=this.resolveMode(this.dataset.modeDefault||this.elements.channelInput.value);this.setMode(initialMode,{resetCopyMode:!1})}disconnectedCallback(){this.elements?.form&&(this.elements.form.removeEventListener("click",this.onModeSwitchClick),this.elements.form.removeEventListener("submit",this.onSubmit),this.resetButtons())}resolveMode(mode){const normalized=String(mode||"").toLowerCase();return normalized==="whatsapp"?BottomSheetLeadForm.modes.emailWhatsapp:normalized==="email"?BottomSheetLeadForm.modes.emailOnly:normalized!==BottomSheetLeadForm.modes.emailWhatsapp&&normalized!==BottomSheetLeadForm.modes.emailOnly?this.hidePhone?BottomSheetLeadForm.modes.emailOnly:BottomSheetLeadForm.modes.emailWhatsapp:this.hidePhone&&normalized===BottomSheetLeadForm.modes.emailWhatsapp?BottomSheetLeadForm.modes.emailOnly:normalized}resolveOfferState(mode=this.currentMode){return mode===BottomSheetLeadForm.modes.emailWhatsapp?BottomSheetLeadForm.offerStates.emailWhatsapp:this.hidePhone?BottomSheetLeadForm.offerStates.emailOnlyWhenPhoneHidden:BottomSheetLeadForm.offerStates.emailOnlyWithPhoneAvailable}normalizeOfferState(value){const normalized=String(value||"").toLowerCase();return normalized===BottomSheetLeadForm.offerStates.emailWhatsapp||normalized===BottomSheetLeadForm.offerStates.emailOnlyWithPhoneAvailable||normalized===BottomSheetLeadForm.offerStates.emailOnlyWhenPhoneHidden?normalized:normalized===BottomSheetLeadForm.modes.emailWhatsapp||normalized==="whatsapp"?BottomSheetLeadForm.offerStates.emailWhatsapp:normalized===BottomSheetLeadForm.modes.emailOnly||normalized==="email"?this.hidePhone?BottomSheetLeadForm.offerStates.emailOnlyWhenPhoneHidden:BottomSheetLeadForm.offerStates.emailOnlyWithPhoneAvailable:this.resolveOfferState()}setMode(nextMode,{resetCopyMode=!0}={}){const mode=this.resolveMode(nextMode),isDualMode=mode===BottomSheetLeadForm.modes.emailWhatsapp,offerState=this.resolveOfferState(mode);this.currentMode=mode,this.currentOfferState=offerState,this.elements.form.dataset.channel=mode,this.elements.form.dataset.leadState=offerState,this.elements.form.dataset.hidePhone=String(this.hidePhone),this.elements.channelInput.value=mode,this.elements.phoneInput&&(this.elements.phoneInput.disabled=this.hidePhone||!isDualMode,this.elements.phoneInput.required=!this.hidePhone&&isDualMode,this.elements.phoneInput.setCustomValidity("")),this.elements.emailInput.setCustomValidity(""),this.elements.termsInput.setCustomValidity(""),this.errorMessage="",this.updateOfferText(offerState),this.updateOfferDescriptions(offerState),this.updateModeSwitches(mode),resetCopyMode&&(this.isCopyMode=!1)}updateOfferText(offerState){const config=this.offerConfig[offerState]||{},submitLabel=config.submitLabel||this.elements.submitButton.dataset.defaultText||"";this.elements.submitButton.dataset.defaultText=submitLabel,this.elements.submitButton.firstElementChild&&!this.isSending&&!this.isCopyMode&&(this.elements.submitButton.firstElementChild.textContent=submitLabel),this.elements.successMessageElement&&(this.elements.successMessageElement.textContent=config.successMessage||"")}updateOfferDescriptions(offerState){this.elements.offerStateDescriptionElements.forEac
1h(element=>{const elementOfferState=this.normalizeOfferState(element.dataset.offerStateDescription);element.hidden=elementOfferState!==offerState})}updateModeSwitches(mode){this.elements.modeSwitchButtons.forEach(button=>{if(this.hidePhone){button.hidden=!0;return}const visibleForMode=this.resolveMode(button.dataset.visibleWhen);button.hidden=visibleForMode!==mode})}onModeSwitchClick(event){const switchButton=event.target.closest(BottomSheetLeadForm.selectors.modeSwitch);if(!switchButton||this.isSending)return;const modeTarget=this.resolveMode(switchButton.dataset.modeTarget);modeTarget!==this.currentMode&&this.setMode(modeTarget,{resetCopyMode:!0})}async onSubmit(event){if(event.preventDefault(),this.isSending)return;if(this.setButtonMessage(this.elements.submitButton.dataset.defaultText,BottomSheetLeadForm.buttonVariants.default,0),!this.validate()){this.setButtonMessage("",BottomSheetLeadForm.buttonVariants.error,BottomSheetLeadForm.timeouts.buttonMessageDuration),this.emit(BottomSheetLeadForm.events.leadSubmitError,{channel:this.currentOfferState,error_type:BottomSheetLeadForm.errorTypes.validation});return}this.errorMessage="",this.isSending=!0;const mode=this.currentMode,offerState=this.currentOfferState||this.resolveOfferState(mode),normalizedPhone=this.elements.phoneInput?this.normalizePhone(this.elements.phoneInput.value):"",email=this.elements.emailInput.value.trim(),basePayload={language_iso:this.elements.languageIsoInput.value,language_root_url:this.elements.languageRootUrlInput.value,channel:offerState,phone:normalizedPhone,email,trackingConsent:this.elements.trackingConsentInput?.checked||!1};try{mode===BottomSheetLeadForm.modes.emailWhatsapp?await Promise.all([this.sendEmailToKlaviyo({...basePayload,phone:""}),this.sendWhatsappToWebhook({...basePayload,channel:"whatsapp"})]):await Promise.all([this.sendEmailToKlaviyo({...basePayload,phone:""}),this.sendConsentLog(basePayload)]),this.resetForm(),this.setMode(mode,{resetCopyMode:!1}),this.isCopyMode=!0,this.emit(BottomSheetLeadForm.events.leadSubmitSuccess,{channel:offerState})}catch(err){let errorType=err?.errorType||BottomSheetLeadForm.errorTypes.server;"cause"in err&&err?.cause?.errors?.[0]?.code==="invalid"?(errorType=BottomSheetLeadForm.errorTypes.validation,this.errorMessage=this.messages.errorPhone,this.setButtonMessage("",BottomSheetLeadForm.buttonVariants.error,BottomSheetLeadForm.timeouts.buttonMessageDurationExtended)):errorType===BottomSheetLeadForm.errorTypes.validation?(this.errorMessage=err.userMessage||err.message||this.messages.errorGeneric,this.setButtonMessage("",BottomSheetLeadForm.buttonVariants.error,BottomSheetLeadForm.timeouts.buttonMessageDurationExtended)):(this.errorMessage="",this.setButtonMessage(err.userMessage||this.messages.errorGeneric,BottomSheetLeadForm.buttonVariants.error,BottomSheetLeadForm.timeouts.buttonMessageDurationExtended));const emittedErrorType=mode===BottomSheetLeadForm.modes.emailWhatsapp&&errorType===BottomSheetLeadForm.errorTypes.network?BottomSheetLeadForm.errorTypes.server:errorType;this.emit(BottomSheetLeadForm.events.leadSubmitError,{channel:offerState,error_type:emittedErrorType}),this.isSending=!1}}resetButtons({resetSubmit=!0}={}){clearTimeout(this.#buttonMessageTimeout),resetSubmit&&this.setButtonMessage(this.elements.submitButton.dataset.defaultText,BottomSheetLeadForm.buttonVariants.default,0)}isSupportedPhoneCountry(phone){return this.supportedPhoneCodes.length===0?!0:this.supportedPhoneCodes.some(code=>phone.startsWith(code))}set isSending(value){this.#isSending=!!value,this.#isSending?this.elements.submitButton.setAttribute("aria-busy","true"):this.elements.submitButton.removeAttribute("aria-busy")}get isSending(){return this.#isSending}set isCopyMode(value){this.#isCopyMode=!!value,this.elements.form.dataset.copyMode=String(this.#isCopyMode),this.resetButtons({resetSubmit:!this.#isCopyMode})}get isCopyMode(){return this.#isCopyMode}set errorMessage(message){this.elements.output.hidden=message==="",setTimeout(()=>{this.elements.output.textContent=message},0)}get errorMessage(){return this.elements.output.textContent||""}setButtonMessage(message,variant=BottomSheetLeadForm.buttonVariants.default,duration=BottomSheetLeadForm.timeouts.buttonMessageDuration){clearTimeout(this.#buttonMessageTimeout),message&&(this.elements.submitButton.firstElementChild.textContent=message),this.elements.submitButton.dataset.variant=variant,!(duration<=0)&&(this.#buttonMessageTimeout=setTimeout(()=>{message&&(this.elements.submitButton.firstElementChild.textContent=this.elements.submitButton.dataset.defaultText),this.elements.submitButton.dataset.variant=BottomSheetLeadForm.buttonVariants.default},duration))}resetForm(){this.elements.phoneInput&&(this.elements.phoneInput.value="",this.elements.phoneInput.setCustomValidity("")),this.elements.emailInput.value="",this.elements.termsInput.checked=!1,this.elements.emailInput.setCustomValidity(""),this.elements.termsInput.setCustomValidity(""),this.errorMessage="",this.isCopyMode=!1,this.isSending=!1}validate(){const isDualMode=this.currentMode===BottomSheetLeadForm.modes.emailWhatsapp&&this.elements.phoneInput&&!this.elements.phoneInput.disabled;this.elements.phoneInput&&this.elements.phoneInput.setCustomValidity(""),this.elements.emailInput.setCustomValidity(""),this.elements.termsInput.setCustomValidity("");const normalizedPhone=this.elements.phoneInput?this.normalizePhone(this.elements.phoneInput.value):"",isPhoneValid=/^\+\d{8,15}$/.test(normalizedPhone);return isDualMode&&!isPhoneValid?(this.errorMessage=this.messages.errorPhone,this.elements.phoneInput.setCustomValidity(this.messages.errorPhone),this.elements.phoneInput.focus(),!1):isDualMode&&!this.isSupportedPhoneCountry(normalizedPhone)?(this.errorMessage=this.messages.errorCountryNotSupported,this.elements.phoneInput.setCustomValidity(this.messages.errorCountryNotSupported),this.elements.phoneInput.focus(),!1):this.elements.emailInput.checkValidity()?this.elements.termsInput.checkValidity()?(this.errorMessage="",!0):(this.errorMessage=this.messages.errorTerms,this.elements.termsInput.setCustomValidity(this.messages.errorTerms),this.elements.termsInput.focus(),!1):(this.errorMessage=this.messages.errorEmail,this.elements.emailInput.setCustomValidity(this.messages.errorEmail),this.elements.emailInput.focus(),!1)}normalizePhone(phone){return phone.trim().replace(/[^0-9+]/g,"")}async sendEmailToKlaviyo(payload){const headers={accept:"application/vnd.api+json","content-type":"application/vnd.api+json",revision:BottomSheetLeadForm.klaviyo.revision};let body=null;try{body=JSON.stringify({data:{type:"subscription",attributes:{profile:{data:{type:"profile",attributes:{...payload.email?{email:payload.email}:{},...payload.phone?{phone_number:payload.phone}:{},subscriptions:{...payload.email?{email:{marketing:{consent:"SUBSCRIBED"}}}:{}},properties:{source:"Bottom Sheet Form",channel:payload.channel,language_iso:payload.language_iso,language_root_url:payload.language_root_url,email_tracking_consent:payload.trackingConsent||!1,consent_version:BottomSheetLeadForm.klaviyo.consentVersion}}}},custom_source:"Bottom Sheet Form"},relationships:{list:{data:{type:"list",id:BottomSheetLeadForm.klaviyo.listId}}}}})}catch(err){throw new Error(this.messages.errorGeneric,{cause:err})}const subscribeUrl=new URL("https://a.klaviyo.com/client/subscriptions/");subscribeUrl.searchParams.set("company_id",BottomSheetLeadForm.klaviyo.companyId);const res=await fetch(subscribeUrl,{method:"POST",headers,body});if(res.status!==202&&!res.ok){const err=await res.json();throw new Error(err.title,{cause:err})}}async sendWhatsappToWebhook(payload){const webhookUrlRaw=this.dataset.whatsappWebhook?.trim()||"";let webhookUrl=null;try{webhookUrl=new URL(webhookUrlRaw)}catch{throw this.createSubmitError(BottomSheetLeadForm.errorTypes.server,this.messages.errorGeneric)}let res=null;const controller=new AbortController,timeoutId=setTimeout(()=>controller.abort(),8e3);try{res=await fetch(webhookUrl.toString(),{method:"POST",signal:controller.signal,headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({channel:payload.channel||"whatsapp",phone:payload.phone,email:payload.email||"",consent:!0,language_iso:payload.language_iso,language_root_url:payload.language_root_url,source:"shopify_bottom_sheet"})})}catch(err){throw this.createSubmitError(BottomSheetLeadForm.errorTypes.network,this.messages.errorGeneric,err,this.messages.errorGeneric)}finally{clearTimeout(timeoutId)}let responseBody=null,rawResponse="";try{rawResponse=await res.text()}catch{rawResponse=""}if(rawResponse.trim()!=="")try{responseBody=JSON.parse(rawResponse)}catch{throw this.createSubmitError(BottomSheetLeadForm.errorTypes.server,this.messages.errorGeneric)}const responseMessage=typeof responseBody?.message=="string"?responseBody.message.trim():"";if(!(res.ok&&responseBody?.ok===!0))throw res.status===422?this.createSubmitError(BottomSheetLeadForm.errorTypes.validation,responseMessage||this.messages.errorPhone||this.messages.errorGeneric):res.status===429?this.createSubmitError(BottomSheetLeadForm.errorTypes.server,responseMessage||this.messages.errorGeneric):this.createSubmitError(BottomSheetLeadForm.errorTypes.server,this.messages.errorGeneric)}async sendConsentLog(payload){const controller=new AbortController,timeoutId=setTimeout(()=>controller.abort(),8e3);try{await fetch(BottomSheetLeadForm.consentLog.endpoint,{method:"POST",signal:controller.signal,headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({email:payload.email,consent_version:BottomSheetLeadForm.klaviyo.consentVersion,email_tracking_consent:payload.trackingConsent||!1,source:"shopify_bottom_sheet",channel:payload.channel})})}catch(err){console.error("[bottom-sheet-lead-form] consent log failed",{branch:"consent-log",error:err?.message||String(err)})}finally{clearTimeout(timeoutId)}}createSubmitError(errorType,message,cause=null,userMessage=null){const error=cause?new Error(message,{cause}):new Error(message);return error.errorType=errorType,error.userMessage=userMessage||message,error}emit(name,detail){window.dispatchEvent(new CustomEvent(name,{detail}))}}customElements.define("bottom-sheet-lead-form",BottomSheetLeadForm)} 2//# sourceMappingURL=/cdn/shop/t/99/assets/bottom-sheet-lead-form.js.map?v=104368168582606451661787060536
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.