(()=>{"use strict";const e=window.eddCartRecovery||{},t={lastSentData:{email:"",first_name:"",last_name:""},isSending:!1,pendingCapture:!1},a=async(n=!1)=>{if(t.isSending)return void(t.pendingCapture=!0);const r=document.getElementById("edd_purchase_form");if(!r)return;const s=r.querySelector('[name="edd_email"]'),i=r.querySelector('[name="edd_first"]'),o=r.querySelector('[name="edd_last"]'),d={email:s?.value?.trim()||"",first_name:i?.value?.trim()||"",last_name:o?.value?.trim()||""};if(d.email&&(n||d.email!==t.lastSentData.email||d.first_name!==t.lastSentData.first_name||d.last_name!==t.lastSentData.last_name)){t.isSending=!0;try{const a=await fetch(`${e.restEndpoint}/customer-info`,{method:"POST",headers:{"Content-Type":"application/json","X-EDD-Cart-Token":e.token,"X-EDD-Cart-Timestamp":e.timestamp,"X-WP-Nonce":e.nonce},credentials:"same-origin",body:JSON.stringify(d)});if(a.ok){const n=await a.json().catch((()=>({})));n.token&&(e.token=n.token),n.timestamp&&(e.timestamp=n.timestamp),t.lastSentData={...d},document.dispatchEvent(new CustomEvent("edd_acr_customer_captured"))}else{const e=await a.json().catch((()=>({})));console.debug("[EDD ACR] Failed to capture customer info:",e.error)}}catch(e){console.debug("[EDD ACR] Error capturing customer info:",e)}finally{t.isSending=!1,t.pendingCapture&&(t.pendingCapture=!1,setTimeout((()=>a()),100))}}},n=()=>{const e=document.getElementById("edd_purchase_form");if(!e)return;const t=e.querySelector('[name="edd_email"]'),n=t?.value?.trim();n&&a()},r=window.eddCartRecovery||{},s=12e4,i="edd_acr_last_heartbeat",o={intervalId:null,lastBeat:0,isVisible:!0,hasCart:!1,consecutiveFailures:0},d=async()=>{try{(()=>{const e=document.getElementById("edd_purchase_form");if(!e)return;const n=e.querySelector('[name="edd_email"]'),r=e.querySelector('[name="edd_first"]'),s=e.querySelector('[name="edd_last"]'),i=n?.value?.trim()||"",o=r?.value?.trim()||"",d=s?.value?.trim()||"";i&&(i!==t.lastSentData.email||o!==t.lastSentData.first_name||d!==t.lastSentData.last_name)&&a()})();const e=Date.now();o.lastBeat=e;try{sessionStorage.setItem(i,e.toString())}catch(e){}const n=await fetch(`${r.restEndpoint}/heartbeat`,{method:"POST",headers:{"Content-Type":"application/json","X-EDD-Cart-Token":r.token,"X-EDD-Cart-Timestamp":r.timestamp,"X-WP-Nonce":r.nonce},credentials:"same-origin"});if(n.ok){const e=await n.json().catch((()=>({})));e.token&&(r.token=e.token),e.timestamp&&(r.timestamp=e.timestamp),o.consecutiveFailures=0,o.hasCart=!0}else{const e=await n.json().catch((()=>({})));400===n.status||404===n.status?(o.consecutiveFailures++,o.consecutiveFailures>=3&&(l(),o.hasCart=!1)):console.debug("[EDD ACR Heartbeat] Request failed:",n.status,e.error)}}catch(e){console.debug("[EDD ACR Heartbeat] Error:",e)}},c=()=>{o.intervalId&&clearInterval(o.intervalId),d(),o.intervalId=setInterval((()=>{o.isVisible&&d()}),s)},l=()=>{o.intervalId&&(clearInterval(o.intervalId),o.intervalId=null)},m=()=>{document.hidden?o.isVisible=!1:(o.isVisible=!0,Date.now()-o.lastBeat>=s&&d())},u=()=>{o.consecutiveFailures=0,o.intervalId||c()},_=()=>{if(!o.intervalId){try{const e=sessionStorage.getItem(i);e&&(o.lastBeat=Number.parseInt(e,10))}catch(e){console.debug("[EDD ACR Heartbeat] Error restoring last heartbeat from storage:",e)}o.isVisible=!document.hidden,document.addEventListener("visibilitychange",m),document.addEventListener("edd_cart_item_added",u),document.addEventListener("edd_email_captured",u),c()}},v=()=>{var e;(()=>{const e=document.getElementById("edd_purchase_form");e&&["edd_email","edd_first","edd_last"].forEach((t=>{const a=e.querySelector(`[name="${t}"]`);a&&a.addEventListener("change",n)}))})(),null!==(e=globalThis.eddCartRecovery?.hasTrackedCart)&&void 0!==e&&e?_():document.addEventListener("edd_acr_customer_captured",(()=>setTimeout((()=>_()),s)),{once:!0})};"loading"===document.readyState?document.addEventListener("DOMContentLoaded",v):v()})();