// Network definitions for supported Ethereum networks.
// Each network specifies its chain ID, default RPC and Blockscout endpoints,
// and the block explorer base URL used for address/tx/token/block links.

const NETWORKS = {
    mainnet: {
        id: "mainnet",
        name: "Ethereum Mainnet",
        chainId: "0x1",
        networkVersion: "1",
        nativeCurrency: "ETH",
        defaultRpcUrl: "https://ethereum-rpc.publicnode.com",
        defaultBlockscoutUrl: "https://eth.blockscout.com/api/v2",
        explorerUrl: "https://etherscan.io",
        isTestnet: false,
    },
    sepolia: {
        id: "sepolia",
        name: "Sepolia Testnet",
        chainId: "0xaa36a7",
        networkVersion: "11155111",
        nativeCurrency: "SepoliaETH",
        defaultRpcUrl: "https://ethereum-sepolia-rpc.publicnode.com",
        defaultBlockscoutUrl: "https://eth-sepolia.blockscout.com/api/v2",
        explorerUrl: "https://sepolia.etherscan.io",
        isTestnet: true,
    },
};

const SUPPORTED_CHAIN_IDS = new Set(
    Object.values(NETWORKS).map((n) => n.chainId),
);

function networkById(id) {
    return NETWORKS[id] || NETWORKS.mainnet;
}

function networkByChainId(chainId) {
    for (const net of Object.values(NETWORKS)) {
        if (net.chainId === chainId) return net;
    }
    return null;
}

// Build a block explorer link for the given path type and value.
// type: "address" | "tx" | "token" | "block"
function explorerLink(network, type, value) {
    return `${network.explorerUrl}/${type}/${value}`;
}

module.exports = {
    NETWORKS,
    SUPPORTED_CHAIN_IDS,
    networkById,
    networkByChainId,
    explorerLink,
};
