(()=>{class e{constructor(e){this.leadID=null,this.pendingStatus=null,this.currentStep=1,this.steps={},this.leadData=[],this.cookieStorage="seoai_lead_form",this.form=e,this.stepsWrapper=this.form.getElementsByClassName("seoaic-ms-steps")[0],this.closeBtn=this.form.getElementsByClassName("seoaic-ms-close")[0],this.openBtns=this.form.getElementsByClassName("seoaic-ms-open-click"),this.closedMsgCloseBtn=this.form.getElementsByClassName("seoaic-ms-closed-msg-close-icon"),this.stepsDOMElements=this.form.getElementsByClassName("seoaic-ms-step"),this.redirectURL=this.form.dataset.redirecturl,this.formId=this.form.dataset.id||"0",this.progressBar=this.form.getElementsByClassName("seoaic-multistep-progress")[0],this.progressBarPercentage=this.form.getElementsByClassName("seoaic-multistep-progress-percent")[0],this.initRecordRemote(),this.run()}run(){if(!this.stepsDOMElements.length)return;const e=this.getStepID(this.stepsDOMElements[0]);for(const e of this.stepsDOMElements)this.steps[this.getStepID(e)]=e;this.initCloseOpen(),this.runStep(this.steps[e])}getCookie(){const e=document.cookie.match(new RegExp("(?:^|;)"+this.cookieStorage+"=([^;]*)"));if(!e)return{};try{return JSON.parse(decodeURIComponent(e[1]))}catch{return{}}}setCookie(e){if(!e)return;const t="https:"===location.protocol?"; secure":"";document.cookie=this.cookieStorage+"="+encodeURIComponent(JSON.stringify(e))+"; path=/; max-age=2592000; SameSite=Lax"+t}addRecord(e,t,s){const i=this.getCookie();i[e]={...i[e]||{},[t]:s},this.setCookie(i)}deleteRecord(e,t){const s=this.getCookie();s[e]&&(delete s[e][t],0===Object.keys(s[e]).length&&delete s[e],this.setCookie(s))}initCloseOpen(){this.closeBtn.addEventListener("click",e=>{this.form.classList.add("closed"),this.addRecord(this.formId,"form","closed")});for(let e=0;e<this.openBtns.length;e++)this.openBtns[e].addEventListener("click",e=>{this.form.classList.remove("closed"),this.deleteRecord(this.formId,"form"),this.deleteRecord(this.formId,"message")});if(this.closedMsgCloseBtn)for(let e=0;e<this.closedMsgCloseBtn.length;e++)this.closedMsgCloseBtn[e].addEventListener("click",e=>{e.stopPropagation(),this.addRecord(this.formId,"message","closed");const t=this.form.getElementsByClassName("seoaic-ms-closed-msg-container");t.length&&t[0].remove()})}getStepsIDs(){return Object.keys(this.steps)}getStepID(e){return e.getAttribute("id")}showStep(e){const t=document.getElementById(e),s=(this.currentStep-1)/this.stepsDOMElements.length*100;this.updateProgressBarTo(s),t.classList.remove("hide"),this.currentStep++,setTimeout(()=>{this.stepsWrapper.scrollTo({top:this.stepsWrapper.scrollHeight,behavior:"smooth"})},2e3)}updateProgressBarTo(e){this.progressBar.style.width=e+"%";const t=Math.ceil(e);this.progressBarPercentage.style.setProperty("--num",t);const s=100==e?95:e;this.progressBarPercentage.style.left=s+"%"}runStep(e){const t=this,s=e.dataset.type,i=e.dataset.autostep,o=e.getElementsByClassName("seoaic-ms-step-question")[0],n=e.getElementsByClassName("seoaic-ms-step-content")[0],a=e.getElementsByClassName("seoaic-ms-step-answers-wrapper")[0],r={type:s,question:o.dataset.origquestion,answer:""},d=n.getElementsByTagName("button");if(this.showStep(this.getStepID(e)),""!=i&&-1!=this.getStepsIDs().indexOf(i))setTimeout(()=>{this.runStep(this.steps[i])},2e3);else if(d.length)for(const i of d)i.addEventListener("click",i=>{const o=i.currentTarget,d=o.dataset.stepId;let l="";try{if("contact_fields"==s){const t=e.querySelectorAll("input, select");for(const e of t)if(!e.validity.valid)throw e.reportValidity(),new Error("Required field")}this.hideStepContent(n).then(()=>{let i=[],n="";if("single"==s)n=o.dataset.origtext,i.push(n);else if("multi"==s){const t=e.querySelectorAll('input[type="checkbox"]:checked');for(const e of t)i.push(e.value);i=i.length?i:["-/-"],n=i.join("; ")}else if("textfield"==s)n=e.getElementsByTagName("textarea")[0].value||"-/-",i.push(n);else if("contact_fields"==s){const t=e.querySelectorAll("input, select");for(const e of t){const t=e.value||"-/-";i.push({field:e.dataset.label,value:t})}n=i.length?i:"-/-",l="column"}r.answer=n;const c=-1!==d.indexOf("#finish#");return this.addStepAnswerData(r,c),t.showStepAnswer(a,i,l)}).then(()=>{-1!==d.indexOf("#finish#")?(this.updateRecordRemote("captured"),this.updateProgressBarTo(100),this.openRedirectURL()):t.steps[d]?t.runStep(t.steps[d]):console.error("[Multistep] Unknown step: "+d)}).catch(e=>{console.error("[Multistep] Step transition failed",e)})}catch(e){console.error("[Multistep] Step failed",e)}});else this.updateRecordRemote("captured"),this.updateProgressBarTo(100),this.openRedirectURL()}openRedirectURL(){this.redirectURL&&setTimeout(()=>{window.open(this.redirectURL,"_blank")||window.location.assign(this.redirectURL)},3e3)}hideStepContent(e){return new Promise((t,s)=>{if(!e)return s(new Error("Missing step content"));e.classList.add("hide-animate"),setTimeout(()=>{t()},500)})}showStepAnswer(e,t,s="row"){return new Promise((i,o)=>{if(!e)return o(new Error("Missing step answers wrapper"));for(const i of t){const t=document.createElement("div");if(t.classList.add("seoaic-ms-answer-item"),"string"==typeof i?t.innerText=i:"object"==typeof i&&(t.innerText=i.field+": "+i.value),"column"==s){const s=document.createElement("div");s.classList.add("seoaic-ms-answer-wrapper"),s.appendChild(t),e.appendChild(s)}else e.appendChild(t)}"column"==s&&e.classList.add("seoaic-flex-column"),e.classList.add("show-animate"),setTimeout(()=>{i()},1300)})}addStepAnswerData(e,t=!1){this.leadData.push(e),t||this.updateRecordRemote()}async initRecordRemote(){const e=await fetch(wpApiSettingsFront.root+"/leads-addon/leads",{method:"POST",headers:{"X-WP-Nonce":wpApiSettingsFront.nonce,"Content-Type":"application/json"},body:JSON.stringify({title:document.title,url:document.location.href})}),t=await e.json();if(t.data?.data?.id&&(this.leadID=t.data.data.id,this.pendingStatus)){const e=this.pendingStatus;this.pendingStatus=null,this.updateRecordRemote(e)}}updateRecordRemote(e="initiated"){this.leadID?fetch(wpApiSettingsFront.root+"/leads-addon/leads/"+this.leadID,{method:"PUT",headers:{"X-WP-Nonce":wpApiSettingsFront.nonce,"Content-Type":"application/json"},body:JSON.stringify({status:e,leadData:this.leadData})}).then(e=>e.json()).then(t=>{"captured"==e&&t.data&&"captured"==t.data.status&&this.dispatchLeadCaptured()}):this.pendingStatus=e}dispatchLeadCaptured(){document.dispatchEvent(new CustomEvent("wpseoai:lead_form_submit",{bubbles:!0}))}}window.addEventListener("load",()=>{const t=document.getElementsByClassName("seoaic-multistep-form");if(t.length)for(let s=0;s<t.length;s++)new e(t[s])})})();