'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactI18next = require('react-i18next');
var iconic = require('@dynamic-labs/iconic');
var walletBook = require('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
var addThick = require('../../../../../shared/assets/add-thick.cjs');
var globe = require('../../../../../shared/assets/globe.cjs');
var linkCircle = require('../../../../../shared/assets/link-circle.cjs');
var qrCode = require('../../../../../shared/assets/qr-code.cjs');
var walletOutlineV2 = require('../../../../../shared/assets/wallet-outline-v2.cjs');
require('../../../../../context/ViewContext/ViewContext.cjs');
require('../../../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs/wallet-connector-core');
require('../../../../../shared/logger.cjs');
require('../../../../../utils/constants/colors.cjs');
require('../../../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../../../shared/consts/index.cjs');
require('../../../../../components/Alert/Alert.cjs');
require('../../../../../events/dynamicEvents.cjs');
require('../../../../../context/DynamicContext/DynamicContext.cjs');
require('../../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../../../../store/state/authMode/authMode.cjs');
var useInternalDynamicContext = require('../../../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
require('../../../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../../../context/ErrorContext/ErrorContext.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-sdk/client/core');
require('../../../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../../../config/ApiEndpoint.cjs');
var useProjectSettings = require('../../../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
require('@dynamic-labs/locale');
require('../../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../../../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../../../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../../../utils/functions/compareChains/compareChains.cjs');
require('../../../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
var useIconThemeVariant = require('../../../../../utils/hooks/useIconThemeVariant/useIconThemeVariant.cjs');
require('../../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../../../context/LoadingContext/LoadingContext.cjs');
require('../../../../../context/WalletContext/WalletContext.cjs');
require('../../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../../../../context/MockContext/MockContext.cjs');
require('../../../../../views/CollectUserDataView/useFields.cjs');
require('../../../../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../../../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
var walletOptions = require('../../../../../store/state/walletOptions/walletOptions.cjs');
require('../../../../../context/FooterAnimationContext/index.cjs');
require('../../../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('react-focus-lock');
require('../../../../../context/ThemeContext/ThemeContext.cjs');
var Icon = require('../../../../../components/Icon/Icon.cjs');
var DynamicWidgetContext = require('../../../context/DynamicWidgetContext.cjs');
require('../../../../../components/IconButton/IconButton.cjs');
require('../../../../../components/MenuList/Dropdown/Dropdown.cjs');
require('formik');
require('../../../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../../../../store/state/sendBalances.cjs');
require('../../../../../components/Input/Input.cjs');
require('../../../../../components/OverlayCard/OverlayCard.cjs');
require('../../../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../components/PasskeyCard/PasskeyCard.cjs');
require('../../CryptoComOnramp/CryptoComOnramp.cjs');
var OnrampContext = require('../../../../../context/OnrampContext/OnrampContext.cjs');
var index = require('../../../../../utils/functions/walletFilters/index.cjs');
var useFundWithWallet = require('../../../../../utils/hooks/useFundWithWallet/useFundWithWallet.cjs');
var useWalletList = require('../../../../../utils/hooks/useWalletList/useWalletList.cjs');
var convertExchangeKeyAndProviderEnum = require('../../../helpers/convertExchangeKeyAndProviderEnum.cjs');
var useStartExchangeFunding = require('./useStartExchangeFunding/useStartExchangeFunding.cjs');
var useStartExternalWalletFunding = require('./useStartExternalWalletFunding/useStartExternalWalletFunding.cjs');
var useStartLinkedWalletFunding = require('./useStartLinkedWalletFunding/useStartLinkedWalletFunding.cjs');
require('qrcode');
require('../../ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../../../../../index.cjs');
require('../../../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../../../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../../../store/state/tokenBalances.cjs');
require('../../../../../store/state/multichainBalances.cjs');
require('../../../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
require('../../../../../components/InlineWidget/InlineWidget.cjs');
require('../../../../../components/IsBrowser/IsBrowser.cjs');
require('../../../../../components/Popper/Popper/Popper.cjs');
require('../../../../../components/Popper/PopperContext/PopperContext.cjs');

const slicePreviewIcons = (icons) => {
    if (icons.length > MAX_PREVIEW_ICONS)
        return icons.slice(0, MAX_PREVIEW_ICONS).concat(jsxRuntime.jsx("div", { className: 'choose-wallet-funding-method__options__option__trailing__preview-icons__plus-icon', children: jsxRuntime.jsx(Icon.Icon, { color: 'text-tertiary', children: jsxRuntime.jsx(addThick.ReactComponent, {}) }) }, 'plus-icon'));
    return icons;
};
const MAX_PREVIEW_ICONS = 3;
const useFundingMethods = () => {
    var _a;
    const { t } = reactI18next.useTranslation();
    const { setDynamicWidgetView } = DynamicWidgetContext.useWidgetContext();
    const { externalWalletFundingEnabled } = useFundWithWallet.useInternalFundWithWallet();
    const { enabledOnrampProviders, selectOnrampProvider } = OnrampContext.useOnrampContext();
    const iconThemeVariant = useIconThemeVariant.useIconThemeVariant();
    const { primaryWallet } = useInternalDynamicContext.useInternalDynamicContext();
    const projectSettings = useProjectSettings.useProjectSettings();
    const exchangeNames = React.useMemo(() => {
        if (!(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges))
            return [];
        const names = projectSettings.exchanges.flatMap(({ exchange, enabledAt }) => {
            if (!enabledAt)
                return [];
            return [convertExchangeKeyAndProviderEnum.convertExchangeKeytoProviderEnum(exchange)];
        });
        return [...new Set(names)];
    }, [projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges]);
    const viewWalletsFilter = React.useMemo(() => (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.chain)
        ? index.FilterChain(primaryWallet.chain)
        : undefined, [primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.chain]);
    const { walletsList } = useWalletList.useWalletList({
        viewWalletsFilter,
    });
    const walletGroups = walletOptions.useWalletGroups();
    const { startExternalWalletFunding } = useStartExternalWalletFunding.useStartExternalWalletFunding();
    const { startExchangeFunding } = useStartExchangeFunding.useStartExchangeFunding();
    const { startLinkedWalletFunding, filteredLinkedWallets } = useStartLinkedWalletFunding.useStartLinkedWalletFunding();
    const onrampOptions = React.useMemo(() => enabledOnrampProviders.filter(({ isPaymentMethod }) => !isPaymentMethod), [enabledOnrampProviders]);
    const paymentMethods = React.useMemo(() => enabledOnrampProviders.filter(({ isPaymentMethod }) => isPaymentMethod), [enabledOnrampProviders]);
    const handleQrClick = React.useCallback(() => setDynamicWidgetView('deposit-view'), [setDynamicWidgetView]);
    const handleBuyCryptoClick = React.useCallback(() => {
        if (!primaryWallet) {
            return;
        }
        onrampOptions.length > 1
            ? setDynamicWidgetView('choose-onramp-provider')
            : selectOnrampProvider({
                provider: onrampOptions[0],
                wallet: primaryWallet,
            });
    }, [
        onrampOptions,
        setDynamicWidgetView,
        primaryWallet,
        selectOnrampProvider,
    ]);
    const previewWalletIcons = React.useMemo(() => {
        if (!externalWalletFundingEnabled)
            return [];
        return slicePreviewIcons(walletsList.map(({ key, walletConnector }, index) => {
            var _a, _b, _c;
            return (jsxRuntime.jsx("div", { className: 'choose-wallet-funding-method__options__option__trailing__preview-icons__wallet-icon', style: { '--z-index': MAX_PREVIEW_ICONS - index }, children: jsxRuntime.jsx(walletBook.WalletIcon, { icon: (_a = walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.metadata.icon) !== null && _a !== void 0 ? _a : (_c = (_b = walletGroups[key]) === null || _b === void 0 ? void 0 : _b.brand) === null || _c === void 0 ? void 0 : _c.icon, walletKey: key, isGroup: false }) }, key));
        }));
    }, [externalWalletFundingEnabled, walletsList, walletGroups]);
    const exchangeIcons = React.useMemo(() => {
        const icons = exchangeNames.map((providerKey, index) => (jsxRuntime.jsx("div", { className: 'choose-wallet-funding-method__options__option__trailing__preview-icons__wallet-icon', style: { '--z-index': MAX_PREVIEW_ICONS - index }, children: jsxRuntime.jsx(iconic.SocialIcon, { name: providerKey, variant: iconThemeVariant }) }, providerKey)));
        return slicePreviewIcons(icons);
    }, [exchangeNames, iconThemeVariant]);
    const previewOnrampIcons = React.useMemo(() => {
        if (!onrampOptions.length)
            return [];
        return slicePreviewIcons(onrampOptions.map(({ iconUrl, id }) => (jsxRuntime.jsx("div", { className: 'choose-wallet-funding-method__options__option__trailing__preview-icons__onramp-icon', children: jsxRuntime.jsx("img", { src: iconUrl, alt: `${id}-icon` }) }, id))));
    }, [onrampOptions]);
    return React.useMemo(() => {
        var _a;
        const methods = [
            {
                icon: jsxRuntime.jsx(qrCode.ReactComponent, {}),
                key: 'qr',
                label: t('dyn_wallet_funding.funding_method_selection.qr_option'),
                onClick: handleQrClick,
                showChevron: true,
            },
        ];
        if (primaryWallet) {
            paymentMethods.forEach((option) => {
                const { id, iconUrl, displayName } = option;
                methods.push({
                    icon: jsxRuntime.jsx("img", { src: iconUrl, alt: displayName }),
                    key: id,
                    label: displayName,
                    onClick: () => selectOnrampProvider({
                        provider: option,
                        wallet: primaryWallet,
                    }),
                    showChevron: false,
                });
            });
        }
        if (onrampOptions.length) {
            methods.push({
                icon: jsxRuntime.jsx(globe.ReactComponent, {}),
                key: 'buy',
                label: t('dyn_wallet_funding.funding_method_selection.buy_option'),
                onClick: handleBuyCryptoClick,
                previewIcons: previewOnrampIcons,
                showChevron: onrampOptions.length > 1,
            });
        }
        if (filteredLinkedWallets.length > 0) {
            methods.push({
                icon: jsxRuntime.jsx(linkCircle.ReactComponent, {}),
                key: 'linked-wallet',
                label: t('dyn_wallet_funding.funding_method_selection.linked_wallet_option'),
                onClick: startLinkedWalletFunding,
                showChevron: true,
            });
        }
        if (exchangeNames.length > 0 &&
            ((_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.featureFlags) === null || _a === void 0 ? void 0 : _a.enableExchanges)) {
            methods.push({
                icon: jsxRuntime.jsx(globe.ReactComponent, {}),
                key: 'exchange',
                label: t('dyn_wallet_funding.funding_method_selection.exchange_option'),
                onClick: startExchangeFunding,
                previewIcons: exchangeIcons,
                showChevron: true,
            });
        }
        if (externalWalletFundingEnabled) {
            methods.push({
                icon: jsxRuntime.jsx(walletOutlineV2.ReactComponent, {}),
                key: 'external-wallet',
                label: t('dyn_wallet_funding.funding_method_selection.external_wallet_option'),
                onClick: startExternalWalletFunding,
                previewIcons: previewWalletIcons,
                showChevron: true,
            });
        }
        return methods;
    }, [
        t,
        handleQrClick,
        primaryWallet,
        onrampOptions.length,
        filteredLinkedWallets.length,
        exchangeNames.length,
        (_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.featureFlags) === null || _a === void 0 ? void 0 : _a.enableExchanges,
        externalWalletFundingEnabled,
        paymentMethods,
        selectOnrampProvider,
        handleBuyCryptoClick,
        previewOnrampIcons,
        startLinkedWalletFunding,
        startExchangeFunding,
        exchangeIcons,
        startExternalWalletFunding,
        previewWalletIcons,
    ]);
};

exports.useFundingMethods = useFundingMethods;
