'use client'
import { pipe } from '@dynamic-labs/utils';
import '../../../context/DynamicContext/useDynamicContext/useDynamicContext.js';
import { useInternalDynamicContext } from '../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js';
import { useInternalUserWallets } from '../../../context/UserWalletsContext/UserWalletsContext.js';
import { useWalletGroups } from '../../../store/state/walletOptions/walletOptions.js';
import { FilterBridgeChainsName } from '../../functions/walletFilters/index.js';
import { walletListBuilder } from '../../functions/walletListBuilder/walletListBuilder.js';
import { getEffectiveRecommendedWallets } from './utils/getEffectiveRecommendedWallets/getEffectiveRecommendedWallets.js';
import { getEffectiveWalletFilter } from './utils/getEffectiveWalletFilter/getEffectiveWalletFilter.js';

const useWalletList = ({ isWalletConnectList = false, viewWalletsFilter, searchFilter, lastUsedWalletKey, }) => {
    const { walletsFilter, walletConnectorOptions, defaultNumberOfWalletsToShow, bridgeChainsToConnect, multiWallet, recommendedWallets, selectedTabSettings, } = useInternalDynamicContext();
    const { userWallets } = useInternalUserWallets();
    const walletGroups = useWalletGroups();
    const getFilteredBridgeChains = (bridgeChainsToConnect === null || bridgeChainsToConnect === void 0 ? void 0 : bridgeChainsToConnect.length) &&
        FilterBridgeChainsName(bridgeChainsToConnect[0].chain);
    const filteredBridgeChains = getFilteredBridgeChains && getFilteredBridgeChains(walletConnectorOptions);
    const effectiveRecommendedWallets = getEffectiveRecommendedWallets(selectedTabSettings, recommendedWallets);
    const effectiveWalletsFilter = getEffectiveWalletFilter(selectedTabSettings, walletsFilter);
    const { numberOfWallets, walletsList } = walletListBuilder({
        groupWallets: true,
        inputList: filteredBridgeChains || walletConnectorOptions,
        isWalletConnectList,
        lastUsedWalletKey,
        multiWallet,
        numberOfWalletsToShow: defaultNumberOfWalletsToShow,
        recommendedWallets: effectiveRecommendedWallets,
        searchFilter,
        showMoreWalletsWithFilter: true,
        userWallets,
        walletGroups,
        // use both filters if available, else use the viewWalletsFilter or effectiveWalletsFilter
        walletsFilter: viewWalletsFilter && effectiveWalletsFilter
            ? pipe(viewWalletsFilter).pipe(effectiveWalletsFilter)
            : viewWalletsFilter || effectiveWalletsFilter,
    });
    return {
        numberOfWallets,
        recommendedWallets: effectiveRecommendedWallets,
        walletsList,
    };
};

export { useWalletList };
