import { useContext } from 'react' import { ConnectionContext } from '../providers/ConnectionProvider' import type { WalletMetadata, UseWalletReturn } from '@meshconnect/uwc-types' import { WalletConnectorError } from '@meshconnect/uwc-types' /** * Hook for accessing a specific wallet configuration * @param {string} walletId - The ID of the wallet to access * @returns {UseWalletReturn} Object containing the wallet metadata and error if no wallet was found * @throws {Error} when used outside of ConnectionProvider * @example * ```tsx * const { wallet, error } = useWallet('metamask') * if (wallet) { * console.log(wallet.name) // 'MetaMask' * } else { * console.log(error?.message) // 'Wallet not found' * } */ export function useWallet(walletId: string): UseWalletReturn { const context = useContext(ConnectionContext) if (!context) { throw new Error('useWallet must be used within a ConnectionProvider') } const selectedWallet = context.wallets.find( (wallet: WalletMetadata) => wallet.id === walletId ) if (!selectedWallet) { return { error: new WalletConnectorError({ type: 'unknown', message: `Wallet with id '${walletId}' not found` }) } } return { wallet: selectedWallet } }