'use client'
import { useEffect } from 'react';
import { isSessionKeyCompatibleWallet } from '@dynamic-labs/wallet-connector-core';
import '../../../context/DynamicContext/useDynamicContext/useDynamicContext.js';
import { useInternalDynamicContext } from '../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js';
import '@dynamic-labs/sdk-api-core';
import '../../constants/values.js';
import '@dynamic-labs/utils';
import '../../../../../_virtual/_tslib.js';
import '@dynamic-labs/multi-wallet';
import { logger } from '../../../shared/logger.js';
import 'react-international-phone';
import '../../constants/colors.js';
import '@dynamic-labs/iconic';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '@dynamic-labs/wallet-book';
import '../../../shared/consts/index.js';
import '../../../store/state/nonce/nonce.js';
import { isTurnkeyEnabled } from '../../functions/isTurnkeyEnabled/isTurnkeyEnabled.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import '@dynamic-labs-sdk/client/core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '../../../events/dynamicEvents.js';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import { usingV3Wallets } from '../../functions/usingV3Wallets/usingV3Wallets.js';
import '../../functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import { useSmartWallets } from '../useSmartWallets/useSmartWallets.js';
import { useAuthMode } from '../../../store/state/authMode/authMode.js';

const useIsLoggedIn = () => {
    var _a, _b, _c;
    const { user, primaryWallet, projectSettings } = useInternalDynamicContext();
    const authMode = useAuthMode();
    const { getEOAWallet } = useSmartWallets();
    const eoaWallet = primaryWallet && getEOAWallet(primaryWallet);
    const isSessionKeyCompatible = (primaryWallet && isSessionKeyCompatibleWallet(primaryWallet)) ||
        (eoaWallet && isSessionKeyCompatibleWallet(eoaWallet));
    const isTurnkeyProviderEnabled = isTurnkeyEnabled(projectSettings);
    // a user is set even when on connect-only mode if signing in with social,
    // but it won't have a primaryWallet in this case
    const isLoggedIn = Boolean(user) || (authMode === 'connect-only' && Boolean(primaryWallet));
    const userWalletsCredentials = (_a = user === null || user === void 0 ? void 0 : user.verifiedCredentials) === null || _a === void 0 ? void 0 : _a.filter(({ walletProvider }) => walletProvider);
    const hasEmbeddedWalletVerifiedCredential = Boolean(userWalletsCredentials === null || userWalletsCredentials === void 0 ? void 0 : userWalletsCredentials.find(({ walletProvider }) => walletProvider === 'embeddedWallet'));
    const isTurnkeyPregen = !usingV3Wallets(projectSettings) &&
        isTurnkeyProviderEnabled &&
        ((_c = (_b = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk) === null || _b === void 0 ? void 0 : _b.embeddedWallets) === null || _c === void 0 ? void 0 : _c.automaticEmbeddedWalletCreation) &&
        (!(projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets.forceAuthenticatorAtSignup) ||
            isSessionKeyCompatible);
    const shouldHaveTurnkeyWallet = isTurnkeyPregen &&
        // if user doesn't have a branded wallet only
        (!(userWalletsCredentials === null || userWalletsCredentials === void 0 ? void 0 : userWalletsCredentials.length) || hasEmbeddedWalletVerifiedCredential);
    useEffect(() => {
        logger.logVerboseTroubleshootingMessage('useIsLoggedIn', {
            authMode,
            eoaWallet,
            isSessionKeyCompatible,
            isTurnkeyProviderEnabled,
            primaryWallet,
            projectSettings,
            user,
        });
    }, [
        authMode,
        eoaWallet,
        isSessionKeyCompatible,
        isTurnkeyProviderEnabled,
        primaryWallet,
        projectSettings,
        user,
    ]);
    // if turnkey is enabled we need to check if the user that is logged/connected
    // has a primaryWallet to not show the widget without secure
    if (shouldHaveTurnkeyWallet) {
        return Boolean(primaryWallet && isLoggedIn);
    }
    return isLoggedIn;
};

export { useIsLoggedIn };
