import{useCallback as H,useEffect as J,useState as m}from"react";import Z from"dayjs";import{useWeb3React as lt}from"@web3-react/core";import{useMemo as dt}from"react";import{isAddress as at}from"@ethersproject/address";import{AddressZero as st}from"@ethersproject/constants";import{Contract as ct}from"@ethersproject/contracts";function it(t,e){return t.getSigner(e).connectUnchecked()}function ut(t,e){return e?it(t,e):t}function M(t,e,o,n){if(!at(t)||t===st)throw Error(`Invalid 'address' parameter '${t}'.`);return new ct(t,e,ut(o,n))}function B(t,e,o=!0){let{provider:n,account:c,chainId:p}=lt();return dt(()=>{if(!t||!e||!n||!p)return null;let r;if(typeof t=="string"?r=t:r=t[p],!r)return null;try{return M(r,e,n,o&&c?c:void 0)}catch(w){return console.error("Failed to get contract",w),null}},[t,e,n,p,o,c])}import{useWeb3React as kt}from"@web3-react/core";import{useToast as Nt}from"@chakra-ui/react";import{createContext as pt,useMemo as Gt}from"react";import{jsx as zt}from"react/jsx-runtime";var qt=pt(void 0),L="production";var g=L==="development"?"https://qa-api-v2.bandit.network/sdk":"https://api-v2.bandit.network/sdk";import Y from"axios";var X=async(t,e,o,n=!1)=>{try{return(await Y.post(`${g}/collection/mint/${t}`,o,{headers:{"X-API-KEY":e}})).data}catch(c){if(console.log(c),n)throw c}},U=async(t,e,o)=>{try{return(await Y.post(`${g}/collection/${t}/mints/error`,o,{headers:{"X-API-KEY":e}})).data}catch(n){console.log(n)}};import _t from"bignumber.js";import mt from"bignumber.js";var ft=new mt(10),V=ft.pow(18);import{useContext as ht}from"react";import{createContext as gt,useCallback as xe,useEffect as Ce,useState as Ee}from"react";import ie from"axios";import{jsx as pe,jsxs as me}from"react/jsx-runtime";import{jsx as ve}from"react/jsx-runtime";var F=gt(void 0);var E=()=>ht(F);import{useEffect as k,useState as q}from"react";import{hexlify as wt}from"@ethersproject/bytes";import{toUtf8Bytes as yt}from"@ethersproject/strings";var O=async(t,e,o)=>{if(t.provider?.wc){let n=wt(yt(o));return await t.provider?.wc.signPersonalMessage([n,e])}return t.getSigner(e).signMessage(o)};var W="CONNECTOR_ID",h="SIGNATURE";import{decodeUTF8 as It}from"tweetnacl-util";import{useWallet as bt}from"@solana/wallet-adapter-react";import*as j from"bs58";import{useEffect as xt,useRef as Ct}from"react";var Et=t=>{let e=Ct();return xt(()=>{e.current=t},[t]),e.current},G=Et;import{useWeb3React as vt}from"@web3-react/core";import{useToast as Pt}from"@chakra-ui/react";import{useSelector as At}from"react-redux";import St from"axios";var $=async(t,e)=>{try{return(await St.post(`${g}/users/getNonce`,{walletAddress:t},{headers:{"X-API-KEY":e}})).data}catch(o){throw new Error(o)}};var $t=()=>{let t=bt(),e=Pt(),[o,n]=q(null),[c,p]=q(null),{account:r,provider:w}=vt(),{accessKey:u}=E(),y=At(s=>s.user.selectedWallet),S=G(r);return k(()=>{let s=localStorage.getItem(h);n(s),p(y)},[r,t.connected,y]),k(()=>{let s=localStorage.getItem(W);!r&&!s&&(localStorage.removeItem(h),n(null))},[r]),k(()=>{r&&r!==S&&(localStorage.removeItem(h),n(null))},[r]),[o,c==="Phantom"?async()=>{if(o)return o;try{let{data:s}=await $(t?.publicKey?.toBase58(),u),d=t&&`Verify your account: ${t?.publicKey?.toBase58()}
Nonce: ${s}`,I=It(d),f=t&&await t?.signMessage(I),x=j.encode(f);return localStorage.setItem(h,x),n(x),x}catch(s){throw s}}:async()=>{if(o)return o;try{let{data:s}=await $(r,u),d=await O(w,r,`Verify your account: ${r}
Nonce: ${s}`);return localStorage.setItem(h,d),n(d),d}catch(s){throw s}}]},z=$t;var Dt=({dynamicConfig:t,contractAddress:e,mintPrice:o,chainId:n,collectionId:c,mintPriceUnit:p})=>{let[r,w]=m(1),[u,y]=m(null),[S,N]=m(!1),[_,D]=m({counter:null}),[s,d]=m(!1),[I,f]=m("start"),[x,Q]=m(null),{account:C}=kt(),{accessKey:b}=E(),[tt,et]=z(),K=Nt(),R=B(e,t&&t.abi?t.abi:[]);J(()=>{if(Object.keys(t).length){let a=t.mints.filter(l=>l.endless?!0:!Z.unix(l.endDate).isBefore(Z()));a.length?y(a[0]):N(!0)}},[t]),J(()=>{if(u){let a=u.inputs.find(l=>l.category.toLowerCase()==="counter");D({counter:a})}},[u]);let ot=H(a=>{y(t.mints[a])},[t]),rt=H(async()=>{try{d(!0),f("pending");let a=tt;a||(a=await et());let{method:l,inputs:v,payable:nt}=u,P=v.map(i=>{if(i.category.toLowerCase()==="input"){if(i.type.toLowerCase()==="address"||i.type.toLowerCase()==="text")return i.defaultValue;if(i.type.toLowerCase()==="user_address")return C}else if(i.category.toLowerCase()==="counter")return r});nt&&(P=[...P,{value:new _t(o).multipliedBy(r).multipliedBy(V).toFixed()}]);let A=await R[l](...P);Q(A.hash);let T=(await A.wait()).events.filter(i=>i.topics[0]==="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef").map(i=>parseInt(Number(i.topics[3])));await X(c,b,{walletAddress:C,transactionHash:A.hash,chainId:n,counterValue:r,price:o,priceUnit:p,tokens:T.length>0?T:Array.from(Array(r).keys()),signature:a}),f("completed")}catch(a){console.log(a),a.reason?K({title:"Failed to mint",description:a.reason,status:"error",duration:5e3}):K({title:"Failed to mint",description:a.message,status:"error",duration:5e3});let l=a?.reason?a?.reason:a?.message,v={errorLog:JSON.stringify({account:C,error:l})};l.includes("user rejected transaction")?f("start"):(f("error"),U(c,b,v))}finally{d(!1)}},[R,C,r,u,o,n,c,b]);return{selectedStage:u,changeSelectedStage:ot,displayConfig:_,counterValue:r,setCounterValue:w,initiateMint:rt,loading:s,mintStatus:I,mintedTx:x,stagesCompleted:S}},lo=Dt;export{lo as default};
