import{removeBloomQuickPanels as A}from"./panel.js-PlKG13TM.js";import{createQuickTradingPanelReact as L}from"./panel-new.ts-DTBxtLQQ.js";import{showSwapToast as B,getBloomToken as E,getApiChainIdForAddress as N}from"./utils.js-CC0KFyjC.js";import"./chains-By83jz87.js";const h="discord";window.platform=h;const u={color:"#00ff95",backgroundColor:"#00563180"},I=[".bloom-evm-buy-qt-btn",".bloom-evm-panel-btn"],r={color:u.color,backgroundColor:u.backgroundColor,quickBuyAmounts:{},autoBuyStartedAt:0,seenMessages:[],boughtAddresses:[],currentPage:null,currentObservers:[],tabId:null,processingMessages:new Set};async function O(){const{"bloom.quickBuyButton":e}=await chrome.storage.local.get("bloom.quickBuyButton");r.color=e?.color||u.color,r.backgroundColor=e?.backgroundColor||u.backgroundColor}async function D(){const{"bloom.quickBuyAmounts":e}=await chrome.storage.local.get("bloom.quickBuyAmounts");r.quickBuyAmounts=e||{}}async function S(){await Promise.all([O(),D()])}function f(){const e=r.backgroundColor;return e.length===7?`${e}80`:e}function q(){return r.backgroundColor.slice(0,7)+"CC"}function P(e){e.style.color=r.color,e.style.backgroundColor=r.backgroundColor,e.style.borderColor=f();const t=e.querySelector("svg");t&&t.setAttribute("fill",r.color)}function d(e){e.onmouseover=()=>{e.style.backgroundColor=q()},e.onmouseout=()=>{e.style.backgroundColor=r.backgroundColor}}function T(e){const t=apiToNumeric[e];if(!t)return"Buy";const o=chainIdToCurrency[t];if(!o)return"Buy";const n=r.quickBuyAmounts[o];return typeof n=="number"&&n>0?`${n} ${o}`:`${getDefaultFor(o)} ${o}`}async function _(e){try{const t=await N(e);if(t&&apiToNumeric[t])return await chrome.storage.local.set({"bloom.panelChain":t}),apiToNumeric[t]}catch(t){console.warn("Failed to resolve chain ID for address:",t)}return null}function F(e){const t=BigInt(e)>>22n;return new Date(Number(t)+14200704e5)}function V(){r.autoBuyStartedAt?(r.autoBuyStartedAt=0,showToast("EVM Auto Buy stopped!","top-center","info")):(r.autoBuyStartedAt=Date.now(),showToast("EVM Auto Buy started!"))}async function g(e,t){if(!r.autoBuyStartedAt||r.seenMessages.includes(t))return;r.seenMessages.push(t);const o=F(t);if(o<=new Date(r.autoBuyStartedAt)||Date.now()-o.getTime()>3e4||r.boughtAddresses.includes(e))return;r.boughtAddresses.push(e);const a=await E();if(!a)return;const s=await executeScraperSwap({address:e,authToken:a},r.tabId,!0);!s?.success&&s?.error!=="already_bought"&&B({status:4,side:"Buy",error:s?.error})}function $(e){const t=e.querySelector(".bloom-evm-auto-buy-toggle");t&&t.remove();const o=document.createElement("button");o.type="button",o.classList.add("bloom-evm-auto-buy-toggle"),Object.assign(o.style,{padding:"4px 8px",borderRadius:"6px",border:"1px solid",borderColor:f(),color:r.color,backgroundColor:r.backgroundColor,zIndex:"1000",cursor:"pointer",transition:"background-color 0.2s ease",fontWeight:"500",fontSize:"12px",marginRight:"8px",whiteSpace:"nowrap"}),d(o),v(o),o.addEventListener("click",()=>{V(),v(o)}),insertBefore(e.firstChild,o);const n=e;if(n&&n.style){const a=n.style.display;(!a||a==="")&&(n.style.display="flex"),n.style.alignItems=n.style.alignItems||"center"}}function v(e){e.textContent=r.autoBuyStartedAt?"Stop EVM Auto Buy":"Start EVM Auto Buy"}async function j(){const e=document.querySelectorAll(I.join(", ")),{"bloom.panelChain":t}=await chrome.storage.local.get("bloom.panelChain");for(const n of e)if(P(n),d(n),n.classList.contains("bloom-evm-buy-qt-btn")&&t){const a=n.querySelector("span");if(a){const s=T(t);s&&(a.textContent=s)}}const o=document.querySelector(".bloom-evm-auto-buy-toggle");o&&(o.style.color=r.color,o.style.borderColor=f(),o.style.backgroundColor=r.backgroundColor,d(o))}chrome.storage.onChanged.addListener(async e=>{let t=!1;if(e["bloom.quickBuyButton"]){const o=e["bloom.quickBuyButton"].newValue||{};r.color=o?.color||u.color,r.backgroundColor=o?.backgroundColor||u.backgroundColor,t=!0}e["bloom.quickBuyAmounts"]&&(r.quickBuyAmounts=e["bloom.quickBuyAmounts"].newValue||{},t=!0),t&&await j()});async function y(e){const t=await _(e);if(!t)return null;const{"bloom.panelChain":o}=await chrome.storage.local.get("bloom.panelChain"),n=T(o),a=document.createElement("span");a.style.display="inline-flex",a.style.gap="4px",a.style.marginLeft="4px",a.classList.add("bloom-evm-button-container");const s=R(e,t,n);a.appendChild(s);const c=z(e,o);return a.appendChild(c),a}function R(e,t,o){const n=document.createElement("button");return n.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="${r.color}" 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: 14px; font-weight: 500;">${o}</span>
    </div>
  `,n.type="button",n.classList.add("bloom-evm-buy-qt-btn"),n.dataset.chainId=t,Object.assign(n.style,{border:"1px solid",borderRadius:"6px",display:"flex",borderColor:f(),color:r.color,backgroundColor:r.backgroundColor,zIndex:"1000",cursor:"pointer",transition:"background-color 0.2s ease",padding:"0px 6px",flexShrink:0,height:"26px"}),d(n),n.onclick=async a=>{a.stopPropagation(),a.preventDefault(),await H(e)},n}function z(e,t){const o=document.createElement("button");return o.innerHTML=`
    <svg style="flex-shrink: 0; width: 14px; height: 14px;" viewBox="0 0 40 40" fill="${r.color}" 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>
  `,o.type="button",o.classList.add("bloom-evm-panel-btn"),o.dataset.apiChain=t,Object.assign(o.style,{border:"1px solid",borderColor:f(),color:r.color,backgroundColor:r.backgroundColor,width:"26px",height:"26px",display:"inline-flex",alignItems:"center",justifyContent:"center",borderRadius:"6px",flexShrink:0,zIndex:"1000",cursor:"pointer",transition:"background-color 0.2s ease"}),d(o),o.onclick=async n=>{n.stopPropagation(),n.preventDefault(),await U(o,e,t)},o}async function H(e,t){const o=await E();if(!o)return chrome.runtime.sendMessage({message:"openTab",url:`https://t.me/BloomEVMBot?start=ref_QT_ca_${e}`});const n=await executeScraperSwap({address:e,authToken:o},r.tabId,!1);n?.success||B({status:4,side:"Buy",error:n?.error})}async function U(e,t,o){A();const n=apiToNumeric[o];window.getChainId=async()=>n;const s=await L(e,t,()=>{});if(s){const c=new MutationObserver(i=>{i.forEach(l=>{l.removedNodes.forEach(b=>{b===s&&c.disconnect()})})});c.observe(document.body,{childList:!0})}}async function m(e,t){if(!r.processingMessages.has(t)&&!e.querySelector(".bloom-evm-button-container")){r.processingMessages.add(t);try{for(const o of Array.from(e.childNodes))await w(o,t)}catch(o){console.error("Failed to add Bloom button:",o)}finally{r.processingMessages.delete(t)}}}async function w(e,t){if(e.nodeType===Node.ELEMENT_NODE&&e.closest('div[class*="repliedTextPreview"]'))return;const o=/\b0x[a-fA-F0-9]{40}\b/g;if(e.nodeType===Node.TEXT_NODE){const n=e.textContent,a=e.parentNode;let s;const c=[];for(;(s=o.exec(n))!==null;)c.push({address:s[0],start:s.index,end:s.index+s[0].length});if(c.length===0)return;for(const{address:i}of c){g(i,t);const l=await y(i);l&&a?.isConnected&&a.insertAdjacentElement("afterend",l)}}else if(e.nodeType===Node.ELEMENT_NODE){const n=e.nodeName.toLowerCase();if(n==="a"){const c=(e.textContent||"").match(o);if(c?.length){for(const i of c){g(i,t);const l=await y(i);l&&e.insertAdjacentElement("afterend",l)}return}}if(n==="code"){const c=e.textContent.match(o);if(c?.length){for(const i of c){g(i,t);const l=await y(i);l&&e.insertAdjacentElement("afterend",l)}return}}if(n==="article"&&Array.from(e.classList).some(s=>s.includes("embedWrapper")||s.includes("embedFull"))){for(const s of Array.from(e.childNodes))await w(s,t);return}const a=Array.from(e.childNodes);for(const s of a)await w(s,t)}}async function M(e=5e3){for(let t=0;t<e/500;t++){const o=document.querySelector('section[class*="title"]');if(o&&o?.firstElementChild?.firstElementChild)return o.firstElementChild.firstElementChild;await new Promise(n=>setTimeout(n,500))}return null}function Z(){r.currentObservers.forEach(e=>e.disconnect()),r.currentObservers=[]}async function Q(){r.currentPage="discord",Z();const e=await M(15e3);if(!e)return;$(e),Array.from(document.body.querySelectorAll('li[class*="messageListItem"]')).forEach(n=>{const a=C(n);a&&m(n,a)});const o=new MutationObserver(n=>{for(const a of n)a?.addedNodes?.forEach(s=>{if(W(s)){const c=C(s);if(!c)return;m(s,c);const i=new MutationObserver(l=>{for(const b of l)b?.addedNodes?.forEach(x=>{if(X(x)){const p=x.closest('li[class*="messageListItem"]');if(!p)return;const k=C(p);k&&m(p,k)}})});i.observe(s,{childList:!0,subtree:!0}),r.currentObservers.push(i)}})});o.observe(document.body,{childList:!0,subtree:!0}),r.currentObservers.push(o)}function C(e){const t=e.getAttribute("id")?.split("-");return t?.[t.length-1]||null}function W(e){return e?.nodeName?.toLowerCase()==="li"&&Array.from(e.classList||[]).some(t=>t.includes("messageListItem"))}function X(e){return e?.nodeName?.toLowerCase()==="article"&&Array.from(e.classList||[]).some(t=>t.includes("embedWrapper")||t.includes("embedFull"))}async function G(){if(r.currentPage!=="discord"||!await M())return;document.body.querySelectorAll('li[class*="messageListItem"]').forEach(o=>{const n=C(o);n&&m(o,n)})}chrome.runtime.onMessage.addListener(async function(e){if(r.tabId=e.tabId,e.message==="txUpdate")return B(e);e.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");const t=(await chrome.storage.local.get("bloom.sitePreferences"))?.["bloom.sitePreferences"]||{};e.event!=="onActivated"?r.autoBuyStartedAt=0:r.autoBuyStartedAt&&t[h]!==!1&&(r.autoBuyStartedAt=Date.now()),A(),await S(),t[h]!==!1&&((e.message==="discord"||e.event==="onActivated"&&r.currentPage==="discord")&&await Q(),e.message==="quickBuyAmount"&&await G())});window.autoBuyStartedAt=r.autoBuyStartedAt;S();
