const newAPiUrl="https://selectorshub.info/nodeapp/api/",EXT_SLUG="PLT";let platform="chrome",isFirefox="undefined"!==typeof InstallTrigger,isEdge=-1<navigator.userAgent.indexOf("Edg/")?!0:!1,isChrome=!!chrome&&(!!chrome.webstore||!!chrome.runtime),isOpera=0<=navigator.userAgent.indexOf(" OPR/");isChrome&&(platform="chrome");isFirefox&&(platform="firefox");isEdge&&(platform="edge");isOpera&&(platform="opera");
let uiBlockAdsSettings=null,upgradeContainer=document.querySelector("#upgrade-popup"),upgradeConfirmBtn=document.querySelector("#upgrade-confirm"),upgradeCancelBtn=document.querySelector("#upgrade-cancel"),adContainer=document.querySelector(".ad-container");upgradeConfirmBtn.addEventListener("click",()=>{upgradeContainer.classList.add("hidden")});upgradeCancelBtn.addEventListener("click",()=>{upgradeContainer.classList.add("hidden")});
$(document).on("click",".export-all",function(){uiBlockAdsSettings&&1===uiBlockAdsSettings.active?chrome.storage.local.get(["save_count"],c=>{if(void 0===c.save_count||null===c.save_count)return chrome.storage.local.set({save_count:0});c.save_count>=uiBlockAdsSettings.nb_actions-1?chrome.storage.local.get(["ttlt"],a=>{"undefined"==typeof a.ttlt||"undefined"==a.ttlt||null==a.ttlt||""==a.ttlt?(upgradeContainer.classList.remove("hidden"),upgradeContainer.querySelector(".upgrade__popup__message").innerText=
uiBlockAdsSettings.message,upgradeContainer.querySelector("#upgrade-confirm").href=uiBlockAdsSettings.link,upgradeContainer.querySelector("#upgrade-confirm").innerText=uiBlockAdsSettings.action_label):upgradeContainer.classList.add("hidden");chrome.storage.local.set({save_count:0})}):chrome.storage.local.set({save_count:c.save_count+1})}):chrome.storage.local.set({save_count:0})});
function fetchProductUpdates(){fetch(`${newAPiUrl}all-ads`,{cache:"no-store"}).then(c=>c.json()).then(async c=>{displayAllAds(c)})}
const displayAllAds=async c=>{if(c.status){if(0<c.footer_ads.length){const a=c.footer_ads.filter(e=>e.extensions.includes(EXT_SLUG)&&e.browsers.map(d=>d.toLowerCase()).includes(platform.toLowerCase()));if(a.length)adContainer.classList.remove("hidden"),document.querySelector(".ad-container").style.display="flex";else return adContainer.classList.add("hidden"),!0;displayAds(a);currentIndex=1;tStartTime=Date.now();const b=document.querySelectorAll(".promoted-item");applyInterval(1E3*a[0].display_time);
b.forEach(e=>{e.addEventListener("mouseenter",()=>{tIsPaused||(clearInterval(tInterval),tTimeRest=Date.now()-tStartTime,tIsPaused=!0)});e.addEventListener("mouseleave",()=>{if(tIsPaused){var d=e.getAttribute("data-display-time");if(d=parseInt(d))tStartTime=Date.now()-tTimeRest,applyInterval(1E3*d-tTimeRest),tIsPaused=!1}})})}0<c.popup_ads.length&&(console.log(88),chrome.storage.local.get(["popup_ads_sessions"],({popup_ads_sessions:a})=>{console.log(a);a||={count:0};const b=c.popup_ads.find(g=>g.ext_slug==
EXT_SLUG);if(b){var {active:e,time_control:d,sessions:f,link:h}=b;console.log(f);1===e&&(a.count++,a.count>=f?setTimeout(()=>{showKeepAds(h);chrome.storage.local.set({popup_ads_sessions:{count:0}})},1E3*d):(console.log(88),chrome.storage.local.set({popup_ads_sessions:a})))}}));0<c.uiblock__ads.length&&(uiBlockAdsSettings=c.uiblock__ads.filter(a=>a.ext_slug==EXT_SLUG)[0])}};
let adsViewerPrevBtn=document.querySelector(".navigate-prev-btn"),adsViewerNextBtn=document.querySelector(".navigate-next-btn"),currentIndex=1,tIntervals=[],tInterval=null,tStartTime=0,tTimeRest=0,tIsPaused=!1;
const applyInterval=c=>{const a=document.querySelectorAll(".promoted-item");tIntervals.forEach(b=>clearInterval(b));tIntervals=[];tInterval=setInterval(()=>{if(a[currentIndex]){tStartTime=Date.now();a.forEach((e,d)=>{d===currentIndex?e.classList.remove("hideElement"):e.classList.add("hideElement")});let b=setTimeout(()=>{clearTimeout(b);clearInterval(tInterval);tStartTime=0;const e=a[currentIndex].id.replace("ad__",""),d=_ads.filter(f=>f.id==e)[0];currentIndex===a.length-1?currentIndex=0:currentIndex++;
tStartTime=Date.now();applyInterval(1E3*d.display_time)},300)}},c);tIntervals.push(tInterval)};adsViewerPrevBtn.addEventListener("click",()=>{let c=document.querySelectorAll(".promoted-item");currentIndex-=2;0>currentIndex&&(currentIndex=c.length+currentIndex);applyInterval(400,"prev")});adsViewerNextBtn.addEventListener("click",()=>{applyInterval(400,"next")});
const displayAds=c=>{clearInterval(tInterval);tStartTime=0;_ads=c;let a=document.querySelector(".footer-ads-container");a.innerHTML="";c.forEach((b,e)=>{const d=document.createElement("div");d.id="ad__"+b.id;d.classList.add("ext-ad-item");d.classList.add("promoted-item");0!==e&&d.classList.add("hideElement");d.setAttribute("data-display-time",b.display_time);e=`
				  <!-- Image Placeholder -->
            <div class="image-placeholder">
                <img src="${b.image_url}" alt="Ad Image">
            </div>
            <!-- Ad Content -->
            <div class="ad-content">
       
                ${b.type?`<h3>[${b.type.name}]</h3>`:""}
                <!-- Register Button -->
                
                <button class="register-button" style="color: ${b.action.text_color}; background-color: ${b.action.background_color}">
               
                ${20<b.action.name.trim().length?`${b.action.name.trim().substring(0,20)}..`:`${b.action.name.trim()}`}
                <img src="${b.action.icon_url}">
            </button>
                <div class="scroll-container"><p class="scroll-text">${b.ad}</p></div>
            </div>
            
            `;d.innerHTML=e;d.addEventListener("click",async()=>{chrome.tabs.create({url:b.link});trackClicks({ext_slug:EXT_SLUG,add_id:b.id,type:"footer"})});a.appendChild(d)})};
function trackClicks(c){const a=(new URLSearchParams({ext_slug:c.ext_slug,add_id:c.add_id,type:c.type})).toString();chrome.storage.local.get(["trackedQueries"],b=>{b=b.trackedQueries||[];b.includes(a)||(b.unshift(a),5<b.length&&(b=b.slice(0,5)),chrome.storage.local.set({trackedQueries:b},()=>{fetch(`${newAPiUrl}link/track?${a}`)}))})}
function showKeepAds(c){console.log(555);var a=document.querySelector(".footer-ads-container");if(a){a.insertAdjacentHTML("beforeend",'\n    <div class="keep_actions_container">\n      <span class="keep-ads-btn">Keep Ads</span>\n      <span class="keep-ads-link">Go Ads Free</span>\n    </div>');const b=a.querySelector(".keep_actions_container"),e=a.querySelector(".keep-ads-btn");a=a.querySelector(".keep-ads-link");e.addEventListener("click",()=>{b.style.display="none";sendHeight()});a.addEventListener("click",
()=>{chrome.tabs.create({url:c})})}else console.warn("Footer container not found")};
