/**
 * Token Address Mapping
 *
 * Provides mapping between token symbols and their Ethereum mainnet contract addresses
 * Extracted from tokenList.json chainId: 1
 */

import { ChainType } from '../../store/models';
import tokenAddresses from './token-addresses.json';

export interface TokenInfo {
  address: string;
  symbol: string;
  name: string;
  decimals: number;
  coinGeckoId?: string;
}

// Token address mapping for Ethereum mainnet (chainId: 1)
// This is generated from tokenList.json via scripts/generate-token-mapping.js
export const TOKEN_ADDRESS_MAP: Record<string, TokenInfo> = tokenAddresses as any;

/**
 * Get token info by symbol
 */
export function getTokenInfo(symbol: string): TokenInfo | undefined {
  return TOKEN_ADDRESS_MAP[symbol.toUpperCase()];
}

/**
 * Get token address by symbol
 */
export function getTokenAddress(symbol: string): string | undefined {
  return TOKEN_ADDRESS_MAP[symbol.toUpperCase()]?.address;
}

/**
 * Get CoinGecko ID by symbol
 */
export function getCoinGeckoId(symbol: string): string | undefined {
  return TOKEN_ADDRESS_MAP[symbol.toUpperCase()]?.coinGeckoId;
}

/**
 * Check if a symbol has address mapping
 */
export function hasTokenAddress(symbol: string, methodType?: ChainType): boolean {
  if (methodType === 'arcblock') {
    return !!TOKEN_ADDRESS_MAP.ABT;
  }
  return !!TOKEN_ADDRESS_MAP[symbol.toUpperCase()];
}

/**
 * Get the actual symbol to use for exchange rate calculation
 * For ArcBlock payment method, always use ABT regardless of the currency symbol
 * For other payment methods, use the original symbol
 */
export function getExchangeRateSymbol(symbol: string, methodType?: ChainType): string {
  if (methodType === 'arcblock') {
    return 'ABT';
  }
  return symbol.toUpperCase();
}

/**
 * Get token info for exchange rate calculation
 * For ArcBlock payment method, always return ABT token info
 * For other payment methods, return the token info for the given symbol
 */
export function getTokenInfoForRate(
  symbol: string,
  methodType?: 'arcblock' | 'ethereum' | 'base' | 'stripe'
): TokenInfo | undefined {
  const rateSymbol = getExchangeRateSymbol(symbol, methodType);
  return TOKEN_ADDRESS_MAP[rateSymbol];
}

/**
 * Get all supported symbols
 */
export function getSupportedSymbols(): string[] {
  return Object.keys(TOKEN_ADDRESS_MAP);
}
