'use client'
import { __awaiter } from '../../../../../../../../_virtual/_tslib.js';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { Typography } from '../../../../../../components/Typography/Typography.js';
import { logger } from '../../../../../../shared/logger.js';
import { getTransactionLink } from '../../../../../../utils/functions/getTransactionLink/getTransactionLink.js';
import { pixelToRem } from '../../../../../../utils/functions/pixelToRem/pixelToRem.js';
import { useInternalFundWithWallet } from '../../../../../../utils/hooks/useFundWithWallet/useFundWithWallet.js';
import { useViewSettings } from '../../../../../../utils/hooks/useViewSettings/useViewSettings.js';
import { useWidgetContext } from '../../../../context/DynamicWidgetContext.js';

const useSubmitWalletFunding = () => {
    const { fund } = useInternalFundWithWallet();
    const { goToInitialDynamicWidgetView, setHeaderAlert } = useWidgetContext();
    const { t } = useTranslation();
    const txConfirmationSettings = useViewSettings('transaction-confirmation');
    return useCallback((_a) => __awaiter(void 0, [_a], void 0, function* ({ tokenAmount, wallet, tokenSymbol, tokenBalances, toWallet, }) {
        var _b;
        if (!tokenAmount)
            return;
        // This should be defined because if the current token is not in the tokenBalances,
        // the user would not be able to select it in the token select screen,
        // or if it was already selected, the user would see balance as 0 and submit button would be disabled
        const token = (_b = tokenBalances === null || tokenBalances === void 0 ? void 0 : tokenBalances.find(({ symbol }) => symbol === tokenSymbol)) === null || _b === void 0 ? void 0 : _b.token;
        if (!token)
            return;
        try {
            const txId = yield fund({
                amount: parseFloat(tokenAmount),
                fromWallet: wallet,
                toWallet,
                token: token === 'native' ? undefined : token,
            });
            logger.info(`Funding with wallet succeeded with transaction id: ${txId}`);
            let txLink;
            if (txId) {
                txLink = yield getTransactionLink({
                    transactionHash: txId,
                    userOpSupportedExplorers: txConfirmationSettings === null || txConfirmationSettings === void 0 ? void 0 : txConfirmationSettings.userOpSupportedExplorers,
                    walletConnector: wallet.connector,
                });
            }
            const TxnSuccessAlert = (jsxs(Typography, { variant: 'body_normal', color: 'current-color', children: [t('dyn_wallet_funding.success'), txLink && (jsx(Typography, { as: 'a', href: txLink, target: '_blank', rel: 'noreferrer', style: {
                            marginLeft: pixelToRem(4),
                            textDecoration: 'underline',
                            textDecorationThickness: pixelToRem(1),
                            textUnderlineOffset: pixelToRem(2),
                        }, weight: 'bold', color: 'current-color', children: t('dyn_wallet_funding.success_txn_link') }))] }));
            goToInitialDynamicWidgetView();
            setHeaderAlert({
                content: TxnSuccessAlert,
                variant: 'success',
            });
        }
        catch (error) {
            logger.error('Failed to fund with wallet', error);
        }
    }), [
        fund,
        goToInitialDynamicWidgetView,
        setHeaderAlert,
        t,
        txConfirmationSettings,
    ]);
};

export { useSubmitWalletFunding };
