(()=>{"use strict";function e(e,t){for(;e;){if(t&&e.tagName?.toLowerCase()===t.toLowerCase())return!0;e=e.parentElement}return!1}class t{observer=null;abandonedEvents=["beforeunload"];constructor(){this.init(),this.initTracking(),this.observeDynamicForms()}getFormIdAndPosId(e){if(!e)return{formId:"",postId:""};const t=document.body.className.match(/page-id-(\d+)/)?.[1]||this.getPostId(e);return{formId:this.getFormId(e)?this.idPrefix+this.getFormId(e):"",postId:t}}observeDynamicForms(){this.observer=new MutationObserver((e=>{e.forEach((e=>{"childList"===e.type&&e.addedNodes.length>0&&e.addedNodes.forEach((e=>{e instanceof Element&&this.handleNewForm(e)}))}))})),this.observer.observe(document.body,{childList:!0,subtree:!0})}handleNewForm(e){const t=this.formSelectors.join(","),r=new Set;e.matches(t)&&r.add(e),e.querySelectorAll(t).forEach((e=>r.add(e))),r.forEach((e=>{this.trackViewedForms(e),this.trackAbandonedForms(e)}))}initTracking(){const e=this.formSelectors.join(","),t=new Set;document.querySelectorAll(e).forEach((e=>t.add(e))),t.forEach((e=>{this.trackViewedForms(e),this.trackAbandonedForms(e)}))}trackViewedForms(e){const t=new IntersectionObserver((r=>{r.forEach((async r=>{if(r.isIntersecting&&r.intersectionRatio>=this.viewedThreshold){const o=this.prepareFormData(e,this.viewedAction);if(o.has("form_id")){const e=await fetch(this.ajaxUrl,{method:"POST",body:o});!e.ok&&this.debugOn&&console.error("Error tracking form view:",e.statusText)}t.unobserve(r.target)}}))}),{threshold:[this.viewedThreshold]});t.observe(e)}trackAbandonedForms(e){let t=!1,r=!1;e.querySelectorAll("input, textarea").forEach((e=>{e.addEventListener("focus",(()=>t=!0))})),e.addEventListener("submit",(()=>r=!0)),this.abandonedEvents.forEach((o=>{window.addEventListener(o,(async()=>{if(t&&!r){const t=this.prepareFormData(e,this.abandonedAction);if(t.has("form_id")){const e=await fetch(this.ajaxUrl,{method:"POST",body:t});!e.ok&&this.debugOn&&console.error("Error tracking abandoned form:",e.statusText)}}}))}))}prepareFormData(t,r){const o=new FormData,{formId:n,postId:s}=this.getFormIdAndPosId(t);return n&&(o.append("action",r),o.append("nonce",this.nonce),o.append("form_id",n),o.append("page_id",s),o.append("location",function(t){return e(t,"header")?"header":e(t,"footer")?"footer":function(e){const t=["header","footer","popup"];for(;e;){const r=e.getAttribute("data-elementor-type");if(t.includes(r))return r;e=e.parentElement}return"page"}(t)}(t))),o}}class r extends t{init(){this.formSelectors=eSendElementorFormsSettings.formSelectors,this.ajaxUrl=eSendElementorFormsSettings.ajaxUrl,this.nonce=eSendElementorFormsSettings.nonce,this.viewedThreshold=eSendElementorFormsSettings.viewedThreshold,this.viewedAction=eSendElementorFormsSettings.viewedAction,this.abandonedAction=eSendElementorFormsSettings.abandonedAction,this.debugOn=eSendElementorFormsSettings.debugOn,this.idPrefix=eSendElementorFormsSettings.idPrefix,this.abandonedEvents=["elementor/popup/hide",...this.abandonedEvents]}getFormId(e){return e.querySelector('input[name="form_id"]')?.getAttribute("value")||(e.classList.contains("elementor-login")?"login":"")}getPostId(e){const t=e.querySelector('input[name="queried_id"]')?.getAttribute("value");return t||e.querySelector('input[name="post_id"]')?.getAttribute("value")||""}prepareFormData(e,t){const r=super.prepareFormData(e,t);return r.append("template_id",e.querySelector('input[name="post_id"]')?.getAttribute("value")||""),r}}window.addEventListener("elementor/frontend/init",(()=>{new r}))})();