/**
 * Coinbase Wallet Adapter
 *
 * Enables connection to Coinbase Wallet via:
 * - Coinbase Wallet browser extension
 * - Coinbase Wallet mobile app (via QR code / deep link)
 * - Coinbase Smart Wallet
 */
import { createWalletClient, custom } from 'viem';
export class CoinbaseWalletAdapter {
    id = 'coinbase';
    config;
    provider = null;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    sdk = null;
    constructor(config) {
        this.config = {
            defaultChainId: 1,
            smartWalletPreference: 'all',
            ...config,
        };
    }
    async initSDK(_chainId, _rpcUrl) {
        if (!this.sdk) {
            // @ts-expect-error -- optional peer dependency, dynamically imported
            const { CoinbaseWalletSDK } = await import('@coinbase/wallet-sdk');
            this.sdk = new CoinbaseWalletSDK({
                appName: this.config.appName,
                appLogoUrl: this.config.appLogoUrl,
            });
        }
        const provider = this.sdk.makeWeb3Provider({
            options: this.config.smartWalletPreference || 'all',
        });
        return provider;
    }
    async connect(args) {
        const chainId = args.expectedChainId || this.config.defaultChainId || 1;
        const rpcUrl = args.rpcUrl || this.config.jsonRpcUrl || 'http://localhost:8545';
        const provider = await this.initSDK(chainId, rpcUrl);
        this.provider = provider;
        const accounts = (await provider.request({ method: 'eth_requestAccounts' }));
        if (!accounts || accounts.length === 0)
            throw new Error('No accounts available from Coinbase Wallet');
        const account = accounts[0];
        const chainIdHex = (await provider.request({ method: 'eth_chainId' }));
        const connectedChainId = parseInt(chainIdHex, 16);
        const chain = {
            id: connectedChainId,
            name: 'Network',
            nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
            rpcUrls: { default: { http: [rpcUrl] }, public: { http: [rpcUrl] } },
        };
        const walletClient = createWalletClient({
            account: account,
            chain,
            transport: custom(provider),
        });
        return { walletClient, address: account, chainId: connectedChainId };
    }
    async tryReconnect(args) {
        try {
            const chainId = args.expectedChainId || this.config.defaultChainId || 1;
            const rpcUrl = args.rpcUrl || this.config.jsonRpcUrl || 'http://localhost:8545';
            const provider = await this.initSDK(chainId, rpcUrl);
            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 connectedChainId = parseInt(chainIdHex, 16);
            const chain = {
                id: connectedChainId,
                name: 'Network',
                nativeCurrency: { name: 'ETH', symbol: 'ETH', decimals: 18 },
                rpcUrls: { default: { http: [rpcUrl] }, public: { http: [rpcUrl] } },
            };
            const walletClient = createWalletClient({
                account: account,
                chain,
                transport: custom(provider),
            });
            return { walletClient, address: account, chainId: connectedChainId };
        }
        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;
        }
        this.sdk = null;
    }
}
//# sourceMappingURL=CoinbaseWalletAdapter.js.map