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