import{removeBloomQuickPanels as E}from"./panel.js-PlKG13TM.js";import{createTradingPanel as k}from"./panel-new.ts-DTBxtLQQ.js";import{getQuickBuyAmountForCurrentChain as y,showSwapToast as h}from"./utils.js-CC0KFyjC.js";import"./chains-By83jz87.js";const a={},w="binance";window.platform=w;async function v(){return 56}window.getChainId=v;async function C(){const o=window.location.href.split("/token/bsc/");let t="";return o.length>1&&(t=o[1].split("?")[0]),t||showToast("Token not found!","top-center","error")}window.getPoolId=C;async function T(){return C()}window.getTokenMint=T;let c="#00ff95",i="#00563180";function S(){document.querySelectorAll(".bloom-evm-buy-qt-btn").forEach(t=>{t.style.color=c,t.style.backgroundColor=i,t.style.borderColor=`${i}${i.length===7?"80":""}`;const n=t.querySelector("svg");n&&n.setAttribute("fill",c);const e=t.querySelector("span");e&&!isNaN(parseFloat(e.textContent))&&(e.textContent=d),t.onmouseover=function(){t.style.backgroundColor=i.slice(0,7)+"CC"},t.onmouseout=function(){t.style.backgroundColor=i}})}chrome.storage.onChanged.addListener(async o=>{if(o["bloom.quickBuyAmounts"]&&(d=await y()),o["bloom.quickBuyButton"]){const t=o["bloom.quickBuyButton"].newValue||{};c=t?.color||"#00ff95",i=t?.backgroundColor||"#00563180"}(o["bloom.quickBuyAmounts"]||o["bloom.quickBuyButton"])&&S()});let p,d,B;chrome.runtime.onMessage.addListener(async function(o){if(B=o.tabId,o.message==="txUpdate")return h(o);o.shouldWarnAboutUpdate&&showToast("A new version of the Bloom Extension is available! Check out how to install it <a href='https://docs.bloombot.app/extension/setup/download-chrome/.zip-file' target='_blank' style='text-decoration: underline; color: #EEA7ED;'>here</a>","top-center","info"),a.memescope?.forEach(e=>e.disconnect());const t=(await chrome.storage.local.get("bloom.sitePreferences"))?.["bloom.sitePreferences"]||{};if(E(),t[w]===!1)return;d=await y();const n=(await chrome.storage.local.get("bloom.quickBuyButton"))?.["bloom.quickBuyButton"]||{};if(c=n?.color||"#00ff95",i=n?.backgroundColor||"#00563180",o.message==="binance-memescope")!await getBloomToken()&&o.event==="onCompleted"&&showToast("Log in to the Bloom extension to enhance your experience!","top-center","info"),(a.memescope||[]).forEach(s=>s.disconnect()),p=o.message,A();else if(o.message==="binance-token"){!await getBloomToken()&&o.event==="onCompleted"&&showToast("Log in to the Bloom extension to enhance your experience!","top-center","info");const r=await C();if(!r||!r.startsWith("0x"))return;(a.token||[]).forEach(l=>l.disconnect()),p=o.message,k()}else if(o.message==="quickBuyAmount")p==="binance-memescope"&&Array.from(document.querySelectorAll("a[href*='token/bsc/']")).forEach(r=>{m(r,!0)});else if(o.message==="reset"&&p==="binance-token"){const e=await C();if(!e||!e.startsWith("0x"))return;k()}});async function A(){d=await y();const o=Array.from(document.querySelectorAll("div[class*='token-card-animation']"));o.forEach(e=>m(e,!0));const t=new IntersectionObserver(e=>{e.forEach(r=>{if(r.isIntersecting){const s=r.target;s.querySelector(".bloom-evm-buy-qt-btn")||m(s,!1)}})},{rootMargin:"100px",threshold:0});o.forEach(e=>t.observe(e));const n=new MutationObserver(e=>{e.forEach(r=>{r.addedNodes.forEach(s=>{if(s.nodeType!==Node.ELEMENT_NODE)return;s.matches?.("div[class*='token-card-animation']")&&(m(s,!0),t.observe(s)),s.querySelectorAll?.("div[class*='token-card-animation']")?.forEach(u=>{m(u,!0),t.observe(u)})})})});n.observe(document.body,{childList:!0,subtree:!0}),a.memescope=a.memescope||[],a.memescope.push(n,t)}async function m(o,t=!1){try{const n=o.querySelector(".bloom-evm-buy-qt-btn");if(n&&!t)return;n&&n.remove();const e=o.querySelector("button.bn-button__primary");if(!e)return;const r=o.querySelector('a[href*="/token/bsc/"]')?.href?.split("/token/bsc/")[1];if(!r)return;M(e,r,d)}catch(n){console.error("Failed to add Bloom button:",n)}}function M(o,t,n){const e=document.createElement("button");e.innerHTML=`
      <div style="display: flex; align-items: center; gap: 4px;">
        <svg style="flex-shrink: 0; width: 14px; height: 14px;" viewBox="0 0 40 40" fill="${c}" xmlns="http://www.w3.org/2000/svg">
          <path d="M37.0146 17.925C41.1031 22.5236 41.0995 29.1627 36.3408 32.01C35.2744 32.6581 34.0458 32.9768 32.8037 32.9768C31.5618 32.977 30.5089 34.0174 30.5088 35.2981V39.6702C24.1963 41.066 18.8553 37.8804 16.4795 33.0657C14.631 29.3229 14.5746 24.5943 17.2432 20.2678C17.3623 23.7545 20.1462 30.1369 26.083 31.4612C26.362 31.5204 26.638 31.5657 26.9121 31.5969C30.0776 31.9714 32.7697 30.65 32.7334 30.2053C32.6804 29.9881 32.1081 29.8967 31.3809 29.7815C30.8308 29.6941 30.192 29.5933 29.6221 29.4124C28.5833 29.0831 27.7736 28.4905 28.1436 27.2395C28.7538 26.2922 29.7681 26.1742 30.9062 26.0422C31.2816 25.9988 31.6712 25.953 32.0635 25.8762C34.093 25.4752 36.2101 24.2221 37.0146 17.925ZM11.8262 9.27563C16.235 9.24781 20.7146 11.5628 23.2471 16.3704C20.201 14.7307 13.3434 13.9774 9.24121 18.5149C7.90955 20.0187 7.22894 21.7821 6.96191 23.0872C6.7501 24.1287 6.80045 24.8758 6.99316 24.967C7.1 25.0175 7.25384 24.908 7.44336 24.7024C7.60309 24.532 7.78937 24.2954 7.99707 24.0315C8.76841 23.0513 9.83908 21.6907 10.9736 21.9143C12.6629 22.4111 12.0929 23.8403 11.46 25.429C11.1403 26.2311 10.8047 27.0745 10.7354 27.8586C10.6734 28.5137 10.7669 29.2842 11.1631 30.1711C11.7472 31.4725 12.9875 33.0302 15.3662 34.8577C9.38443 36.1383 3.70042 32.8175 3.64355 27.2258C3.64355 27.2066 3.64398 27.1868 3.64355 27.1672C3.64313 27.1477 3.64258 27.1278 3.64258 27.1086C3.64265 25.8925 3.98005 24.6986 4.58301 23.6428C5.21675 22.5329 4.83945 21.1152 3.74219 20.4739L0 18.2883C1.85727 12.3899 6.79708 9.30872 11.8262 9.27563ZM11.6943 7.42896C13.5878 1.54994 19.2745 -1.76803 24.0898 0.977783C25.177 1.58593 26.0664 2.50275 26.6885 3.59009C27.3225 4.70006 28.7247 5.08192 29.8223 4.44067L33.5654 2.25513C38.5807 7.82595 37.7402 15.305 33.3848 19.7659C31.0263 22.1844 27.6343 23.7147 23.584 23.5754C26.5111 21.7283 30.5836 16.0981 28.749 10.2366C28.6698 9.99261 28.583 9.75684 28.4863 9.52856C27.2373 6.5142 24.712 4.78065 24.3486 5.04028C24.1719 5.16619 24.3772 5.69334 24.6445 6.37915C24.773 6.70945 24.9167 7.07676 25.0381 7.45337C25.3464 8.4045 25.5164 9.41848 24.9727 10.051C23.7025 11.2845 22.7625 10.0702 21.7188 8.72095C21.1921 8.04027 20.6384 7.32551 20.0029 6.87329C19.7997 6.72699 19.5708 6.59274 19.3125 6.47778C17.9032 5.84351 15.6051 5.76851 11.6943 7.42896Z" fill="currentColor"/>
        </svg>
        <span style="white-space: nowrap; font-size: 13px; font-weight: 500;">${n}</span>
      </div>
    `,e.type="button",e.classList.add("bloom-evm-buy-qt-btn"),Object.assign(e.style,{position:"absolute",right:"64px",bottom:"11px",border:"1px solid",borderRadius:"12px",borderColor:`${i}${i.length===7?"80":""}`,color:c,backgroundColor:i,padding:"0px 8px",cursor:"pointer",zIndex:"1000",fontSize:"12px",visibility:"visible",transition:"background-color 0.2s ease"}),e.onmouseover=function(){e.style.backgroundColor=i.slice(0,7)+"CC"},e.onmouseout=function(){e.style.backgroundColor=i},e.onclick=async function(r){r.preventDefault(),r.stopPropagation();const s=await getBloomToken();if(!s)return chrome.runtime.sendMessage({message:"openTab",url:`https://t.me/BloomEVMbot?start=ref_QT_ca_${t}`});const l=await v();if(!l)return showToast("Failed to get chain ID!","top-center","error");let u=!1,f=["EU1","US1"],b=(await chrome.storage.local.get("bloom.activeRegion"))?.["bloom.activeRegion"]||"EU1";for(;!u&&f.length;){const x={selectedRegion:b,address:t,authToken:s,amount:n,side:"Buy",chainId:l},g=await executeSwap(x,B);if(g?.success)u=!0;else if(g?.error==="timeout"){h({status:4,side:"Buy"}),f.splice(f.indexOf(b),1),b=f[0],await chrome.storage.local.set({"bloom.activeRegion":b});break}else{h({status:4,side:"Buy",error:g?.error});break}}},insertBefore(o,e)}
