'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactI18next = require('react-i18next');
var sdkApiCore = require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs/utils');
require('../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('@dynamic-labs/iconic');
var ViewContext = require('../../../context/ViewContext/ViewContext.cjs');
require('@dynamic-labs/wallet-connector-core');
require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
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');
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('../../functions/compareChains/compareChains.cjs');
require('../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../context/ThemeContext/ThemeContext.cjs');
require('../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('../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');
require('../../../store/state/walletOptions/walletOptions.cjs');
var Typography = require('../../../components/Typography/Typography.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('../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('react-focus-lock');
var DynamicWidgetContext = require('../../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../../../components/IconButton/IconButton.cjs');
require('../../../components/MenuList/Dropdown/Dropdown.cjs');
require('formik');
require('../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('../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../../../../index.cjs');
require('../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('qrcode');
require('../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../widgets/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');
var useExchangeAccounts = require('../useExchangeAccounts/useExchangeAccounts.cjs');
require('../../../components/InlineWidget/InlineWidget.cjs');
require('../../../components/IsBrowser/IsBrowser.cjs');
require('../../../components/Popper/Popper/Popper.cjs');
require('../../../components/Popper/PopperContext/PopperContext.cjs');

const useSubmitExchangeFunding = () => {
    const { exchangeTransfer } = useExchangeAccounts.useExchangeAccounts();
    const { setDynamicWidgetView, goToInitialDynamicWidgetView, setHeaderAlert } = DynamicWidgetContext.useWidgetContext();
    const { clearStackAndPush } = ViewContext.useViewContext();
    const { setShowAuthFlow } = useInternalDynamicContext.useInternalDynamicContext();
    const { t } = reactI18next.useTranslation();
    const submit = React.useCallback((_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ tokenAmount, exchange, tokenSymbol, mfaCode, toAddress, props, }) {
        if (!tokenAmount)
            return;
        try {
            const result = yield exchangeTransfer({
                exchange,
                transferRequest: Object.assign({ amount: parseInt(tokenAmount, 10), currency: tokenSymbol, to: toAddress }, (mfaCode ? { mfaCode } : {})),
            });
            setShowAuthFlow(false);
            setDynamicWidgetView('deposited-exchange', {
                onClose: goToInitialDynamicWidgetView,
                onSubmit: goToInitialDynamicWidgetView,
                toAddress,
                transaction: result,
            });
        }
        catch (error) {
            let errorMessage = '';
            let showError = false;
            if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.TransferMfaRequired) {
                setShowAuthFlow(true);
                clearStackAndPush('mfa-exchange-view', {
                    onBack: () => {
                        setShowAuthFlow(false);
                        setDynamicWidgetView('confirm-exchange-transfer', Object.assign({}, props));
                    },
                    onSubmit: (code) => {
                        submit({
                            exchange,
                            mfaCode: code,
                            props,
                            toAddress,
                            tokenAmount,
                            tokenSymbol,
                        });
                    },
                });
            }
            else if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.TransferMfaFailed) {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.mfa_failed');
            }
            else if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.InvalidTransferCurrency) {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.invalid_currency_description');
            }
            else if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.InvalidTransferFunds) {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.invalid_balance');
            }
            else if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.TransferAmountTooSmall) {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.amount_too_small');
            }
            else if (error.code ===
                sdkApiCore.UnprocessableEntityErrorCode.TransferAddressNotWhitelisted) {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.address_not_whitelisted');
            }
            else {
                showError = true;
                errorMessage = t('dyn_exchange_transfer_errors.unknown');
            }
            if (showError) {
                const errorAlert = (jsxRuntime.jsxs(Typography.Typography, { variant: 'body_normal', color: 'current-color', copykey: 'dyn_exchange_transfer_errors.title', children: [t('dyn_exchange_transfer_errors.title'), jsxRuntime.jsx(Typography.Typography, { color: 'current-color', children: errorMessage })] }));
                setShowAuthFlow(false);
                goToInitialDynamicWidgetView();
                setHeaderAlert({
                    content: errorAlert,
                    variant: 'error',
                });
            }
        }
    }), [
        clearStackAndPush,
        exchangeTransfer,
        goToInitialDynamicWidgetView,
        setDynamicWidgetView,
        setHeaderAlert,
        setShowAuthFlow,
        t,
    ]);
    return submit;
};

exports.useSubmitExchangeFunding = useSubmitExchangeFunding;
