const dobareChannel=new BroadcastChannel("DOBARE-IFRAME"),leadMagnetWrapper=document.getElementById("dobare-lead-magnet-wrraper"),LOCAL_STORAGE_LEAD_MAGNET_KEY="is_lead_magnet_show";let iframeElement,isIframeActive=null,redirectLink="",lastHeight=0,resizeTimeout=null,resizeRAF=null;const getLeadMagnetData=async()=>{const e=`${window.wpApiSettings.root}dobare/v1/lead-magnet?_=${Date.now()}`;try{const t=await fetch(e,{headers:{"X-WP-Nonce":wpApiSettings.nonce}});if(!t.ok)throw new Error(`Response status: ${t.status}`);return await t.json()}catch(e){console.error(e.message)}},sendWidthToIframe=()=>{const e=window.innerWidth;iframeElement&&dobareChannel.postMessage({key:"SEND_WIDTH_TO_DOBARE_IFRAME",width:e})},sendLeadMagnetDataToChild=e=>{dobareChannel.postMessage({key:"SEND_LEAD_MAGNET_DATA_TO_DOBARE_IFRAME",data:Object.assign({},e.data,{currency:window.dobareIframeData&&window.dobareIframeData.currency||"تومان",is_custom:window.dobareIframeData&&window.dobareIframeData.isCustom||!1})})};function getCookie(e){const t=e+"=",a=document.cookie.split(";");for(let e=0;e<a.length;e++){let n=a[e];for(;" "===n.charAt(0);)n=n.substring(1,n.length);if(0===n.indexOf(t))return decodeURIComponent(n.substring(t.length,n.length))}return null}const createIframeElement=e=>{let t=document.createElement("iframe");t.src=dobareIframeData.iframeSrc,t.style.border="none",t.style.width="100%",t.style.maxWidth="min(385px, 100%)",t.style.height="180px",t.style.position="fixed",t.style.bottom="20px",t.style.zIndex="9999",t.style.overflow="hidden",t.style.transition="height 0.3s ease-in-out",t.loading="lazy",t.id="dob-lead-magnet","L"===(e.data.position||"L")?(t.style.left="20px",t.style.right="auto"):(t.style.right="20px",t.style.left="auto");const a=document.createElement("style");a.textContent="\n\t\t@media (max-width: 480px) {\n\t\t\t#dob-lead-magnet {\n\t\t\t\tbottom: 20px !important;\n\t\t\t\tleft: 20px !important;\n\t\t\t\tright: 20px !important;\n\t\t\t\twidth: calc(100% - 40px) !important;\n\t\t\t\tmax-width: calc(100% - 40px) !important;\n\t\t\t\tmargin: 0 !important;\n\t\t\t}\n\t\t}\n\t",document.head.appendChild(a),isIframeActive=e.data.is_active,redirectLink=e.data.redirect_to,t.addEventListener("load",()=>{sendLeadMagnetDataToChild(e),sendWidthToIframe()}),"true"!==getCookie("is_lead_magnet_show")&&isIframeActive&&document.body.appendChild(t)};window.addEventListener("resize",()=>{resizeRAF||(resizeRAF=window.requestAnimationFrame(()=>{sendWidthToIframe(),resizeRAF=null}))});const __dobareLeadMagnetInit=async function(){try{if("true"===getCookie("is_lead_magnet_show"))return}catch(e){}const e=await getLeadMagnetData();createIframeElement(e),iframeElement=document.getElementById("dob-lead-magnet")},__dobareLeadMagnetScheduleInit=function(){"requestIdleCallback"in window?window.requestIdleCallback(()=>{__dobareLeadMagnetInit()},{timeout:3e3}):setTimeout(__dobareLeadMagnetInit,0)};"complete"===document.readyState?__dobareLeadMagnetScheduleInit():window.addEventListener("load",__dobareLeadMagnetScheduleInit),dobareChannel.onmessage=function(e){switch(e.data.key){case"SET_DOBARE_IFRAME_HEIGHT":if(iframeElement){const t=e.data.height;resizeTimeout&&clearTimeout(resizeTimeout),resizeTimeout=setTimeout(()=>{const e=Math.max(t,176);Math.abs(e-lastHeight)>5&&(iframeElement.style.height=`${e}px`,lastHeight=e)},50)}break;case"REMOVE_DOBARE_IFRAME":iframeElement&&iframeElement.parentNode&&(iframeElement.parentNode.removeChild(iframeElement),iframeElement=null);break;case"REDIRECT_TO_LOGIN_PAGE":iframeElement&&(window.location.href=redirectLink),dobareChannel.postMessage({key:"CONFRIM_REDIRECT_TO_LOGIN_PAGE"})}};