import { b as bs58Exports } from '../../../../_virtual/index3.mjs';
import 'react';
import { GenericWalletIcon } from '../assets/genericWallet.mjs';
import { usePresentation, getButtonId } from '../ui/components/PresentationConfig.mjs';
import { Wallets } from '../../../core/src/public/ui.mjs';

const hasMultipleEthereumWallets = ()=>!!window.ethereum?.providers;
const hasMetaMask = ()=>!!(hasMultipleEthereumWallets() ? window.ethereum.providers?.find((wallet)=>wallet.isMetaMask) : window.ethereum.isMetaMask);
const hasCoinbaseWallet = ()=>!!(hasMultipleEthereumWallets() ? window.ethereum.providers?.find((wallet)=>wallet.isCoinbaseWallet) : window.ethereum?.isCoinbaseWallet);
const hasOtherEthereumWallet = ()=>!hasMetaMask() && !hasCoinbaseWallet() && !!window.ethereum;
const getMetaMaskProvider = ()=>hasMultipleEthereumWallets() ? window.ethereum.providers?.find((wallet)=>wallet.isMetaMask) : window.ethereum;
const getCoinbaseProvider = ()=>hasMultipleEthereumWallets() ? window.ethereum.providers?.find((wallet)=>wallet.isCoinbaseWallet) : window.ethereum;
const getOtherInjectedProvider = ()=>window.ethereum.providers?.find((wallet)=>!wallet.isCoinbaseWallet && !wallet.isMetaMask);
const walletIcons = {
    [Wallets.Phantom]: 'phantom',
    [Wallets.Binance]: 'binance',
    [Wallets.Coinbase]: 'coinbase',
    [Wallets.Metamask]: 'metamask'
};
const useCryptoButtonProps = ()=>{
    const presentation = usePresentation();
    const iconRegistry = presentation.iconRegistry;
    return {
        getIcon: (wallet)=>{
            const iconName = walletIcons[wallet];
            return iconName ? iconRegistry[iconName] : GenericWalletIcon;
        },
        getId: (wallet)=>getButtonId(`wallet-${wallet}`, presentation.options)
    };
};
const WalletToText = {
    [Wallets.Phantom]: {
        id: "crypto.wallet.phantom",
        message: "Phantom"
    },
    [Wallets.Binance]: {
        id: "crypto.wallet.binance",
        message: "Binance"
    },
    [Wallets.Coinbase]: {
        id: "crypto.wallet.coinbase",
        message: "Coinbase"
    },
    [Wallets.Metamask]: {
        id: "crypto.wallet.metamask",
        message: "Metamask"
    },
    [Wallets.GenericEthereumWallet]: {
        id: "crypto.wallet.otherEthereum",
        message: "Other Ethereum Wallet"
    },
    [Wallets.GenericSolanaWallet]: {
        id: "crypto.wallet.otherSolana",
        message: "Other Solana Wallet"
    }
};
const isWalletVisible = (wallet)=>{
    switch(wallet){
        case Wallets.Metamask:
            return !!window.ethereum && hasMetaMask();
        case Wallets.Phantom:
            return (!!window.solana && window.solana?.isPhantom) ?? false;
        case Wallets.Binance:
            return !!window.BinanceChain;
        case Wallets.Coinbase:
            return !!window.ethereum && hasCoinbaseWallet();
        case Wallets.GenericEthereumWallet:
            return !!window.ethereum && hasOtherEthereumWallet();
        case Wallets.GenericSolanaWallet:
            return !!window.solana && !window.solana.isPhantom;
        default:
            return false;
    }
};
const connectWithEthereumProvider = async (request)=>{
    const [address] = await request({
        method: 'eth_requestAccounts'
    });
    return address;
};
const connectWithWallet = async ({ wallet })=>{
    switch(wallet){
        case Wallets.Metamask:
            {
                const provider = getMetaMaskProvider();
                return provider?.request ? connectWithEthereumProvider(provider?.request) : '';
            }
        case Wallets.Phantom:
            {
                const connectResp = await window.solana.connect();
                return connectResp.publicKey.toString();
            }
        case Wallets.Binance:
            {
                const provider = window.BinanceChain;
                return provider?.request ? connectWithEthereumProvider(provider?.request) : '';
            }
        case Wallets.Coinbase:
            {
                const provider = getCoinbaseProvider();
                return provider?.request ? connectWithEthereumProvider(provider?.request) : '';
            }
        case Wallets.GenericEthereumWallet:
            {
                const provider = getOtherInjectedProvider();
                return provider?.request ? connectWithEthereumProvider(provider?.request) : '';
            }
        case Wallets.GenericSolanaWallet:
            {
                const connectResp = await window.solana.connect();
                return connectResp.publicKey.toString();
            }
        default:
            return '';
    }
};
const signMessageWithSolanaProvider = async (message)=>{
    const encodedMessage = new TextEncoder().encode(message);
    const signResp = await window.solana.request({
        method: 'signMessage',
        params: {
            message: encodedMessage,
            display: 'utf8'
        }
    });
    return bs58Exports.encode(signResp.signature);
};
const signMessageWithEthereumProvider = async (request, message, address)=>{
    const signature = await request({
        method: 'personal_sign',
        params: [
            message,
            address
        ]
    });
    return signature;
};
const signMessageWithWallet = async ({ wallet, message, address })=>{
    switch(wallet){
        case Wallets.Phantom:
        case Wallets.GenericSolanaWallet:
            return signMessageWithSolanaProvider(message);
        case Wallets.Metamask:
            {
                const provider = getMetaMaskProvider();
                return provider?.request ? signMessageWithEthereumProvider(provider?.request, message, address) : '';
            }
        case Wallets.Binance:
            {
                const provider = window.BinanceChain;
                return provider?.request ? signMessageWithEthereumProvider(provider?.request, message, address) : '';
            }
        case Wallets.Coinbase:
            {
                const provider = getCoinbaseProvider();
                return provider?.request ? signMessageWithEthereumProvider(provider?.request, message, address) : '';
            }
        case Wallets.GenericEthereumWallet:
            {
                const provider = getOtherInjectedProvider();
                return provider?.request ? signMessageWithEthereumProvider(provider?.request, message, address) : '';
            }
        default:
            return '';
    }
};
const isSolanaWallet = (wallet)=>{
    const solanaWallets = [
        Wallets.Phantom,
        Wallets.GenericSolanaWallet
    ];
    return solanaWallets.includes(wallet);
};

export { WalletToText, connectWithWallet, isSolanaWallet, isWalletVisible, signMessageWithWallet, useCryptoButtonProps, walletIcons };
//# sourceMappingURL=crypto.mjs.map
