'use client'
'use strict';

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

var _tslib = require('../../../../../../_virtual/_tslib.cjs');
var React = require('react');
var client = require('@dynamic-labs-sdk/client');
var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../ViewContext/ViewContext.cjs');
var logger = require('../../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
var localStorage = require('../../../../utils/constants/localStorage.cjs');
require('../../../../utils/constants/colors.cjs');
require('../../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../../shared/consts/index.cjs');
var authMode = require('../../../../store/state/authMode/authMode.cjs');
var connectedWalletsInfo = require('../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
var primaryWalletId = require('../../../../store/state/primaryWalletId/primaryWalletId.cjs');
var useDynamicEvents = require('../../../../utils/hooks/events/useDynamicEvents/useDynamicEvents.cjs');
require('../../DynamicContext.cjs');
require('../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../../CaptchaContext/CaptchaContext.cjs');
require('../../../ErrorContext/ErrorContext.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-sdk/client/core');
require('../../../../client/client.cjs');
require('../../../../config/ApiEndpoint.cjs');
require('../../../../events/dynamicEvents.cjs');
require('@dynamic-labs/locale');
require('../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
var getClientSessionKeys = require('../../../../utils/functions/clientSessionKeys/getClientSessionKeys.cjs');
require('../../../AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../AccountExistsContext/AccountExistsContext.cjs');
require('../../../UserWalletsContext/UserWalletsContext.cjs');
require('../../../VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../../utils/functions/compareChains/compareChains.cjs');
require('../../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../ThemeContext/ThemeContext.cjs');
require('../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../../SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../LoadingContext/LoadingContext.cjs');
require('../../../WalletContext/WalletContext.cjs');
require('../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../../MockContext/MockContext.cjs');
require('../../../../views/CollectUserDataView/useFields.cjs');
require('../../../FieldsStateContext/FieldsStateContext.cjs');
require('../../../UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../../../store/state/walletOptions/walletOptions.cjs');
require('react-i18next');
require('../../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('../../../../components/Alert/Alert.cjs');
require('../../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../../components/IconButton/IconButton.cjs');
require('../../../../components/InlineWidget/InlineWidget.cjs');
require('../../../../components/Input/Input.cjs');
require('../../../../components/IsBrowser/IsBrowser.cjs');
require('../../../../components/MenuList/Dropdown/Dropdown.cjs');
require('../../../../components/OverlayCard/OverlayCard.cjs');
require('../../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../../components/Popper/Popper/Popper.cjs');
require('../../../../components/Popper/PopperContext/PopperContext.cjs');
require('react-focus-lock');
require('qrcode');
require('formik');
require('../../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../../WalletGroupContext/WalletGroupContext.cjs');
require('../../../IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../../widgets/DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../../../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../FooterAnimationContext/index.cjs');
require('../../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../PasskeyContext/PasskeyContext.cjs');
require('../../../OnrampContext/OnrampContext.cjs');
require('../../../../store/state/sendBalances.cjs');
require('../../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../../../index.cjs');
require('../../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../../../store/state/tokenBalances.cjs');
require('../../../../store/state/multichainBalances.cjs');
require('../../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');

const useHandleLogout = (params) => {
    const paramsRef = React.useRef(params);
    paramsRef.current = params;
    const logout = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const p = paramsRef.current;
        // We store which wallet connectors were active before logout
        // to not have to worry about having the array change midway through
        const activeConnectors = [
            ...p.walletConnectors,
            ...p.connectedWallets.map((connectedWallet) => connectedWallet.connector),
        ];
        yield client.logout();
        logger.logger.logVerboseTroubleshootingMessage('useHandleLogout called', {
            params: paramsRef.current,
        });
        utils.tracing.logEvent('logout', 'useHandleLogout called');
        p.setSelectedWalletConnectorKey(null);
        p.clearAllWalletConnectSessions();
        p.setShowDynamicUserProfile(false);
        p.setShowBridgeWidget(false);
        p.resetLoginWithEmail();
        p.setLegacyIsVerifying(false);
        utils.StorageService.removeItem(localStorage.WALLET_PICKER_SEARCH_KEY);
        utils.StorageService.removeItem(localStorage.EXCHANGE_PICKER_SEARCH_KEY);
        utils.StorageService.removeItem(localStorage.PHANTOM_SIGNATURE_STATE);
        utils.StorageService.removeItem(localStorage.DELEGATION_STATE);
        getClientSessionKeys.clearClientSessionKeys();
        authMode.setAuthMode(p.initialAuthenticationMode);
        p.setShowAuthFlow(false, {
            emitCancelAuth: false,
            ignoreIfIsEmbeddedWidget: false,
            initializeWalletConnect: false,
            performMultiWalletChecks: false,
        });
        p.setMultiWalletWidgetState('idle');
        connectedWalletsInfo.resetConnectedWalletsInfo();
        p.removeBridgeOnboardingCompleted();
        primaryWalletId.resetPrimaryWalletId();
        p.removeConnectedNameService();
        p.removeSessionStorageSessionKeys();
        yield Promise.all(activeConnectors.map((connector) => connector.endSession()));
    }), []);
    /** Trigger a logout when the SDK failed to load data from storage */
    React.useEffect(() => {
        utils.StorageService.events.on('parseFailure', logout);
        return () => {
            utils.StorageService.events.off('parseFailure', logout);
        };
    }, [logout]);
    /** Allows triggering a logout from anywhere in the code */
    useDynamicEvents.useInternalDynamicEvents('triggerLogout', () => logout());
    return logout;
};

exports.useHandleLogout = useHandleLogout;
