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