'use client'
import { __rest } from '../../../../../_virtual/_tslib.js';
import { useMemo, useEffect } from 'react';
import { isWalletBookPopulated } from '@dynamic-labs/wallet-book';
import { getWallets } from '../../../views/WalletList/data.js';
import '@dynamic-labs/sdk-api-core';
import '../../constants/values.js';
import '@dynamic-labs/utils';
import '@dynamic-labs/multi-wallet';
import '../../../shared/logger.js';
import 'react-international-phone';
import '../../constants/colors.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '../../../shared/consts/index.js';
import '../../../store/state/nonce/nonce.js';
import { serializeWalletConnectors } from '../../functions/serializeWalletConnectors/serializeWalletConnectors.js';
import '@dynamic-labs-sdk/client/core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '../../../events/dynamicEvents.js';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import { mergeWalletConnectorOptions } from '../../../store/state/walletOptions/walletOptions.js';
import { useRedirectUrl } from '../../../store/utils/settingsUtils/settingsUtils.js';
import { verifyRequiredConnectorsAreSetup } from './utils/verifyRequiredConnectorsAreSetup/verifyRequiredConnectorsAreSetup.js';

const useCreateWalletConnectorOptions = (_a) => {
    var { projectSettings, networkConfigurations, walletBook, walletConnectorsProp, walletConnectorExtensions } = _a, props = __rest(_a, ["projectSettings", "networkConfigurations", "walletBook", "walletConnectorsProp", "walletConnectorExtensions"]);
    const redirectUrl = useRedirectUrl();
    const walletConnectorsKey = useMemo(() => { var _a; return (_a = serializeWalletConnectors(walletConnectorsProp)) === null || _a === void 0 ? void 0 : _a.join(','); }, [walletConnectorsProp]);
    const isWalletBookLoaded = useMemo(() => isWalletBookPopulated(walletBook), [walletBook]);
    const walletConnectorOptions = useMemo(() => {
        if (!projectSettings || !isWalletBookLoaded || !networkConfigurations)
            return [];
        return getWallets({
            getSupportedWalletOpts: Object.assign({ networkConfigurations,
                redirectUrl, settings: projectSettings, skipMemo: true, walletBook, walletConnectorsProp: walletConnectorsProp !== null && walletConnectorsProp !== void 0 ? walletConnectorsProp : [] }, props),
            walletConnectorExtensions,
        });
        // Cannot include walletUiUtils in the dependency array because it will cause an infinite loop
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        projectSettings,
        redirectUrl,
        walletConnectorsKey,
        isWalletBookLoaded,
        networkConfigurations,
    ]);
    useEffect(() => {
        if (!projectSettings || walletConnectorOptions.length === 0)
            return;
        verifyRequiredConnectorsAreSetup(projectSettings, walletConnectorOptions.map(({ walletConnector }) => walletConnector));
        mergeWalletConnectorOptions(walletConnectorOptions);
    }, [walletConnectorOptions, projectSettings]);
    return { walletConnectorOptions };
};

export { useCreateWalletConnectorOptions };
