'use client'
'use strict';

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

var _tslib = require('../../../../../../_virtual/_tslib.cjs');
var React = require('react');
var utils = require('@dynamic-labs/utils');
require('../../../../../index.cjs');
require('../../../../context/DynamicContext/useDynamicContext/useDynamicContext.cjs');
var useInternalDynamicContext = require('../../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
var ViewContext = require('../../../../context/ViewContext/ViewContext.cjs');
var rejectPromiseRef = require('../../../functions/rejectPromiseRef/rejectPromiseRef.cjs');
var resolvePromiseRef = require('../../../functions/resolvePromiseRef/resolvePromiseRef.cjs');
var useDynamicEvents = require('../../events/useDynamicEvents/useDynamicEvents.cjs');
var useFundingHelpers = require('../../useFundingHelpers/useFundingHelpers.cjs');
var FilterWalletsForExternalFunding = require('../utils/FilterWalletsForExternalFunding.cjs');
var index = require('../../../functions/walletFilters/index.cjs');
var dynamicEvents = require('../../../../events/dynamicEvents.cjs');

const useConnectWalletForFunding = () => {
    const { primaryWallet, setShowAuthFlow } = useInternalDynamicContext.useInternalDynamicContext();
    const { clearStackAndPush } = ViewContext.useViewContext();
    const { initiatedByWidgetRef, getWalletAddress, resetWalletConnectSession } = useFundingHelpers.useFundingHelpers();
    const connectWalletPromiseRef = React.useRef(null);
    useDynamicEvents.useInternalDynamicEvents('authFlowClose', () => {
        rejectPromiseRef.rejectPromiseRef(connectWalletPromiseRef, new utils.DynamicError('User cancelled', 'user_cancelled'));
    });
    const openWalletList = React.useCallback((initiatedByWidget) => {
        const onSelectWallet = (_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ walletConnector, }) {
            if (!connectWalletPromiseRef.current)
                return;
            let isDuplicate = false;
            try {
                initiatedByWidgetRef.current = initiatedByWidget;
                const walletAddress = yield getWalletAddress(walletConnector);
                if (!walletAddress) {
                    rejectPromiseRef.rejectPromiseRef(connectWalletPromiseRef, new utils.DynamicError('No wallet address found', 'no_wallet_address_found'));
                    return;
                }
                if (walletAddress === (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address)) {
                    isDuplicate = true;
                    clearStackAndPush('duplicate-wallet', {
                        initiatedByWidget,
                        onReturn: openWalletList,
                    });
                    return;
                }
                const wallet = walletConnector.createWallet({
                    address: walletAddress,
                    chain: walletConnector.connectedChain,
                    connector: walletConnector,
                    id: 'external-funding-wallet',
                    isAuthenticated: false,
                    key: walletConnector.key,
                });
                resolvePromiseRef.resolvePromiseRef(connectWalletPromiseRef, wallet);
                dynamicEvents.publicDynamicEvents.emit('walletConnectedForFunding', {
                    initiatedByWidget,
                    isPhantomRedirect: false,
                    wallet,
                });
            }
            catch (error) {
                rejectPromiseRef.rejectPromiseRef(connectWalletPromiseRef, error);
            }
            finally {
                if (!isDuplicate)
                    setShowAuthFlow(false);
            }
        });
        clearStackAndPush('external-funding-wallet-list', {
            onSelectWallet,
            showDefaultFooter: true,
            viewWalletsFilter: utils.pipe(FilterWalletsForExternalFunding.FilterWalletsForExternalFunding).pipe(index.FilterChain(primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.chain)),
        });
    }, [
        clearStackAndPush,
        getWalletAddress,
        initiatedByWidgetRef,
        primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address,
        primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.chain,
        setShowAuthFlow,
    ]);
    return React.useCallback((_a) => _tslib.__awaiter(void 0, [_a], void 0, function* ({ initiatedByWidget, }) {
        if (connectWalletPromiseRef.current) {
            return connectWalletPromiseRef.current.promise;
        }
        resetWalletConnectSession();
        connectWalletPromiseRef.current = new utils.DeferredPromise();
        openWalletList(initiatedByWidget);
        setShowAuthFlow(true, {
            emitCancelAuth: false,
            ignoreIfIsEmbeddedWidget: true,
            initializeWalletConnect: true,
            performMultiWalletChecks: false,
        });
        return connectWalletPromiseRef.current.promise;
    }), [openWalletList, resetWalletConnectSession, setShowAuthFlow]);
};

exports.useConnectWalletForFunding = useConnectWalletForFunding;
