(()=>{"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=eSendCf7FormsSettings.formSelectors,this.ajaxUrl=eSendCf7FormsSettings.ajaxUrl,this.nonce=eSendCf7FormsSettings.nonce,this.viewedThreshold=eSendCf7FormsSettings.viewedThreshold,this.viewedAction=eSendCf7FormsSettings.viewedAction,this.abandonedAction=eSendCf7FormsSettings.abandonedAction,this.debugOn=eSendCf7FormsSettings.debugOn,this.idPrefix=eSendCf7FormsSettings.idPrefix}getFormId(e){return e.querySelector('input[name="_wpcf7"]')?.getAttribute("value")||""}getPostId(e){return e.querySelector('input[name="_wpcf7_container_post"]')?.getAttribute("value")||""}}window.addEventListener("load",(()=>{new r}))})();