import { Eip1193WalletAdapter } from './adapters/Eip1193WalletAdapter';
/**
 * Supported wallet types
 */
export var WalletType;
(function (WalletType) {
    WalletType["MetaMask"] = "metamask";
    WalletType["Coinbase"] = "coinbase";
    WalletType["WalletConnect"] = "walletconnect";
    WalletType["Injected"] = "injected";
    WalletType["Rabby"] = "rabby";
    WalletType["Brave"] = "brave";
})(WalletType || (WalletType = {}));
function detectInjectedWallets() {
    if (typeof window === 'undefined') {
        return { hasMetaMask: false, hasCoinbaseExtension: false, hasRabby: false, hasBrave: false, hasInjected: false };
    }
    const ethereum = window.ethereum;
    const providers = ethereum?.providers || (ethereum ? [ethereum] : []);
    return {
        hasMetaMask: providers.some((p) => p.isMetaMask && !p.isRabby && !p.isBraveWallet),
        hasCoinbaseExtension: providers.some((p) => p.isCoinbaseWallet),
        hasRabby: providers.some((p) => p.isRabby),
        hasBrave: providers.some((p) => p.isBraveWallet),
        hasInjected: !!ethereum,
    };
}
function getSpecificProvider(type) {
    if (typeof window === 'undefined')
        return undefined;
    const ethereum = window.ethereum;
    if (!ethereum)
        return undefined;
    const providers = ethereum.providers;
    if (providers && Array.isArray(providers)) {
        switch (type) {
            case 'metamask': return providers.find((p) => p.isMetaMask && !p.isRabby && !p.isBraveWallet);
            case 'coinbase': return providers.find((p) => p.isCoinbaseWallet);
            case 'rabby': return providers.find((p) => p.isRabby);
            case 'brave': return providers.find((p) => p.isBraveWallet);
        }
    }
    const eth = ethereum;
    switch (type) {
        case 'metamask': return eth.isMetaMask && !eth.isRabby && !eth.isBraveWallet ? ethereum : undefined;
        case 'coinbase': return eth.isCoinbaseWallet ? ethereum : undefined;
        case 'rabby': return eth.isRabby ? ethereum : undefined;
        case 'brave': return eth.isBraveWallet ? ethereum : undefined;
    }
}
/**
 * Wallet Registry - manages available wallets and creates adapters
 */
export class WalletRegistry {
    config;
    constructor(config = {}) {
        this.config = config;
    }
    getAvailableWallets() {
        const detected = detectInjectedWallets();
        const wallets = [
            {
                id: WalletType.MetaMask,
                name: 'MetaMask',
                description: detected.hasMetaMask ? 'Connect using MetaMask' : 'Popular Ethereum wallet',
                icon: '/wallets/metamask.svg',
                installed: detected.hasMetaMask,
                downloadUrl: 'https://metamask.io/download/',
            },
            {
                id: WalletType.Coinbase,
                name: 'Coinbase Wallet',
                description: detected.hasCoinbaseExtension ? 'Connect using Coinbase Wallet' : 'Secure wallet by Coinbase',
                icon: '/wallets/coinbase.svg',
                installed: detected.hasCoinbaseExtension,
                downloadUrl: 'https://www.coinbase.com/wallet/downloads',
                isExternal: !detected.hasCoinbaseExtension,
            },
            {
                id: WalletType.WalletConnect,
                name: 'WalletConnect',
                description: 'Scan with your mobile wallet',
                icon: '/wallets/walletconnect.svg',
                installed: true,
                isExternal: true,
            },
        ];
        if (detected.hasRabby) {
            wallets.push({
                id: WalletType.Rabby, name: 'Rabby', description: 'Connect using Rabby Wallet',
                icon: '/wallets/rabby.svg', installed: true,
            });
        }
        if (detected.hasBrave) {
            wallets.push({
                id: WalletType.Brave, name: 'Brave Wallet', description: 'Connect using Brave Wallet',
                icon: '/wallets/brave.svg', installed: true,
            });
        }
        return wallets;
    }
    async createAdapter(walletType) {
        switch (walletType) {
            case WalletType.MetaMask:
            case WalletType.Rabby:
            case WalletType.Brave: {
                const provider = getSpecificProvider(walletType);
                return new Eip1193WalletAdapter({ provider });
            }
            case WalletType.Injected:
                return new Eip1193WalletAdapter();
            case WalletType.Coinbase: {
                const cbProvider = getSpecificProvider('coinbase');
                if (cbProvider)
                    return new Eip1193WalletAdapter({ provider: cbProvider });
                const { CoinbaseWalletAdapter } = await import('./adapters/CoinbaseWalletAdapter');
                return new CoinbaseWalletAdapter({
                    appName: this.config.appName || 'PrivacyBoost',
                    appLogoUrl: this.config.appLogoUrl,
                    defaultChainId: this.config.chainId,
                    jsonRpcUrl: this.config.rpcUrl,
                });
            }
            case WalletType.WalletConnect: {
                if (!this.config.walletConnectProjectId) {
                    throw new Error('WalletConnect requires a project ID.');
                }
                const { WalletConnectAdapter } = await import('./adapters/WalletConnectAdapter');
                return new WalletConnectAdapter({
                    projectId: this.config.walletConnectProjectId,
                    chains: this.config.chainId ? [this.config.chainId] : [1],
                    metadata: {
                        name: this.config.appName || 'PrivacyBoost',
                        description: this.config.appDescription || 'Privacy-preserving transfers',
                        url: this.config.appUrl || 'https://privacyboost.io',
                        icons: this.config.appIcons || ['https://privacyboost.io/icon.png'],
                    },
                    rpcUrls: this.config.rpcUrl && this.config.chainId
                        ? { [this.config.chainId]: this.config.rpcUrl }
                        : undefined,
                });
            }
            default:
                throw new Error(`Unknown wallet type: ${walletType}`);
        }
    }
    hasAnyWallet() {
        return this.getAvailableWallets().some((w) => w.installed || w.isExternal);
    }
    getRecommendedWallet() {
        const wallets = this.getAvailableWallets();
        return wallets.find((w) => w.installed && !w.isExternal)
            || wallets.find((w) => w.isExternal)
            || wallets[0] || null;
    }
}
export function createWalletRegistry(config) {
    return new WalletRegistry(config);
}
//# sourceMappingURL=WalletRegistry.js.map