'use client'
'use strict';

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

var _tslib = require('../../../../../../../../_virtual/_tslib.cjs');
var React = require('react');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('../../../../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../../../../config/ApiEndpoint.cjs');
require('../../../../../../utils/constants/values.cjs');
require('@dynamic-labs/utils');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../../../utils/constants/colors.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../../../../shared/consts/index.cjs');
require('../../../../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs/locale');
require('../../../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../../../../events/dynamicEvents.cjs');
var useOnboardingCompleteUser = require('../../../../../../client/extension/user/useOnboardingCompleteUser/useOnboardingCompleteUser.cjs');
var useProjectSettings = require('../../../../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
var OnrampContext = require('../../../../../../context/OnrampContext/OnrampContext.cjs');
var authMode = require('../../../../../../store/state/authMode/authMode.cjs');
require('../../../../../../context/DynamicContext/DynamicContext.cjs');
require('../../../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../../../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../../../../context/ErrorContext/ErrorContext.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');
require('../../../../../../context/ThemeContext/ThemeContext.cjs');
require('../../../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
var useSocialAccounts = require('../../../../../../utils/hooks/useSocialAccounts/useSocialAccounts.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');
require('../../../../../../store/state/walletOptions/walletOptions.cjs');
require('react-i18next');
require('../../../../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('../../../../../../components/Alert/Alert.cjs');
require('../../../../../../context/WalletContext/WalletContext.cjs');
require('../../../../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../../../../components/IconButton/IconButton.cjs');
require('../../../../../../components/InlineWidget/InlineWidget.cjs');
require('../../../../../../components/Input/Input.cjs');
require('../../../../../../components/IsBrowser/IsBrowser.cjs');
require('../../../../../../components/MenuList/Dropdown/Dropdown.cjs');
require('../../../../../../components/OverlayCard/OverlayCard.cjs');
require('../../../../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../../../../components/Popper/Popper/Popper.cjs');
require('../../../../../../components/Popper/PopperContext/PopperContext.cjs');
require('react-focus-lock');
require('qrcode');
require('formik');
require('../../../../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../../../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../../../../../../context/LoadingContext/LoadingContext.cjs');
require('../../../../context/DynamicWidgetContext.cjs');
require('../../../../helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../../../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../../../../context/FooterAnimationContext/index.cjs');
require('@dynamic-labs/types');
require('../../../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('../../../../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../../../../store/state/sendBalances.cjs');
require('../../../../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../../components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../../../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../../components/PasskeyCard/PasskeyCard.cjs');
require('../../../CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../../../../../index.cjs');
require('../../../ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../../../../../store/state/tokenBalances.cjs');
require('../../../../../../store/state/multichainBalances.cjs');
require('../../../../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
var useConnectExchangeForFunding = require('../../../../../../utils/hooks/useConnectExchangeForFunding/useConnectExchangeForFunding.cjs');
var usePromptAndFundWithExchange = require('../../../../../../utils/hooks/usePromptAndFundWithExchange/usePromptAndFundWithExchange.cjs');

const useStartExchangeFunding = () => {
    const connectExchangeForFunding = useConnectExchangeForFunding.useConnectExchangeForFunding();
    const user = useOnboardingCompleteUser.useOnboardingCompleteUser();
    const { linkSocialAccount } = useSocialAccounts.useSocialAccounts();
    const promptAmountAndFundWithExchange = usePromptAndFundWithExchange.usePromptAmountAndFundWithExchange();
    const { openOnramp } = OnrampContext.useOnrampContext();
    const projectSettings = useProjectSettings.useProjectSettings();
    const startExchangeFunding = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c, _d;
        const authMode$1 = authMode.getAuthMode();
        try {
            let exchange;
            if ((projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges) &&
                ((_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges) === null || _a === void 0 ? void 0 : _a.length) > 1) {
                exchange = yield connectExchangeForFunding({
                    initiatedByWidget: true,
                });
            }
            else if (projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges) {
                exchange = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges[0].exchange;
            }
            else {
                return;
            }
            const fullExchange = projectSettings.exchanges.find((e) => e.exchange === exchange);
            const provider = fullExchange === null || fullExchange === void 0 ? void 0 : fullExchange.socialProvider;
            const connectedSocial = (_c = (_b = user === null || user === void 0 ? void 0 : user.verifiedCredentials) === null || _b === void 0 ? void 0 : _b.some(({ oauthProvider }) => oauthProvider === provider)) !== null && _c !== void 0 ? _c : false;
            const connectThoughSocial = provider &&
                (projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers) &&
                (((_d = projectSettings.providers.find((p) => p.provider === provider)) === null || _d === void 0 ? void 0 : _d.enabledAt) ||
                    connectedSocial);
            if (connectThoughSocial) {
                if (!connectedSocial) {
                    yield linkSocialAccount(provider, {
                        triggerFundFromExchangeOnSuccess: exchange,
                    });
                }
                else {
                    promptAmountAndFundWithExchange({ exchange });
                }
            }
            else if (fullExchange === null || fullExchange === void 0 ? void 0 : fullExchange.onRampProvider) {
                yield openOnramp({
                    onrampProvider: fullExchange === null || fullExchange === void 0 ? void 0 : fullExchange.onRampProvider,
                    overrideOnRamp: true,
                });
            }
        }
        catch (error) {
            logger.logger.error('Error while attempting to connect exchange for funding.', {
                authMode: authMode$1,
                error,
                userId: user === null || user === void 0 ? void 0 : user.id,
            });
        }
    }), [
        projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.exchanges,
        projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers,
        connectExchangeForFunding,
        user === null || user === void 0 ? void 0 : user.verifiedCredentials,
        user === null || user === void 0 ? void 0 : user.id,
        linkSocialAccount,
        promptAmountAndFundWithExchange,
        openOnramp,
    ]);
    return {
        startExchangeFunding,
    };
};

exports.useStartExchangeFunding = useStartExchangeFunding;
