'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');
var client = require('../../../../client/client.cjs');
require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs-sdk/client');
require('../../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../ViewContext/ViewContext.cjs');
require('../../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../../utils/constants/colors.cjs');
require('../../../../utils/constants/values.cjs');
require('../../../../shared/consts/index.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../store/state/nonce/nonce.cjs');
var dynamicContextProps = require('../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
var primaryWalletId = require('../../../../store/state/primaryWalletId/primaryWalletId.cjs');
var connectedWalletsInfo = require('../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../../events/dynamicEvents.cjs');
require('@dynamic-labs/locale');
var wallets = require('../../../../data/api/wallets/wallets.cjs');
var authMode = require('../../../../store/state/authMode/authMode.cjs');
var UserWalletsContext = require('../../../UserWalletsContext/UserWalletsContext.cjs');

const useRemoveWallet = ({ secondaryWallets, handleLogOut, setPrimaryWallet, }) => {
    const { removedWalletsIds, userWallets } = UserWalletsContext.useInternalUserWallets();
    const connectedWalletsInfo$1 = connectedWalletsInfo.useConnectedWalletsInfo();
    const nextWalletId = React.useMemo(() => (secondaryWallets[0] ? secondaryWallets[0].id : undefined), [secondaryWallets]);
    const callback = React.useCallback((walletId, clearStackAndPushInitialView) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const wallet = userWallets.find((w) => w.id === walletId);
        if (!wallet)
            return;
        const primaryWalletId$1 = primaryWalletId.getPrimaryWalletId();
        const isUnlinkingPrimaryWallet = walletId === primaryWalletId$1;
        const authMode$1 = authMode.getAuthMode();
        const user = (_a = client.getDynamicClient()) === null || _a === void 0 ? void 0 : _a.user;
        if (authMode$1 === 'connect-and-sign') {
            const isLastVerifiedCredential = (user === null || user === void 0 ? void 0 : user.verifiedCredentials.length) === 1;
            if (isLastVerifiedCredential) {
                throw new utils.DynamicError('This is your only login method. Add another wallet or log in method before removing it.');
            }
            yield wallets.unlinkWallet({
                environmentId: dynamicContextProps.getEnvironmentId(),
                onSuccess: () => {
                    removedWalletsIds.current.push(walletId);
                },
                // primaryWalletId is the suggestion of the next credential to be set as lastVerifiedCredential
                // If we're unlinking the primary wallet, we don't want to set it to that!
                primaryWalletId: isUnlinkingPrimaryWallet
                    ? undefined
                    : primaryWalletId$1,
                walletId,
            });
        }
        else {
            removedWalletsIds.current.push(walletId);
        }
        const updatedConnectedWalletsInfo = connectedWalletsInfo$1.filter((wallet) => wallet.id !== walletId);
        connectedWalletsInfo.setConnectedWalletsInfo(updatedConnectedWalletsInfo);
        if (isUnlinkingPrimaryWallet && nextWalletId) {
            setPrimaryWallet(nextWalletId);
        }
        // If there are no wallets left and no user (connect-only), we need to log out
        if (!user && !nextWalletId) {
            handleLogOut();
            return;
        }
        const numberOfWalletsWithSameConnector = userWallets.filter(({ connector }) => connector.key === wallet.connector.key).length;
        // If there's only 1, it's the one being removed and we can kill the session
        if (numberOfWalletsWithSameConnector === 1) {
            yield wallet.connector.endSession();
        }
    }), [
        userWallets,
        connectedWalletsInfo$1,
        nextWalletId,
        removedWalletsIds,
        setPrimaryWallet,
        handleLogOut,
    ]);
    return callback;
};

exports.useRemoveWallet = useRemoveWallet;
