import{useEffect as L,useState as At}from"react";import"react";import K from"axios";import{createContext as J,useMemo as Ut}from"react";import{jsx as Rt}from"react/jsx-runtime";var Wt=J(void 0),$="production";var f=$==="development"?"https://qa-api-v2.bandit.network/sdk":"https://api-v2.bandit.network/sdk";var k=async(t,e)=>{try{return(await K.get(`${f}/users/${t}`,{headers:{"X-API-KEY":e}})).data}catch(s){throw new Error(s)}};var E=async(t,e)=>{try{return(await K.post(`${f}/users/getNonce`,{walletAddress:t},{headers:{"X-API-KEY":e}})).data}catch(s){throw new Error(s)}};import{useDispatch as rt,useSelector as at}from"react-redux";import{useEffect as Q,useState as Z}from"react";import{AppConfig as j,UserSession as tt}from"@stacks/connect";var et=()=>{let[t,e]=Z(""),s=new j(["store_write","publish_data"]),o=new tt({appConfig:s}),d=()=>{o.signUserOut(),e("")};return Q(()=>{if(o)try{let c=o.loadUserData();e(c.profile.stxAddress.testnet)}catch{}},[o]),{stacksAddress:t,userSession:o,disconnectStacks:d}},T=et;import{useWeb3React as nt}from"@web3-react/core";import{useWallet as ct}from"@solana/wallet-adapter-react";import{createSlice as st}from"@reduxjs/toolkit";var ot={selectedWallet:void 0,selectedSolanaWallet:void 0,accessKey:null,details:{}},U=st({name:"user",initialState:ot,reducers:{updateSelectedWallet(t,{payload:{wallet:e}}){t.selectedWallet=e},updateSolanaSelectedWallet(t,{payload:{wallet:e}}){t.selectedSolanaWallet=e},updateAccessKey(t,{payload:{accessKey:e}}){t.accessKey=e},updateUserDetails(t,{payload:{details:e}}){t.details=e}}}),{updateSelectedWallet:Ht,updateSolanaSelectedWallet:Jt,updateAccessKey:Qt,updateUserDetails:b}=U.actions,Zt=U.reducer;var it=()=>{let{selectedWallet:t,accessKey:e,details:s={}}=at(i=>i.user),{stacksAddress:o}=T(),{account:d}=nt(),{publicKey:c,disconnect:n}=ct(),l=rt();return{user:s,fetchUser:async()=>{try{let i=c?c.toBase58():"",y=t==="stacks"?o:t==="Phantom"?i:d;if(s?.walletAddress?.toLowerCase()===y?.toLowerCase())return;let x=await k(y,e);l(b({details:x}))}catch(i){console.log(i)}},refreshUser:async()=>{try{if(!s.walletAddress)return;let i=await k(s.walletAddress,e);l(b({details:i}))}catch(i){console.log(i)}}}},W=it;import N from"axios";var R=async(t,e,s)=>{try{return(await N.post(`${f}/collection/contest/${t}/verifyContest`,e,{headers:{"X-API-KEY":s}})).data}catch(o){throw new Error(o)}},D=async(t,e,s)=>{try{return(await N.get(`${f}/users/${t}/contest/${e}/getTaskStatus`,{headers:{"X-API-KEY":s}})).data}catch(o){throw new Error(o)}};import{useSelector as G}from"react-redux";import{useToast as kt}from"@chakra-ui/react";import{useEffect as I,useState as B}from"react";import{hexlify as lt}from"@ethersproject/bytes";import{toUtf8Bytes as dt}from"@ethersproject/strings";var _=async(t,e,s)=>{if(t.provider?.wc){let o=lt(dt(s));return await t.provider?.wc.signPersonalMessage([o,e])}return t.getSigner(e).signMessage(s)};var Y="CONNECTOR_ID",h="SIGNATURE";import{decodeUTF8 as ht}from"tweetnacl-util";import{useWallet as wt}from"@solana/wallet-adapter-react";import*as V from"bs58";import{useEffect as ut,useRef as pt}from"react";var mt=t=>{let e=pt();return ut(()=>{e.current=t},[t]),e.current},X=mt;import{useWeb3React as yt}from"@web3-react/core";import{useToast as St}from"@chakra-ui/react";import{useSelector as xt}from"react-redux";import{useContext as gt}from"react";import{createContext as ft,useCallback as Ke,useEffect as Te,useState as Ue}from"react";import Se from"axios";import{jsx as ke,jsxs as Ee}from"react/jsx-runtime";import{jsx as De}from"react/jsx-runtime";var M=ft(void 0);var O=()=>gt(M);var Ct=()=>{let t=wt(),e=St(),[s,o]=B(null),[d,c]=B(null),{account:n,provider:l}=yt(),{accessKey:g}=O(),w=xt(a=>a.user.selectedWallet),i=X(n);return I(()=>{let a=localStorage.getItem(h);o(a),c(w)},[n,t.connected,w]),I(()=>{let a=localStorage.getItem(Y);!n&&!a&&(localStorage.removeItem(h),o(null))},[n]),I(()=>{n&&n!==i&&(localStorage.removeItem(h),o(null))},[n]),[s,d==="Phantom"?async()=>{if(s)return s;try{let{data:a}=await E(t?.publicKey?.toBase58(),g),u=t&&`Verify your account: ${t?.publicKey?.toBase58()}
Nonce: ${a}`,A=ht(u),P=t&&await t?.signMessage(A),S=V.encode(P);return localStorage.setItem(h,S),o(S),S}catch(a){throw a}}:async()=>{if(s)return s;try{let{data:a}=await E(n,g),u=await _(l,n,`Verify your account: ${n}
Nonce: ${a}`);return localStorage.setItem(h,u),o(u),u}catch(a){throw a}}]},F=Ct;import{useWallet as Et}from"@solana/wallet-adapter-react";import{useWeb3React as bt}from"@web3-react/core";var It=({collectionId:t,contestMetaData:e=[],isCollectionContest:s,chainId:o,refresh:d=!1})=>{let[c,n]=At({}),{user:l}=W(),g=G(r=>r.user.accessKey),w=G(r=>r.user.selectedWallet),[i,y]=F(),x=Et(),{account:v}=bt(),a=kt();L(()=>{t&&s&&l?.walletAddress&&u()},[t,l?.walletAddress,d]),L(()=>{l?.walletAddress||n({})},[l?.walletAddress,d]);let u=async()=>{try{let r=await D(l?.walletAddress,t,g),m={};r.forEach(p=>{m={...m,[p.moduleId]:p}}),n(m)}catch(r){console.log(r)}},A=async(r,m)=>{try{let p=i;p||(p=await y()),await R(t,{moduleId:r,task:m,walletAddress:w==="Phantom"?x?.publicKey?.toBase58():v,signature:p},g),await u(),a({title:"Verification success",description:"",status:"success"})}catch(p){a({title:"Verification Failed",description:"",status:"error"}),console.log(p)}},S=e.filter(r=>c[r.id]?.tasksCompleted?.length===r?.tasks?.length&&c[r.id]?.isClaimed).length===e.length,C=e.filter(r=>c[r.id]?.tasksCompleted?.length===r?.tasks?.length&&!c[r.id]?.isClaimed),q=C.length>0,z=C?C.reduce((r,m)=>r+m.rewardData?.count,0):0,H=C.map(r=>r.id);return{contestCompleted:S,isEligibleToMint:q,tokensEligibleToClaim:z,eligibleModules:H,claimedTokens:0,availableTokens:11,modulesStatus:c,verifyModuleTask:A,user:l,getUserCompletedTasks:u,chainId:o}},ds=It;export{ds as default};
