/**
 * WalletConnect Adapter
 *
 * Enables connection to any WalletConnect-compatible wallet (mobile wallets, etc.)
 * Uses WalletConnect v2 protocol.
 */
import { createWalletClient, custom } from 'viem';
export class WalletConnectAdapter {
    id = 'walletconnect';
    config;
    provider = null;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    EthereumProvider = null;
    constructor(config) {
        this.config = { showQrModal: true, chains: [1], ...config };
    }
    async getEthereumProviderModule() {
        if (!this.EthereumProvider) {
            // @ts-expect-error -- optional peer dependency, dynamically imported
            const module = await import('@walletconnect/ethereum-provider');
            this.EthereumProvider = module.EthereumProvider;
        }
        return this.EthereumProvider;
    }
    async connect(args) {
        const EthereumProvider = await this.getEthereumProviderModule();
        const rpcMap = { ...this.config.rpcUrls };
        if (args.expectedChainId && args.rpcUrl)
            rpcMap[args.expectedChainId] = args.rpcUrl;
        const chains = args.expectedChainId ? [args.expectedChainId] : this.config.chains || [1];
        const provider = await EthereumProvider.init({
            projectId: this.config.projectId,
            chains,
            optionalChains: this.config.optionalChains,
            showQrModal: this.config.showQrModal ?? true,
            metadata: this.config.metadata,
            rpcMap,
        });
        await provider.connect();
        this.provider = provider;
        const accounts = (await provider.request({ method: 'eth_accounts' }));
        if (!accounts || accounts.length === 0)
            throw new Error('No accounts available from WalletConnect');
        const account = accounts[0];
        const chainIdHex = (await provider.request({ method: 'eth_chainId' }));
        const chainId = parseInt(chainIdHex, 16);
        const chain = {
            id: chainId,
            name: 'Network',
            nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
            rpcUrls: {
                default: { http: [rpcMap[chainId] || args.rpcUrl || 'http://localhost:8545'] },
                public: { http: [rpcMap[chainId] || args.rpcUrl || 'http://localhost:8545'] },
            },
        };
        const walletClient = createWalletClient({
            account: account,
            chain,
            transport: custom(provider),
        });
        return { walletClient, address: account, chainId };
    }
    async tryReconnect(args) {
        try {
            const EthereumProvider = await this.getEthereumProviderModule();
            const rpcMap = { ...this.config.rpcUrls };
            if (args.expectedChainId && args.rpcUrl)
                rpcMap[args.expectedChainId] = args.rpcUrl;
            const chains = args.expectedChainId ? [args.expectedChainId] : this.config.chains || [1];
            const provider = await EthereumProvider.init({
                projectId: this.config.projectId,
                chains,
                optionalChains: this.config.optionalChains,
                showQrModal: false,
                metadata: this.config.metadata,
                rpcMap,
            });
            if (!provider.session)
                return null;
            this.provider = provider;
            const accounts = (await provider.request({ method: 'eth_accounts' }));
            if (!accounts || accounts.length === 0)
                return null;
            const account = accounts[0];
            const chainIdHex = (await provider.request({ method: 'eth_chainId' }));
            const chainId = parseInt(chainIdHex, 16);
            const chain = {
                id: chainId,
                name: 'Network',
                nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
                rpcUrls: {
                    default: { http: [rpcMap[chainId] || args.rpcUrl || 'http://localhost:8545'] },
                    public: { http: [rpcMap[chainId] || args.rpcUrl || 'http://localhost:8545'] },
                },
            };
            const walletClient = createWalletClient({
                account: account,
                chain,
                transport: custom(provider),
            });
            return { walletClient, address: account, chainId };
        }
        catch {
            return null;
        }
    }
    async disconnect() {
        if (this.provider) {
            try {
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                await this.provider.disconnect?.();
            }
            catch { /* ignore */ }
            this.provider = null;
        }
    }
}
//# sourceMappingURL=WalletConnectAdapter.js.map