"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNetworkConfigurationHook = void 0;
const useLeftAccounts_1 = require("../hooks/useLeftAccounts");
const useLeftAccountsApy_1 = require("../hooks/useLeftAccountsApy");
const useRightBalanceNetwork_1 = require("../hooks/useRightBalanceNetwork");
const sortByBalance_1 = require("../utils/sortByBalance");
const getLeftElement = (NetworkConfigurationDeps) => (leftElement) => {
    switch (leftElement) {
        case "undefined":
            return undefined;
        case "numberOfAccountsAndApy":
            return (params) => (0, useLeftAccountsApy_1.useLeftAccountsApyModule)(params, NetworkConfigurationDeps.useAccountData, NetworkConfigurationDeps.accountsCountAndApy, NetworkConfigurationDeps.accountsApy);
        case "numberOfAccounts":
        default:
            return (0, useLeftAccounts_1.createUseLeftAccountsModule)({
                useAccountData: NetworkConfigurationDeps.useAccountData,
                accountsCount: NetworkConfigurationDeps.accountsCount,
            });
    }
};
const getRightElement = (NetworkConfigurationDeps) => (rightElement) => {
    switch (rightElement) {
        case "undefined":
            return undefined;
        case "balance":
        default:
            return (params) => (0, useRightBalanceNetwork_1.createUseRightBalanceNetwork)({
                useBalanceDeps: NetworkConfigurationDeps.useBalanceDeps,
                balanceItem: NetworkConfigurationDeps.balanceItem,
            })({ networks: params.networks });
    }
};
const sortNetworks = (result, leftElement, rightElement) => {
    if (leftElement === "numberOfAccounts" ||
        leftElement === "numberOfAccountsAndApy" ||
        leftElement === undefined // default
    ) {
        result.sort((a, b) => (b?.count || 0) - (a?.count || 0));
    }
    if (rightElement === "balance" ||
        rightElement === undefined // default
    ) {
        result.sort((a, b) => (0, sortByBalance_1.compareByBalanceThenFiat)(a?.balanceData, b?.balanceData));
    }
};
const createNetworkConfigurationHook = (NetworkConfigurationDeps) => ({ networksConfig }) => {
    const { leftElement, rightElement } = networksConfig ?? {};
    const leftHook = getLeftElement(NetworkConfigurationDeps)(leftElement);
    const rightHook = getRightElement(NetworkConfigurationDeps)(rightElement);
    const hooks = [rightHook, leftHook].filter((hook) => Boolean(hook));
    return (networks) => {
        const hookResults = hooks.map(hook => hook({
            networks,
        }));
        const networksWithComponents = networks.map((network, index) => {
            const asset = network.type === "TokenCurrency" ? network.parentCurrency : network;
            const merged = { ...asset };
            for (const hookResult of hookResults) {
                if (hookResult[index]) {
                    Object.assign(merged, hookResult[index]);
                }
            }
            return merged;
        });
        sortNetworks(networksWithComponents, leftElement, rightElement);
        return networksWithComponents;
    };
};
exports.createNetworkConfigurationHook = createNetworkConfigurationHook;
//# sourceMappingURL=createNetworkConfiguration.js.map