import { getCanonicalTokenBySymbol } from './canonical-tokens';

const CHAIN_138 = 138;

/** Hub USD cash eMoney + official mirror stables used in Chain 138 PMM cash pools. */
const CHAIN138_CASH_USD_SYMBOLS = ['cUSDC', 'cUSDT', 'USDC', 'USDT'] as const;

function normalizeAddress(value: string | undefined): string | undefined {
  const normalized = String(value || '').trim().toLowerCase();
  return /^0x[a-f0-9]{40}$/.test(normalized) ? normalized : undefined;
}

/** Lowercase addresses for USD cash legs in canonical Stack A PMM pools. */
export function getChain138CashUsdTokenAddresses(): ReadonlySet<string> {
  const addresses = new Set<string>();
  for (const symbol of CHAIN138_CASH_USD_SYMBOLS) {
    const spec = getCanonicalTokenBySymbol(CHAIN_138, symbol);
    const address = normalizeAddress(spec?.addresses?.[CHAIN_138]);
    if (address) {
      addresses.add(address);
    }
  }
  return addresses;
}

export function isChain138CashUsdTokenAddress(address: string): boolean {
  return getChain138CashUsdTokenAddresses().has(address.trim().toLowerCase());
}
