'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');
require('@dynamic-labs/utils');
require('../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('@dynamic-labs/iconic');
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('@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');
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');
var useConfirmationModal = require('../useConfirmationModal/useConfirmationModal.cjs');
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');
require('../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../../../store/state/tokenBalances.cjs');
require('../../../store/state/multichainBalances.cjs');
require('../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../../../../index.cjs');
require('react-dom');
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('../useSubdomainCheck/useSubdomainCheck.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('../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../context/FooterAnimationContext/index.cjs');
require('../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../store/state/sendBalances.cjs');
require('../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
var SignMessageConfirmationModal = require('../../../modals/SignMessageConfirmationModal/SignMessageConfirmationModal.cjs');
var SyncWalletPromptModal = require('../../../modals/SyncWalletPromptModal/SyncWalletPromptModal.cjs');
var TransactionConfirmationModal = require('../../../modals/TransactionConfirmationModal/TransactionConfirmationModal.cjs');
var AddNetworkModal = require('../../../modals/AddNetworkModal/AddNetworkModal.cjs');
var ZkSyncApprovalModal = require('../../../modals/ZkSyncApprovalModal/ZkSyncApprovalModal.cjs');

const useWalletUiUtils = ({ hideEmbeddedWalletTransactionUIs = false, }) => {
    const confirmationStateRef = React.useRef('enabled');
    const { t } = reactI18next.useTranslation();
    const { modal: sendTransactionModal, open: openSendTransaction } = useConfirmationModal.useConfirmationModal({
        elementId: 'dynamic-send-transaction',
    });
    const { modal: signMessageModal, open: openSignMessage } = useConfirmationModal.useConfirmationModal({
        elementId: 'dynamic-sign-message',
    });
    const { modal: syncWalletPromptModal, open: openSyncWalletPrompt } = useConfirmationModal.useConfirmationModal({
        elementId: 'dynamic-sync-wallet',
    });
    const { modal: addNetworkModal, open: openAddNetwork } = useConfirmationModal.useConfirmationModal({
        elementId: 'dynamic-prompt-to-add-network',
    });
    const { modal: zkSyncCreateSessionModal, open: openZkSyncCreateSession } = useConfirmationModal.useConfirmationModal({
        elementId: 'dynamic-zksync-approval',
    });
    const shouldConfirmAction = React.useCallback(({ walletConnector }) => {
        // For Dynamic Waas connector, we disable confirmation if hideEmbeddedWalletTransactionUIs is true
        // V2 wallets require the UI component to be rendered even if the confirmation is disabled
        // Waas does not require the UI component to be rendered so it can be skipped
        if ((walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.overrideKey) === 'dynamicwaas' &&
            hideEmbeddedWalletTransactionUIs) {
            return false;
        }
        if (confirmationStateRef.current === 'disabled') {
            confirmationStateRef.current = 'enabled';
            return false;
        }
        return true;
    }, [hideEmbeddedWalletTransactionUIs, confirmationStateRef]);
    const signMessage = React.useCallback((_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ walletConnector, message, handler }) {
        // prevent the modal from opening itself again
        if (!shouldConfirmAction({ walletConnector }))
            return handler();
        return openSignMessage((resolve, reject) => (jsxRuntime.jsx(SignMessageConfirmationModal.SignMessageConfirmationModal, { message: message, handler: handler, onSignMessage: resolve, onReject: reject, hideModal: hideEmbeddedWalletTransactionUIs &&
                !(walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.isGlobalTransaction) }))).then((signedMessage) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
            if (signedMessage === 'callHandlerOutsideModal')
                return handler();
            else
                return signedMessage;
        }));
    }), [hideEmbeddedWalletTransactionUIs, openSignMessage, shouldConfirmAction]);
    const sendTransaction = React.useCallback((walletConnector, transaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        // prevent the modal from opening itself again
        if (!shouldConfirmAction({ walletConnector }))
            return transaction.submit();
        return openSendTransaction((resolve, reject) => (jsxRuntime.jsx(TransactionConfirmationModal.TransactionConfirmationModal, { copykey: 'dyn_send_transaction.confirmation.title', title: t('dyn_send_transaction.confirmation.title'), transaction: transaction, onTransactionResponseSuccess: resolve, onReject: reject, walletConnector: walletConnector, hideModal: hideEmbeddedWalletTransactionUIs &&
                !(walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.isGlobalTransaction) }))).then((signedTransaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
            if (signedTransaction === 'callHandlerOutsideModal')
                return transaction.submit();
            else
                return signedTransaction;
        }));
    }), [
        hideEmbeddedWalletTransactionUIs,
        openSendTransaction,
        shouldConfirmAction,
        t,
    ]);
    // Solana only
    const signTransaction = React.useCallback((walletConnector, transaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        // prevent the modal from opening itself again
        if (!shouldConfirmAction({ walletConnector }))
            return transaction.submit();
        return openSendTransaction((resolve, reject) => (jsxRuntime.jsx(TransactionConfirmationModal.TransactionConfirmationModal, { copykey: 'dyn_send_transaction.confirmation.title', title: t('dyn_send_transaction.confirmation.title'), transaction: transaction, onTransactionResponseSuccess: resolve, onReject: reject, walletConnector: walletConnector, hideModal: hideEmbeddedWalletTransactionUIs &&
                !(walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.isGlobalTransaction) }))).then((signedTransaction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
            if (signedTransaction === 'callHandlerOutsideModal')
                return transaction.submit();
            else
                return signedTransaction;
        }));
    }), [
        hideEmbeddedWalletTransactionUIs,
        openSendTransaction,
        shouldConfirmAction,
        t,
    ]);
    const syncWallet = React.useCallback((_b) => _tslib.__awaiter(void 0, [_b], void 0, function* ({ walletConnector, activeAddress, expectedAddress }) {
        yield openSyncWalletPrompt((resolve, reject) => (jsxRuntime.jsx(SyncWalletPromptModal.SyncWalletPromptModal, { onResolve: resolve, onReject: reject, connector: walletConnector, expectedAddress: expectedAddress, activeAddress: activeAddress })));
    }), [openSyncWalletPrompt]);
    const addNetwork = React.useCallback((_c) => _tslib.__awaiter(void 0, [_c], void 0, function* ({ handler, network, walletConnector }) {
        yield openAddNetwork((resolve) => (jsxRuntime.jsx(AddNetworkModal.AddNetworkModal, { network: network, walletConnector: walletConnector, onCancel: resolve, onNetworkAdded: resolve, addNetworkMutation: handler })));
    }), [openAddNetwork]);
    const zkSyncCreateSession = React.useCallback((_d) => _tslib.__awaiter(void 0, [_d], void 0, function* ({ session: sessionConfig }) {
        return yield openZkSyncCreateSession((resolve, reject) => (jsxRuntime.jsx(ZkSyncApprovalModal.ZkSyncApprovalModal, { session: sessionConfig, onCancel: reject, onApproved: () => resolve(true) })));
    }), [openZkSyncCreateSession]);
    const disabledConfirmationOnce = React.useCallback(() => {
        confirmationStateRef.current = 'disabled';
    }, []);
    const walletUiUtils = React.useMemo(() => ({
        addNetwork,
        disabledConfirmationOnce,
        sendTransaction,
        signMessage,
        signTransaction,
        syncWallet,
        zkSyncCreateSession,
    }), [
        signMessage,
        signTransaction,
        disabledConfirmationOnce,
        sendTransaction,
        syncWallet,
        addNetwork,
        zkSyncCreateSession,
    ]);
    return React.useMemo(() => [
        signMessageModal ||
            sendTransactionModal ||
            syncWalletPromptModal ||
            addNetworkModal ||
            zkSyncCreateSessionModal,
        walletUiUtils,
    ], [
        signMessageModal,
        sendTransactionModal,
        syncWalletPromptModal,
        addNetworkModal,
        zkSyncCreateSessionModal,
        walletUiUtils,
    ]);
};

exports.useWalletUiUtils = useWalletUiUtils;
