require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../error-handling/logger.cjs`),t=require(`../../functions/utils/token-check.utils.cjs`);let n=require(`idb-keyval`);var r=new class{STORAGE_KEY=`hiddenTokenAddresses`;UNHIDDEN_SPAM_KEY=`unhiddenSpamTokens`;async getHiddenTokens(t){try{return(await(0,n.get)(this.STORAGE_KEY))?.[t]||[]}catch(t){return e.Logger.error(`Error retrieving hidden tokens`,t),[]}}async getHiddenTokenAddresses(t){try{return(await this.getHiddenTokens(t)).map(({erc20TokenAddress:e})=>e.toLowerCase())}catch(t){return e.Logger.error(`Error retrieving hidden tokens`,t),[]}}async filterOutHiddenTokens(e,n){let r=await this.getHiddenTokens(e);return n.filter(e=>!r.some(n=>t.isSameERC20Token(n,e)))}async hideToken(r){try{await(0,n.update)(this.STORAGE_KEY,e=>{let n=e??{},{chainId:i}=r;return n[i]||(n[i]=[]),n[i].some(e=>t.isSameERC20Token(e,r))||n[i].push(r),n}),r.isSpam&&await this.deleteUnhiddenSpamToken(r)}catch(t){e.Logger.error(`Error hiding token`,t)}}async showToken(r){try{await(0,n.update)(this.STORAGE_KEY,e=>{let{chainId:n}=r,i=e??{};return i[n]||(i[n]=[]),i[n]=i[n].filter(e=>!t.isSameERC20Token(e,r)),i}),r.isSpam&&await this.storeUnhiddenSpamToken(r)}catch(t){e.Logger.error(`Error showing token`,t)}}async storeUnhiddenSpamToken(r){let{chainId:i}=r;try{await(0,n.update)(this.UNHIDDEN_SPAM_KEY,e=>{let n=e??{};return n[i]||(n[i]=[]),n[i].some(e=>t.isSameERC20Token(e,r))||n[i].push(r),n})}catch(t){e.Logger.error(`Error storing unhidden spam token`,t)}}async deleteUnhiddenSpamToken(e){await(0,n.update)(this.UNHIDDEN_SPAM_KEY,n=>{let{chainId:r}=e,i=n??{};return i[r]=i[r]?.filter(n=>!t.isSameERC20Token(n,e)),i})}async getUnhiddenSpamTokens(t){try{return(await(0,n.get)(this.UNHIDDEN_SPAM_KEY))?.[t]||[]}catch(t){return e.Logger.error(`Error retrieving unhidden spam tokens`,t),[]}}async hideUnnecessarySpamTokens(e,n){let r=await this.getUnhiddenSpamTokens(e),i=n.filter(e=>e.isSpam?!r.some(n=>t.isSameERC20Token(n,e)):!1);await Promise.all(i.map(e=>this.hideToken(e)))}async hideSpamsAndFetchHiddenTokensAddresses(e,t){return await this.hideUnnecessarySpamTokens(e,t),this.getHiddenTokenAddresses(e)}};exports.tokenVisibilityStorage=r;