const contentWrapper=document.getElementById("dobare-lead-magnet-wrraper"),dobareChannel=new BroadcastChannel("DOBARE-IFRAME");let timerElement=null,progressTimer=null,timeLeft=60,totalTime=60;const LOCAL_STORAGE_LEAD_MAGNET_KEY="is_lead_magnet_show";let resizeObserver=null,lastHeight=0,resizeTimeout=null,isActive=null,position="left",creditAmount=0,redirectTo=null,businessName="",currencyWord="تومان",isCustom=!1;const toPersianNumber=e=>e.toString().replace(/\d/g,e=>"۰۱۲۳۴۵۶۷۸۹"[e]),priceFormatNumberWithCommas=e=>{const t=e.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",");return toPersianNumber(t)},removeIframeFromDom=()=>{setCookie("is_lead_magnet_show","true"),dobareChannel.postMessage({key:"REMOVE_DOBARE_IFRAME"})},setCookie=(e,t)=>{const r=new Date;r.setTime(r.getTime()+6048e5);const n="expires="+r.toUTCString();document.cookie=`${e}=${encodeURIComponent(t)}; ${n}; path=/`},startCountdown=()=>{timerElement&&progressTimer?countdownInterval=setInterval(()=>{if(timeLeft<0)return clearInterval(countdownInterval),void removeIframeFromDom();const e=Math.floor(timeLeft/60),t=timeLeft%60,r=String(e).padStart(2,"0"),n=String(t).padStart(2,"0");timerElement.textContent=`${toPersianNumber(r)}:${toPersianNumber(n)}`,50===n&&setCookie("is_lead_magnet_show","true");const o=(totalTime-timeLeft)/totalTime*100;isCustom||(progressTimer.style.background=`\n\t\t\tlinear-gradient(white, white) content-box no-repeat,\n\t\t\tconic-gradient(white ${o}%, rgba(135, 0, 155, 1) 0%) border-box\n\t\t  `),timeLeft--},1e3):console.error("Timer elements not found")},sendHeightToParent=()=>{if(!contentWrapper)return;const e=calculateHeight();Math.abs(e-lastHeight)>5&&(lastHeight=e,dobareChannel.postMessage({key:"SET_DOBARE_IFRAME_HEIGHT",height:e}))},calculateHeight=()=>{if(!contentWrapper)return 180;const e=contentWrapper.scrollHeight||contentWrapper.clientHeight,t=progressTimer?progressTimer.scrollHeight:0,r=contentWrapper.querySelectorAll("*");let n=0;return r.forEach(e=>{const t=e.scrollHeight;t>n&&(n=t)}),Math.max(e,n,t,180)},setupResizeObserver=()=>{contentWrapper&&(resizeObserver&&resizeObserver.disconnect(),resizeObserver=new ResizeObserver(()=>{resizeTimeout&&clearTimeout(resizeTimeout),resizeTimeout=setTimeout(sendHeightToParent,100)}),resizeObserver.observe(contentWrapper),setTimeout(sendHeightToParent,300),setTimeout(sendHeightToParent,1e3))},goToLoginPage=()=>{const e=setInterval(()=>{dobareChannel.postMessage({key:"REDIRECT_TO_LOGIN_PAGE"}),dobareChannel.onmessage=function(t){"CONFRIM_REDIRECT_TO_LOGIN_PAGE"===t.data.key&&clearInterval(e)}},200)};window.addEventListener("load",()=>{sendHeightToParent(),setupResizeObserver()}),window.addEventListener("resize",sendHeightToParent),document.addEventListener("contentLoaded",()=>{progressTimer=document.getElementById("dobare-progress-timer"),timerElement=document.getElementById("dobare-timer"),progressTimer&&timerElement&&startCountdown(),setupResizeObserver()}),dobareChannel.onmessage=function(e){if("SEND_LEAD_MAGNET_DATA_TO_DOBARE_IFRAME"===e.data.key){const t=e.data.data;isActive=t.is_active,position=t.position,creditAmount=t.credit_amount,redirectTo=t.redirect_to,businessName=t.business_title,currencyWord=t.currency||"تومان",isCustom=t.is_custom||!1,updateUI()}};const updateUI=()=>{const e=document.getElementById("dobare-gift-credit-amount"),t=document.getElementById("business-name"),r=document.getElementById("dobare-currency-word"),n=window.parent.document.getElementById("dob-lead-magnet");n&&("L"===position?(n.style.left="20px",n.style.right="auto"):"R"===position&&(n.style.right="20px",n.style.left="auto")),e&&(e.textContent=priceFormatNumberWithCommas(creditAmount)||"۰"),r&&(r.textContent=currencyWord),t&&(t.textContent=businessName||"بیزینس"),setTimeout(sendHeightToParent,100)};document.addEventListener("contentLoaded",()=>{updateUI()});