import { createConfig, http } from 'wagmi'
import { metaMask, walletConnect, coinbaseWallet } from 'wagmi/connectors'
import {
  allNetworks,
  chainRpcUrls,
  chain138,
  allMainnet,
  etherlink,
  bsc,
  avalanche,
  cronos,
  gnosis,
} from './networks'

const projectId = import.meta.env.VITE_WALLETCONNECT_PROJECT_ID || ''

export const config = createConfig({
  chains: allNetworks,
  connectors: [
    metaMask(),
    walletConnect({ projectId }),
    coinbaseWallet({ appName: 'Bridge DApp' }),
  ],
  transports: {
    [chain138.id]: http(chainRpcUrls[chain138.id]),
    [allMainnet.id]: http(chainRpcUrls[allMainnet.id]),
    [etherlink.id]: http(chainRpcUrls[etherlink.id]),
    [bsc.id]: http(chainRpcUrls[bsc.id]),
    [avalanche.id]: http(chainRpcUrls[avalanche.id]),
    [cronos.id]: http(chainRpcUrls[cronos.id]),
    [gnosis.id]: http(chainRpcUrls[gnosis.id]),
    // Standard chains use default public RPC when no override
    ...Object.fromEntries(
      allNetworks
        .filter((c) => !(c.id in chainRpcUrls))
        .map((c) => [c.id, http()])
    ),
  },
})
