'use client'
import { useMemo } from 'react';

const useNetworkConfigurationsFromProjectSettings = ({ projectSettings, cosmosNetworkOverrides, evmNetworksOverrides, solanaNetworksOverrides, tronNetworksOverrides, aptosNetworksOverrides, }) => useMemo(() => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const networks = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.networks;
    if (!networks)
        return {};
    const evm = networks.find((configurations) => configurations.chainName === 'evm');
    const solana = networks.find((configurations) => configurations.chainName === 'solana');
    const cosmos = networks.find((configurations) => configurations.chainName === 'cosmos');
    const starknet = networks.find((configurations) => configurations.chainName === 'starknet');
    const eclipse = networks.find((configurations) => configurations.chainName === 'eclipse');
    const sui = networks.find((configurations) => configurations.chainName === 'sui');
    const tron = networks.find((configurations) => configurations.chainName === 'tron');
    const aptos = networks.find((configurations) => configurations.chainName === 'aptos');
    let cosmosNetworks = (_a = cosmos === null || cosmos === void 0 ? void 0 : cosmos.networks) === null || _a === void 0 ? void 0 : _a.map((net) => createNetwork(net, Number.parseInt));
    cosmosNetworks = overrideNetworks(cosmosNetworkOverrides, cosmosNetworks);
    let evmNetworks = (_b = evm === null || evm === void 0 ? void 0 : evm.networks) === null || _b === void 0 ? void 0 : _b.map((net) => createNetwork(net, Number.parseInt));
    evmNetworks = overrideNetworks(evmNetworksOverrides, evmNetworks);
    const starknetNetworks = (_c = starknet === null || starknet === void 0 ? void 0 : starknet.networks) === null || _c === void 0 ? void 0 : _c.map((net) => createNetwork(net));
    let solanaNetworks = (_d = solana === null || solana === void 0 ? void 0 : solana.networks) === null || _d === void 0 ? void 0 : _d.map((net) => createNetwork(net));
    solanaNetworks = overrideNetworks(solanaNetworksOverrides, solanaNetworks);
    const eclipseNetworks = (_e = eclipse === null || eclipse === void 0 ? void 0 : eclipse.networks) === null || _e === void 0 ? void 0 : _e.map((net) => createNetwork(net));
    if (evmNetworksOverrides) {
        if (Array.isArray(evmNetworksOverrides)) {
            evmNetworks = evmNetworksOverrides;
        }
        else {
            evmNetworks = evmNetworksOverrides(evmNetworks !== null && evmNetworks !== void 0 ? evmNetworks : []);
        }
    }
    const suiNetworks = (_f = sui === null || sui === void 0 ? void 0 : sui.networks) === null || _f === void 0 ? void 0 : _f.map((net) => createNetwork(net));
    let tronNetworks = (_g = tron === null || tron === void 0 ? void 0 : tron.networks) === null || _g === void 0 ? void 0 : _g.map((net) => createNetwork(net));
    tronNetworks = overrideNetworks(tronNetworksOverrides, tronNetworks);
    let aptosNetworks = (_h = aptos === null || aptos === void 0 ? void 0 : aptos.networks) === null || _h === void 0 ? void 0 : _h.map((net) => createNetwork(net));
    aptosNetworks = overrideNetworks(aptosNetworksOverrides, aptosNetworks);
    const networkConfigurations = {
        aptos: aptosNetworks,
        cosmos: cosmosNetworks,
        eclipse: eclipseNetworks,
        evm: evmNetworks,
        solana: solanaNetworks,
        starknet: starknetNetworks,
        sui: suiNetworks,
        tron: tronNetworks,
    };
    return networkConfigurations;
}, [
    projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.networks,
    cosmosNetworkOverrides,
    evmNetworksOverrides,
    solanaNetworksOverrides,
    tronNetworksOverrides,
    aptosNetworksOverrides,
]);
const createNetwork = (network, parser = (input) => input) => {
    const result = {
        bech32Prefix: network.bech32Prefix,
        blockExplorerUrls: network.blockExplorerUrls,
        chainId: parser(network.chainId),
        cluster: network.cluster,
        genesisHash: network.genesisHash,
        iconUrls: network.iconUrls,
        isTestnet: network.isTestnet,
        key: network.key,
        lcdUrl: network.lcdUrl,
        name: network.name,
        nativeCurrency: network.nativeCurrency,
        networkId: parser(network.networkId),
        privateCustomerRpcUrls: network.privateCustomerRpcUrls,
        rpcUrls: network.rpcUrls,
        vanityName: network.vanityName,
    };
    return result;
};
const overrideNetworks = (networkOverrides, networks) => {
    if (!networkOverrides) {
        return networks;
    }
    if (Array.isArray(networkOverrides)) {
        return networkOverrides;
    }
    else {
        return networkOverrides(networks !== null && networks !== void 0 ? networks : []);
    }
};

export { useNetworkConfigurationsFromProjectSettings };
