'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('../../../client/client.cjs');
require('react');
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('@dynamic-labs/utils');
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');
require('@dynamic-labs/locale');
var wallets = require('../../../data/api/wallets/wallets.cjs');
var dynamicContextProps = require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
var primaryWalletId = require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../events/dynamicEvents.cjs');
var getUserProfile = require('../../../client/extension/user/getUserProfile/getUserProfile.cjs');

const updatePrimaryWalletId = (walletId) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    var _a;
    primaryWalletId.setPrimaryWalletId(walletId);
    const user = getUserProfile.getUserProfile();
    if (user) {
        const environmentId = dynamicContextProps.getEnvironmentId();
        // We will compare the value of this wallet's lastSelectedAt to the latest
        // selected credential's lastSelectedAt. If it's the same, we can skip the
        // /select endpoint. This is to avoid unnecessary calls to the /select
        // endpoint, which causes an update to the user's JWT.
        const currentWalletLastSelectedAt = (_a = user.verifiedCredentials.find((credential) => credential.id === walletId)) === null || _a === void 0 ? void 0 : _a.lastSelectedAt;
        if (currentWalletLastSelectedAt) {
            const lastSelectedWalletCredential = user.verifiedCredentials.reduce((mostRecentSelectedCredential, credentialToCheck) => {
                if (credentialToCheck.format !== 'blockchain' ||
                    !credentialToCheck.lastSelectedAt) {
                    return mostRecentSelectedCredential;
                }
                if (!mostRecentSelectedCredential ||
                    !mostRecentSelectedCredential.lastSelectedAt) {
                    return credentialToCheck;
                }
                return credentialToCheck.lastSelectedAt >
                    mostRecentSelectedCredential.lastSelectedAt
                    ? credentialToCheck
                    : mostRecentSelectedCredential;
            }, null);
            if ((lastSelectedWalletCredential === null || lastSelectedWalletCredential === void 0 ? void 0 : lastSelectedWalletCredential.lastSelectedAt) &&
                lastSelectedWalletCredential.lastSelectedAt.getTime() ===
                    currentWalletLastSelectedAt.getTime()) {
                return;
            }
        }
        yield wallets.storeSelectedWallet({ environmentId, walletId });
    }
});

exports.updatePrimaryWalletId = updatePrimaryWalletId;
