'use client'
'use strict';

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

var React = require('react');
var walletBook = require('@dynamic-labs/wallet-book');
var utils = require('@dynamic-labs/utils');
var logger = require('../../../shared/logger.cjs');
var index = require('../../../shared/utils/functions/lastAuthenticatedAccount/index.cjs');
var shouldFetchNonce = require('../../../shared/utils/functions/shouldFetchNonce/shouldFetchNonce.cjs');
var loadingAndLifecycle = require('../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
var authMode = require('../../../store/state/authMode/authMode.cjs');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-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('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('../../../shared/consts/index.cjs');
require('../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../events/dynamicEvents.cjs');
var useInitStatus = require('../../../client/extension/initStatus/useInitStatus/useInitStatus.cjs');

const useGlobalLoading = ({ connectedInfo, connectedWallets, projectSettings, primaryWalletId, primaryWallet, user, walletBook: walletBook$1, enableInstrumentation, appName, environmentId, nonce, }) => {
    var _a;
    // Once we declare the SDK as loaded, we do not ever want to flip back to false
    const hasLoaded = React.useRef(false);
    const loadingStart = React.useRef(new Date().getTime());
    const didTraceSdkMountedRef = React.useRef(false);
    const initStatus = useInitStatus.useInitStatus();
    if (!didTraceSdkMountedRef.current) {
        didTraceSdkMountedRef.current = true;
        utils.tracing.logEvent('sdk-react-core.initialization', 'sdk-mounted');
    }
    const { sessionValidation } = loadingAndLifecycle.useLoadingAndLifecycle();
    const authMode$1 = authMode.useAuthMode();
    const isFetchingNonce = React.useMemo(() => shouldFetchNonce.shouldFetchNonce({
        authMode: authMode$1,
    }) && !nonce, [authMode$1, nonce]);
    const finishLoading = () => {
        if (hasLoaded.current)
            return;
        hasLoaded.current = true;
        logger.logger.debug('[useGlobalLoading] SDK finished loading', {
            authMode: authMode$1,
            enableInstrumentation,
            primaryWallet,
            user,
        });
        utils.tracing.logEvent('sdk-react-core.initialization', 'sdk-loaded');
        // Calculate how long loading took and emit log
        if (enableInstrumentation) {
            const loadingTime = new Date().getTime() - loadingStart.current;
            // Pass as default arg and as an object. The object value will be injected
            // in the log later on, keeping its format as a number for calculation purposes
            logger.logger.instrument(`SDK loaded in (ms) ${loadingTime}`, {
                appName,
                environmentId,
                key: 'sdkHasLoaded',
                primaryWalletId,
                time: loadingTime,
                userId: user === null || user === void 0 ? void 0 : user.userId,
            });
        }
    };
    if (hasLoaded.current)
        return true;
    const isWalletBookLoading = !walletBook.isWalletBookPopulated(walletBook$1);
    const isJavascriptSDKLoading = initStatus === 'uninitialized' || initStatus === 'in-progress';
    const isProjectSettingsLoading = !projectSettings;
    const shouldHavePrimaryWallet = primaryWalletId ||
        (user && ((_a = index.lastAuthenticatedAccount(user)) === null || _a === void 0 ? void 0 : _a.format) === 'blockchain');
    const isPrimaryWalletLoading = Boolean(shouldHavePrimaryWallet && !primaryWallet);
    const isConnectOnlyWalletsLoading = authMode$1 === 'connect-only' &&
        connectedInfo &&
        connectedWallets.length === 0;
    const isValidatingSession = !sessionValidation;
    logger.logger.logVerboseTroubleshootingMessage('[useGlobalLoading] SDK loading', {
        isConnectOnlyWalletsLoading,
        isFetchingNonce,
        isPrimaryWalletLoading,
        isProjectSettingsLoading,
        isValidatingSession,
        isWalletBookLoading,
        javascriptSDKInitStatus: initStatus,
    });
    if (!isWalletBookLoading &&
        !isProjectSettingsLoading &&
        !isPrimaryWalletLoading &&
        !isConnectOnlyWalletsLoading &&
        !isValidatingSession &&
        !isFetchingNonce &&
        !isJavascriptSDKLoading) {
        finishLoading();
    }
    return hasLoaded.current;
};

exports.useGlobalLoading = useGlobalLoading;
