import { createUseLeftAccountsModule } from "../hooks/useLeftAccounts";
import { useLeftAccountsApyModule } from "../hooks/useLeftAccountsApy";
import { createUseRightBalanceNetwork } from "../hooks/useRightBalanceNetwork";
import { compareByBalanceThenFiat } from "../utils/sortByBalance";
const getLeftElement = (NetworkConfigurationDeps) => (leftElement) => {
    switch (leftElement) {
        case "undefined":
            return undefined;
        case "numberOfAccountsAndApy":
            return (params) => useLeftAccountsApyModule(params, NetworkConfigurationDeps.useAccountData, NetworkConfigurationDeps.accountsCountAndApy, NetworkConfigurationDeps.accountsApy);
        case "numberOfAccounts":
        default:
            return createUseLeftAccountsModule({
                useAccountData: NetworkConfigurationDeps.useAccountData,
                accountsCount: NetworkConfigurationDeps.accountsCount,
            });
    }
};
const getRightElement = (NetworkConfigurationDeps) => (rightElement) => {
    switch (rightElement) {
        case "undefined":
            return undefined;
        case "balance":
        default:
            return (params) => 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) => compareByBalanceThenFiat(a?.balanceData, b?.balanceData));
    }
};
export 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;
    };
};
//# sourceMappingURL=createNetworkConfiguration.js.map