'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { useCallback } from 'react';
import { DynamicError } from '@dynamic-labs/utils';
import { getWalletProvider } from '@dynamic-labs/wallet-connector-core';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import { logger } from '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../constants/colors.js';
import '../../constants/values.js';
import '../../../shared/consts/index.js';
import { USER_NOT_LOGGED_IN } from '../../constants/errors.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import { getVerifyArgs } from '../../functions/getVerifyArgs/getVerifyArgs.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import { updatePrimaryWalletId } from '../../functions/updatePrimaryWalletId/updatePrimaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import { useDynamicSettings, useEnvironmentId } from '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '@dynamic-labs/locale';
import { transferWallet } from '../../../data/api/wallets/wallets.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import '../../../events/dynamicEvents.js';
import { useOnboardingCompleteUser } from '../../../client/extension/user/useOnboardingCompleteUser/useOnboardingCompleteUser.js';
import '../../../context/DynamicContext/useDynamicContext/useDynamicContext.js';
import { useInternalDynamicContext } from '../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js';
import { useInternalUserWallets } from '../../../context/UserWalletsContext/UserWalletsContext.js';

const useTransferWallet = () => {
    const user = useOnboardingCompleteUser();
    const { displaySiweStatement, events } = useDynamicSettings();
    const environmentId = useEnvironmentId();
    const { primaryWallet } = useInternalDynamicContext();
    const { addedWalletsIds } = useInternalUserWallets();
    const updatePrimaryWallet = useCallback((walletConnector, user) => {
        const credentialId = user.lastVerifiedCredentialId;
        if (!credentialId)
            return;
        const shouldUpdatePrimary = !primaryWallet ||
            walletConnector.key === primaryWallet.connector.key ||
            (primaryWallet.connector.connectedChain === 'FLOW' &&
                walletConnector.connectedChain === 'FLOW');
        if (shouldUpdatePrimary) {
            updatePrimaryWalletId(credentialId);
        }
    }, [primaryWallet]);
    return useCallback((props) => __awaiter(void 0, void 0, void 0, function* () {
        if (!user)
            throw new DynamicError(USER_NOT_LOGGED_IN, 'user_not_logged_in');
        const { overrideSiweStatement, walletConnector } = props;
        const args = Object.assign(Object.assign({}, props), { displaySiweStatement,
            environmentId, siweStatement: overrideSiweStatement, walletProvider: getWalletProvider(walletConnector) });
        const verifyArgs = yield getVerifyArgs(args);
        if (events === null || events === void 0 ? void 0 : events.onSignedMessage) {
            try {
                events.onSignedMessage({
                    messageToSign: verifyArgs.messageToSign,
                    signedMessage: verifyArgs.signedMessage,
                });
            }
            catch (error) {
                logger.warn('Error calling onSignedMessage callback:', error);
            }
        }
        const userProfile = yield transferWallet(environmentId, verifyArgs);
        if (!userProfile) {
            throw new DynamicError('Error transferring wallet.', 'transferring_wallet');
        }
        updatePrimaryWallet(walletConnector, userProfile);
        const walletId = userProfile.lastVerifiedCredentialId;
        if (walletId) {
            addedWalletsIds.current.push(walletId);
        }
    }), [
        addedWalletsIds,
        displaySiweStatement,
        environmentId,
        events,
        updatePrimaryWallet,
        user,
    ]);
};

export { useTransferWallet };
