(()=>{'use strict';
const cfg=window.SPP_CONFIG,root=document.getElementById('spp-root');if(!cfg||!root)return;
const dialog=root.querySelector('.spp-dialog'),copy=root.querySelector('[data-spp-copy]'),cta=root.querySelector('[data-spp-click]');
const mobile=matchMedia('(hover: none), (pointer: coarse)').matches;
if(mobile?!cfg.mobile:!cfg.desktop)return;
const prefix='spp:v1:'+location.hostname+':';
const now=()=>Date.now();
const safeRead=k=>{try{return localStorage.getItem(prefix+k);}catch(e){return null;}};
const safeWrite=(k,v)=>{try{localStorage.setItem(prefix+k,v);}catch(e){}};
const safeSession=k=>{try{return sessionStorage.getItem(prefix+k);}catch(e){return null;}};
const markSession=k=>{try{sessionStorage.setItem(prefix+k,'1');}catch(e){window.__sppDone=true;}};
const end=Number(safeRead('until')||0);if(end>now()||safeSession('shown')||window.__sppDone)return;
let visible=false,finished=false,lastFocus=null,activeMs=0,lastTick=now(),idleMs=0;
const update=()=>{const t=now(),delta=Math.min(Math.max(t-lastTick,0),5000);lastTick=t;if(!document.hidden){activeMs+=delta;idleMs+=delta}return activeMs/1000};
const emit=(event)=>{if(!cfg.stats)return;const data=new URLSearchParams({action:'spp_event',nonce:cfg.nonce,event});try{fetch(cfg.url,{method:'POST',credentials:'same-origin',body:data,keepalive:true,headers:{'Content-Type':'application/x-www-form-urlencoded'}}).catch(()=>{});}catch(e){}};
const eligible=()=>!finished&&!visible&&!document.hidden&&update()>=cfg.minSeconds;
const show=()=>{if(!eligible())return;visible=true;finished=true;markSession('shown');lastFocus=document.activeElement;root.hidden=false;root.setAttribute('aria-hidden','false');document.body.classList.add('spp-open');dialog.focus();emit('shown');};
const delay=(days)=>{if(days>0)safeWrite('until',String(Math.max(Number(safeRead('until')||0),now()+days*86400000)));};
const close=(event)=>{if(!visible)return;visible=false;root.hidden=true;root.setAttribute('aria-hidden','true');document.body.classList.remove('spp-open');delay(cfg.repeatDays);if(lastFocus&&typeof lastFocus.focus==='function')lastFocus.focus();if(event)emit('closed');};
root.querySelectorAll('[data-spp-dismiss]').forEach(el=>el.addEventListener('click',()=>close(true)));
document.addEventListener('keydown',e=>{if(!visible)return;if(e.key==='Escape'){e.preventDefault();close(true);}if(e.key==='Tab'){const nodes=Array.from(dialog.querySelectorAll('button:not([disabled]),a[href]'));if(!nodes.length)return;const first=nodes[0],last=nodes[nodes.length-1];if(e.shiftKey&&document.activeElement===first){e.preventDefault();last.focus()}else if(!e.shiftKey&&document.activeElement===last){e.preventDefault();first.focus()}}});
const claim=()=>{delay(cfg.claimDays);};
if(copy)copy.addEventListener('click',async()=>{let success=false;try{await navigator.clipboard.writeText(cfg.coupon);success=true}catch(e){try{const field=document.createElement('textarea');field.value=cfg.coupon;field.style.position='fixed';field.style.left='-9999px';document.body.appendChild(field);field.select();success=document.execCommand ('copy');field.remove()}catch(e){}}if(success){copy.textContent='Скопировано ✓';claim();emit('copied');}else{copy.textContent='Не удалось скопировать';}});
if(cta)cta.addEventListener('click',()=>{claim();emit('clicked')});
let exitPending=false,exitCheck=null;
const clearExit=()=>{exitPending=false;if(exitCheck!==null){clearTimeout(exitCheck);exitCheck=null;}};
const checkExit=()=>{
exitCheck=null;
if(!exitPending||finished||visible||document.hidden){clearExit();return;}
const active=update(),required=Math.max(0,Number(cfg.exitSeconds)||0,Number(cfg.minSeconds)||0);
if(active>=required){clearExit();show();return;}
exitCheck=setTimeout(checkExit,Math.min(250,Math.max(50,(required-active)*1000)));
};
if(cfg.exit&&!mobile){
const onTopExit=e=>{
if(e.relatedTarget!==null||e.clientY>5||finished||visible)return;
exitPending=true;
if(exitCheck===null)checkExit();
};
document.addEventListener('mouseout',onTopExit);
document.documentElement.addEventListener('mouseleave',onTopExit);
document.addEventListener('mouseover',()=>{if(exitPending)clearExit();});
document.addEventListener('visibilitychange',()=>{if(document.hidden)clearExit();});
}
const resetIdle=()=>{update();idleMs=0};['pointerdown','pointermove','keydown','scroll','touchstart'].forEach(type=>document.addEventListener(type,resetIdle,{passive:true}));
document.addEventListener('visibilitychange',()=>{lastTick=now()});
setInterval(()=>{if(finished||visible||document.hidden)return;const active=update();if(cfg.timer&&active>=cfg.timerSeconds){show();return}if(cfg.idle&&idleMs>=cfg.idleSeconds*1000)show()},1000);
if(cfg.scroll)window.addEventListener('scroll',()=>{const total=document.documentElement.scrollHeight-innerHeight;if(total>0&&scrollY/total*100>=cfg.scrollPercent)show()},{passive:true});
})();