'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import '../../../client/client.js';
import 'react';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '@dynamic-labs/utils';
import '../../constants/colors.js';
import '../../constants/values.js';
import '../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import '@dynamic-labs/locale';
import { storeSelectedWallet } from '../../../data/api/wallets/wallets.js';
import { getEnvironmentId } from '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import { setPrimaryWalletId } from '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../events/dynamicEvents.js';
import { getUserProfile } from '../../../client/extension/user/getUserProfile/getUserProfile.js';

const updatePrimaryWalletId = (walletId) => __awaiter(void 0, void 0, void 0, function* () {
    var _a;
    setPrimaryWalletId(walletId);
    const user = getUserProfile();
    if (user) {
        const environmentId = 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 storeSelectedWallet({ environmentId, walletId });
    }
});

export { updatePrimaryWalletId };
