'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var React = require('react');
require('@dynamic-labs/utils');
require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs-sdk/client/core');
require('../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('../../../shared/consts/index.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
var updatePrimaryWalletId = require('../../functions/updatePrimaryWalletId/updatePrimaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
var dynamicContextProps = require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
var primaryWalletId = require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../events/dynamicEvents.cjs');
require('@dynamic-labs/locale');
var user = require('../../../data/api/user/user.cjs');
var findPrimaryWalletVCForUser = require('../../functions/findPrimaryWalletVCForUser/findPrimaryWalletVCForUser.cjs');

/**
 * Refresh user state
 * @returns A callback function that can be used to refresh the user state
 */
const useRefreshUser = () => React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const user$1 = yield user.refreshUserJwt({ environmentId: dynamicContextProps.getEnvironmentId() });
    if (!(user$1 === null || user$1 === void 0 ? void 0 : user$1.verifiedCredentials))
        throw new Error('useRefreshUser resulted in an invalid user: no verified credentials found');
    // Check if the primary wallet is still in this user's VCs
    const primaryWalletInUserVCs = user$1.verifiedCredentials.some(({ id }) => id === primaryWalletId.getPrimaryWalletId());
    if (primaryWalletInUserVCs)
        return user$1;
    // Try to set primary wallet ID from available wallets (AA or embedded)
    const primaryWalletVC = findPrimaryWalletVCForUser.findPrimaryWalletVCForUser(user$1);
    if (primaryWalletVC) {
        updatePrimaryWalletId.updatePrimaryWalletId(primaryWalletVC.id);
        return user$1;
    }
    // No wallet VCs found, unset the primary wallet
    primaryWalletId.setPrimaryWalletId(undefined);
    return user$1;
}), []);

exports.useRefreshUser = useRefreshUser;
