/**
 * Shared chain configuration and public client creation.
 *
 * Used by Auth and Vault resources so that chain resolution, RPC fallback,
 * and error handling are consistent across the SDK.
 */
import { createPublicClient, http, defineChain } from 'viem';
import { mainnet, optimism, optimismSepolia, soneiumMinato } from 'viem/chains';
import { ErrorCodes, PrivacyBoostError } from '../errors';
const localhost = defineChain({
    id: 31337,
    name: 'Localhost',
    nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
    rpcUrls: { default: { http: ['http://localhost:8545'] } },
});
/** Known chains keyed by chain ID. */
export const KNOWN_CHAINS = {
    1: mainnet,
    10: optimism,
    11155420: optimismSepolia,
    1946: soneiumMinato,
    31337: localhost,
};
/**
 * Create a viem PublicClient for the given chain.
 *
 * - For known chains, the chain's built-in default RPC is used when
 *   `rpcUrl` is omitted (`http(undefined)` falls back to the chain default).
 * - For unknown chains, `rpcUrl` is required; throws INVALID_CONFIG if missing.
 */
export function createChainClient(chainId, rpcUrl) {
    const knownChain = KNOWN_CHAINS[chainId];
    if (knownChain) {
        return createPublicClient({
            chain: knownChain,
            transport: http(rpcUrl),
        });
    }
    if (!rpcUrl) {
        throw new PrivacyBoostError(ErrorCodes.INVALID_CONFIG, `Unsupported chain ID: ${chainId}. Provide rpcUrl in config.`);
    }
    const customChain = defineChain({
        id: chainId,
        name: `Chain ${chainId}`,
        nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
        rpcUrls: { default: { http: [rpcUrl] } },
    });
    return createPublicClient({
        chain: customChain,
        transport: http(rpcUrl),
    });
}
//# sourceMappingURL=chains.js.map