import { base, bsc, bscTestnet } from "viem/chains"
import { fallback, http } from "wagmi"
import { getDefaultConfig } from "@rainbow-me/rainbowkit"
import {
  coinbaseWallet,
  metaMaskWallet,
  rabbyWallet,
  rainbowWallet,
} from "@rainbow-me/rainbowkit/wallets"
import binanceWallet from "@binance/w3w-rainbow-connector-v2"

import { BASE_CHAIN_ID, swell, SWELL_CHAIN_ID } from "./constants"
import { getBaseUrl } from "./components/shared/actions"
import { STAGING_BASE_URL } from "./components/shared/constants"

const currentUrl = getBaseUrl()
const isStaging = currentUrl.includes(STAGING_BASE_URL) // More flexible check

const WALLETCONNECT_V2_PROJECT_ID = "b782b5d726e33549eacb0645ec01a94c"

const commonWallets = [
  rabbyWallet,
  metaMaskWallet,
  coinbaseWallet,
  rainbowWallet,
]

export const baseWagmiConfig = getDefaultConfig({
  appName: "RainbowKit App",
  projectId: WALLETCONNECT_V2_PROJECT_ID,
  chains: [base, ...(isStaging ? [bsc, bscTestnet] : [])], // Use Binance chains only on staging
  wallets: [
    {
      groupName: "Most Used",
      wallets: isStaging ? [...commonWallets, binanceWallet] : commonWallets, // Use Binance wallet only on staging
    },
  ],
  ssr: true,
  transports: {
    [BASE_CHAIN_ID]: fallback([
      http(
        "https://base-mainnet.g.alchemy.com/v2/q2VDjsGh2h0P6WZSXUq2eTw7y0_Ffkdq",
      ),
      ...base.rpcUrls.default.http.map((rpcUrl) => http(rpcUrl)),
    ]),
  },
})

export const swellWagmiConfig = getDefaultConfig({
  appName: "RainbowKit App",
  projectId: WALLETCONNECT_V2_PROJECT_ID,
  chains: [swell],
  wallets: [
    {
      groupName: "Most Used",
      wallets: commonWallets, // No Binance wallet in swell config
    },
  ],
  ssr: true,
  transports: {
    [SWELL_CHAIN_ID]: fallback([
      http(
        "https://rpc.ankr.com/swell/06bb58c1350581017986b4a9f26b0faed9200826c27e480717e831a395779e97",
      ),
      ...swell.rpcUrls.default.http.map((rpcUrl) => http(rpcUrl)),
    ]),
  },
})
