/**
 * Trustless bridge “L2” side: Chain 138 (mainnet) or Chain 2138 (testnet).
 * Set VITE_TRUSTLESS_L2_CHAIN_ID=2138 and deploy addresses via VITE_LOCKBOX_2138, etc.
 * When using 2138, enable VITE_ENABLE_CHAIN2138 so Wagmi lists the chain.
 */

import type { Chain } from 'viem'
import { TRUSTLESS } from './bridge'
import { chain138, chain2138Testnet, chain2138TestnetEnabled } from './networks'

const raw = import.meta.env.VITE_TRUSTLESS_L2_CHAIN_ID
const parsed = raw === undefined || raw === '' ? 138 : Number(raw)
export const TRUSTLESS_L2_CHAIN_ID: 138 | 2138 = parsed === 2138 ? 2138 : 138

export type TrustlessL2LockboxConfig = {
  chainId: 138 | 2138
  viemChain: Chain
  lockbox: `0x${string}`
  weth: `0x${string}`
  cusdt: `0x${string}`
  cusdc: `0x${string}`
}

/** Resolves L2 chain + lockbox token addresses for TrustlessBridgeForm. */
export function getTrustlessL2LockboxConfig(): TrustlessL2LockboxConfig {
  if (TRUSTLESS_L2_CHAIN_ID === 2138) {
    if (!chain2138TestnetEnabled) {
      return {
        chainId: 138,
        viemChain: chain138,
        lockbox: TRUSTLESS.chain138.LOCKBOX_138,
        weth: TRUSTLESS.chain138.WETH,
        cusdt: TRUSTLESS.chain138.CUSDT,
        cusdc: TRUSTLESS.chain138.CUSDC,
      }
    }
    const c = TRUSTLESS.chain2138
    return {
      chainId: 2138,
      viemChain: chain2138Testnet,
      lockbox: c.LOCKBOX_2138,
      weth: c.WETH,
      cusdt: c.CUSDT,
      cusdc: c.CUSDC,
    }
  }
  return {
    chainId: 138,
    viemChain: chain138,
    lockbox: TRUSTLESS.chain138.LOCKBOX_138,
    weth: TRUSTLESS.chain138.WETH,
    cusdt: TRUSTLESS.chain138.CUSDT,
    cusdc: TRUSTLESS.chain138.CUSDC,
  }
}
