var C=Math.pow;var v=(n,t,a)=>new Promise((h,c)=>{var o=s=>{try{i(a.next(s))}catch(u){c(u)}},r=s=>{try{i(a.throw(s))}catch(u){c(u)}},i=s=>s.done?h(s.value):Promise.resolve(s.value).then(o,r);i((a=a.apply(n,t)).next())});import{useCallback as V,useEffect as T,useState as w}from"react";import{createUmi as L}from"@metaplex-foundation/umi-bundle-defaults";import{fetchCandyGuard as O,mplCandyMachine as $,safeFetchCandyMachine as F}from"@metaplex-foundation/mpl-candy-machine";import{useWallet as R}from"@solana/wallet-adapter-react";import{useToast as W}from"@chakra-ui/react";import{walletAdapterIdentity as q}from"@metaplex-foundation/umi-signer-wallet-adapters";import{clusterApiUrl as z,Connection as E,LAMPORTS_PER_SOL as _,PublicKey as J}from"@solana/web3.js";import{publicKey as N}from"@metaplex-foundation/umi";import{fromWeb3JsPublicKey as S,toWeb3JsPublicKey as I}from"@metaplex-foundation/umi-web3js-adapters";import{getAssociatedTokenAddressSync as D,getMint as K}from"@solana/spl-token";import{fetchDigitalAsset as x}from"@metaplex-foundation/mpl-token-metadata";var U=(n,t,a)=>n==="devnet"||n===9091?{rpc:"https://kora-disz8d-fast-devnet.helius-rpc.com/",bundlrAddress:"https://devnet.bundlr.network",bundlrProviderUrl:"https://kora-disz8d-fast-devnet.helius-rpc.com/",explorer:`https://solscan.io/${a}/${t}?cluster=devnet`}:n==="mainnet-beta"||n===9090?{rpc:"https://still-patient-panorama.solana-mainnet.quiknode.pro/a9222e86a1f5d32291a0bfa0b5fde4f9309e04cf/",bundlrAddress:"https://node1.bundlr.network",bundlrProviderUrl:"https://still-patient-panorama.solana-mainnet.quiknode.pro/a9222e86a1f5d32291a0bfa0b5fde4f9309e04cf/",explorer:`https://solscan.io/${a}/${t}`}:{rpc:"http://127.0.0.1:8899",bundlrAddress:"https://devnet.bundlr.network",bundlrProviderUrl:z("devnet"),explorer:`https://solscan.io/${a}/${t}?cluster=custom`},j=n=>{let t=R(),a=W(),[h,c]=w(),[o,r]=w(),[i,s]=w(!1),[u,M]=w(),[A,l]=w(),[d,e]=w(),{chainId:f,contract:{candyMachineId:k},mintPrice:g,mintPriceUnit:b}=n;T(()=>{if(f&&t.publicKey&&[9090,9091].includes(f)){let p=f===9091?"devnet":"mainnet-beta",P=new E(U(p).rpc);c(P),r(L(U(p).rpc).use($()).use(q(t)))}},[t,f]);let m=V(p=>v(void 0,null,function*(){try{if(!o||!p||!t.publicKey)return;let P=N(S(new J(p))),y=yield F(o,P),B=yield O(o,y==null?void 0:y.mintAuthority);return{candyMachine:y,candyGuard:B}}catch(P){throw a({title:"Invalid Candy Machine",description:"Provided CMID is not CMv3.",status:"error",duration:6e3}),P}}),[o,t.publicKey]);return T(()=>{let p=()=>v(void 0,null,function*(){let P=k;if(P)try{s(!0);let y=yield m(P);if(!y)return;let{ticker:B,price:G,buttonTitle:X,isButtonDisabled:Y,tokenMint:Z,tokenMintMetadata:tt,balance:et}=yield H(y,h,t.publicKey,o);e(B),l(G),M(y),s(!1)}catch(y){console.log(y)}finally{}});t.publicKey&&!(n!=null&&n.isCollectionContest)&&p(),n!=null&&n.isCollectionContest&&(e(b),l(g))},[k,m,t.publicKey]),{umi:o,getCandyMachine:m,getUrls:U,connection:h,candyMachineState:u,solanaMintPrice:A,solanaMintPriceUnit:d,isFetching:i}},H=(n,t,a,h)=>v(void 0,null,function*(){let c="SOL",o,r,i,s,u,M,{buttonTitle:A,isButtonDisabled:l,price:d,ticker:e,tokenMint:f,tokenMintMetadata:k,balance:g}=yield Q(n,t,a,h);return r=A,i=l,o=d,c=e,s=f,u=k,M=g,{ticker:c,price:o,buttonTitle:r,isButtonDisabled:i,tokenMint:s,tokenMintMetadata:u,balance:M}}),Q=(n,t,a,h)=>v(void 0,null,function*(){try{let c="SOL",o=0,r,i,s,u,M,{candyMachine:A,candyGuard:l}=n;if(A&&l){if(l.guards.solPayment.__option==="Some"){let d=parseInt(l.guards.solPayment.value.lamports.basisPoints.toString());o=d/_;let e=yield t.getBalance(a);M=e/_,e<d?(r="Insufficient Balance",i=!0):(r="Mint",i=!1)}if(l.guards.tokenPayment.__option==="Some"){let d=l.guards.tokenPayment.value,e=I(d.mint),f=d.amount,k=parseInt(f.toString());s=e;let g=yield K(t,e);o=k/C(10,g.decimals);try{let m=yield x(h,S(e));u=m.metadata,c=m.metadata.symbol.toLowerCase()}catch(m){c="unknown"}if((yield t.getParsedTokenAccountsByOwner(a,{mint:e})).value.length===0)r="No Token Account",i=!0;else{let m=D(e,a),p=yield t.getTokenAccountBalance(m);M=p.value.uiAmount,p.value.uiAmount<k?(r="Insufficient Balance",i=!0):(r="Mint",i=!1)}}if(l.guards.tokenBurn.__option==="Some"){let d=l.guards.tokenBurn.value,e=I(d.mint),f=parseInt(d.amount.toString());s=e;let k=yield K(t,e);o=f/C(10,k.decimals);try{let b=yield x(h,S(e));u=b.metadata,c=b.metadata.symbol.toLowerCase()}catch(b){c="unknown"}if((yield t.getParsedTokenAccountsByOwner(a,{mint:e})).value.length===0)r="No Token Account",i=!0;else{let b=D(e,a),m=yield t.getTokenAccountBalance(b);M=m.value.uiAmount,m.value.uiAmount<f?(r="Insufficient Balance",i=!0):(r="Mint",i=!1)}}}return{ticker:c,price:o,buttonTitle:r,isButtonDisabled:i,tokenMint:s,tokenMintMetadata:u,balance:M}}catch(c){throw c}}),bt=j;export{bt as default,U as getUrls};
