import{l as O,i as U,C as d}from"./config-DkKno7iL.js";document.addEventListener("DOMContentLoaded",async function(){const _=await O();if(_.length===0){console.error("Failed to load tokens");return}const g=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"});function E(t,c){return d[t]?d[t].price:c!==void 0?parseFloat(c.current_price):(console.warn(`${t} price not found, using fallback`),0)}function w(t,c){return d[t]?d[t].price_change_percentage_24h:(c==null?void 0:c.price_change_percentage_24h)||0}function P(t,c){return d[t]?d[t].market_cap||0:(c==null?void 0:c.market_cap)||0}async function h(t,c){let a=0,C=0,s=0,i=0;for(const e of c){const r=e.toLowerCase(),F=`${r}_user_worth`,A=`${r}_price`,I=`${r}_price_change`,L=`${r}_price_change_24h`,T=`${r}_balance`,k=`${r}_value`,B=`${r}_fiat_value`,N=`${r}_market_cap`,$=document.querySelector(`.${F}`),y=$?parseFloat($.value):0;y>0&&C++;try{const f=t.find(o=>o.symbol===e.toLowerCase()),S=E(e,f),p=y*S;document.querySelectorAll(`.${A}`).forEach(o=>{o.textContent=g.format(S)}),document.querySelectorAll(`.${T}`).forEach(o=>{o.textContent=`${y.toFixed(8)} ${e}`}),document.querySelectorAll(`.${k}, .${B}`).forEach(o=>{o.textContent=g.format(p)});const l=w(e,f);document.querySelectorAll(`.${I}, .${L}`).forEach(o=>{const q=l>=0?`+${l.toFixed(2)}%`:`${l.toFixed(2)}%`;o.textContent=q,o.classList.remove("text-red-400","text-secondary-400","text-gray-400"),l<0?o.classList.add("text-red-400"):l>0?o.classList.add("text-secondary-400"):o.classList.add("text-gray-400")});const v=P(e,f);document.querySelectorAll(`.${N}`).forEach(o=>{v>0?o.textContent=g.format(v):o.textContent="N/A"}),a+=p,p>0&&(s+=p*l/100,i+=p)}catch(f){console.error(`Error updating ${e}:`,f)}}const u=document.getElementById("totalBalance");u&&(u.textContent=g.format(a));const m=document.getElementById("activePositions");m&&(m.textContent=C);const n=document.getElementById("portfolioChange");if(n&&i>0){const e=s/i*100,r=e>=0?`+${e.toFixed(2)}%`:`${e.toFixed(2)}%`;n.textContent=r,n.classList.remove("text-red-400","text-secondary-400","text-gray-400"),e<0?n.classList.add("text-red-400"):e>0?n.classList.add("text-secondary-400"):n.classList.add("text-gray-400")}}async function x(t){if(!await U()){console.warn("CoinGecko API is inactive, showing 0/N/A values"),h([],t);return}const a="fraser_crypto_data",C=300,s=localStorage.getItem(a),i=localStorage.getItem(a+"_timestamp"),u=new Date().getTime(),m=u-i;if(s&&i&&m<=C*1e3){const n=JSON.parse(s);h(n,t)}else try{const n=await fetch("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=24h");if(!n.ok)throw new Error(`HTTP error! status: ${n.status}`);const e=await n.json();localStorage.setItem(a,JSON.stringify(e)),localStorage.setItem(a+"_timestamp",u),h(e,t)}catch(n){if(console.error("Price fetching error:",n),s){console.log("Using expired cached data as fallback");const e=JSON.parse(s);h(e,t)}}}x(_),setInterval(()=>{x(_)},3e4)});
