'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var utils = require('@dynamic-labs/utils');
require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs-sdk/client/core');
require('../../../client/client.cjs');
require('react');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('../../../utils/constants/values.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../utils/constants/colors.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../../context/ViewContext/ViewContext.cjs');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../shared/consts/index.cjs');
var dynamicContextProps = require('../dynamicContextProps/dynamicContextProps.cjs');
require('../primaryWalletId/primaryWalletId.cjs');
require('../connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../events/dynamicEvents.cjs');
var utils$1 = require('../../../data/api/utils.cjs');
require('@dynamic-labs/locale');
var useDynamicEvents = require('../../../utils/hooks/events/useDynamicEvents/useDynamicEvents.cjs');
require('../../../context/DynamicContext/DynamicContext.cjs');
require('../loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../authMode/authMode.cjs');
require('../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../context/ErrorContext/ErrorContext.cjs');
require('../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../utils/functions/compareChains/compareChains.cjs');
require('../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../context/ThemeContext/ThemeContext.cjs');
require('../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
var usePromise = require('../../../utils/hooks/usePromise/usePromise.cjs');
require('@dynamic-labs/types');
require('../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../context/LoadingContext/LoadingContext.cjs');
require('../../../context/WalletContext/WalletContext.cjs');
require('../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../../context/MockContext/MockContext.cjs');
require('../../../views/CollectUserDataView/useFields.cjs');
require('../../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../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('../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../context/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('../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../context/FooterAnimationContext/index.cjs');
require('../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../sendBalances.cjs');
require('../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('../tokenBalances.cjs');
require('../multichainBalances.cjs');
require('../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
var createStoreState = require('../../internalImplementation/utils/createStoreState/createStoreState.cjs');

const { getNonce: getNonceRaw, setNonce, useNonce: useNonceRaw, } = createStoreState.createStoreState('nonce');
const getNonce = () => getNonceRaw().nonce;
const useNonce = () => useNonceRaw().nonce;
const storeNewNonce = (newNonce) => {
    const expiry = new Date().getTime() + 60000 * 60 * 24;
    setNonce({
        expiresAt: expiry,
        nonce: newNonce,
    });
};
let nonceExpiryTimeout;
const checkAndExpireNonceWhenRequired = () => {
    const nonce = getNonceRaw();
    if (!(nonce === null || nonce === void 0 ? void 0 : nonce.expiresAt)) {
        return;
    }
    if (nonceExpiryTimeout) {
        clearTimeout(nonceExpiryTimeout);
    }
    const timeToExpire = nonce.expiresAt - new Date().getTime();
    const timeout = timeToExpire > 0 ? timeToExpire : 0;
    nonceExpiryTimeout = setTimeout(() => {
        logger.logger.debug('[Nonce] Expiring nonce', {
            expiresAt: nonce.expiresAt,
            nonce: nonce.nonce,
        });
        setNonce({
            expiresAt: undefined,
            nonce: undefined,
        });
    }, timeout);
};
const useFetchNonce = ({ shouldFetch = true, retries = 2, } = {}) => {
    const nonce = useNonce();
    const environmentId = dynamicContextProps.useEnvironmentId();
    checkAndExpireNonceWhenRequired();
    const { retrigger } = usePromise.usePromise(() => _tslib.__awaiter(void 0, void 0, void 0, function* () { return utils$1.getNonce(environmentId); }), {
        deps: [environmentId],
        enabled: shouldFetch && !nonce,
        onResolve: (newNonce) => {
            storeNewNonce(newNonce);
            checkAndExpireNonceWhenRequired();
        },
        retries,
    });
    useDynamicEvents.useDynamicEvents('logout', retrigger);
    return nonce;
};
const consumeNonce = () => {
    const nonce = getNonce();
    if (!nonce) {
        throw new utils.DynamicError('Nonce is not defined');
    }
    setNonce({
        expiresAt: undefined,
        nonce: undefined,
    });
    return nonce;
};
const fetchAndStoreNonce = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
    const existingNonce = getNonce();
    if (existingNonce) {
        return;
    }
    const environmentId = dynamicContextProps.getEnvironmentId();
    const nonce = yield utils$1.getNonce(environmentId);
    storeNewNonce(nonce);
});

exports.consumeNonce = consumeNonce;
exports.fetchAndStoreNonce = fetchAndStoreNonce;
exports.getNonce = getNonce;
exports.setNonce = setNonce;
exports.useFetchNonce = useFetchNonce;
exports.useNonce = useNonce;
