/** Browser/wallet-facing Chain 138 RPC FQDNs — public tier only (never core 192.168.11.211). */
export const CHAIN138_PUBLIC_RPC_URLS = [
  'https://rpc.public-0138.defi-oracle.io',
  'https://rpc-http-pub.d-bis.org',
  'https://rpc.defi-oracle.io',
] as const;

const DEFAULT_CHAIN138_PUBLIC_RPC_URL = CHAIN138_PUBLIC_RPC_URLS[0];
const DEFAULT_CHAIN138_SERVER_RPC_URL = 'http://192.168.11.235:8545';

/** HTTPS FQDN for wallets, MetaMask, and browser JSON-RPC (same on LAN and external). */
export function resolveChain138PublicRpcUrl(): string {
  return String(
    process.env.CHAIN138_RPC_HTTP_PUBLIC_FQDN ||
      process.env.RPC_URL_138_PUBLIC ||
      process.env.CHAIN138_RPC_URL_PUBLIC ||
      process.env.CHAIN_138_RPC_URL ||
      DEFAULT_CHAIN138_PUBLIC_RPC_URL
  ).trim();
}

export function resolveChain138PublicRpcUrls(): string[] {
  const fromEnv = process.env.CHAIN138_PUBLIC_RPC_URLS?.trim();
  if (fromEnv) {
    return fromEnv.split(/[\s,]+/).filter(Boolean);
  }
  return [...CHAIN138_PUBLIC_RPC_URLS];
}

/**
 * Server-side JSON-RPC (indexers, eth_call on VMID 5000/5011).
 * Uses LAN public Besu (2201 / 192.168.11.221) when set — still public tier, not core.
 */
export function resolveChain138RpcUrl(): string {
  const lan =
    process.env.CHAIN138_SERVER_RPC_URL?.trim() ||
    process.env.RPC_HTTP_PUB_URL?.trim() ||
    (process.env.RPC_URL_138_PUBLIC?.includes('192.168.11.') ? process.env.RPC_URL_138_PUBLIC : '') ||
    DEFAULT_CHAIN138_SERVER_RPC_URL;
  return lan;
}

export { DEFAULT_CHAIN138_PUBLIC_RPC_URL as DEFAULT_CHAIN138_RPC_URL };
