'use client'
'use strict';

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

var _tslib = require('../../../../../_virtual/_tslib.cjs');
var React = require('react');
var logger = require('../../../shared/logger.cjs');
var loadingAndLifecycle = require('../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('@dynamic-labs-sdk/client/core');
require('@dynamic-labs/sdk-api-core');
require('../../../client/client.cjs');
var useInitStatus = require('../../../client/extension/hooks/useClientInitStatus/useInitStatus.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('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../constants/colors.cjs');
require('../../constants/values.cjs');
require('../../../shared/consts/index.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 useProjectSettings = require('../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
var handleStoreAndEnvironmentMismatch = require('./handleStoreAndEnvironmentMismatch/handleStoreAndEnvironmentMismatch.cjs');
var handleWalletInfoOutOfSync = require('./handleWalletInfoOutOfSync/handleWalletInfoOutOfSync.cjs');

const useValidateSession = ({ environmentId, handleLogOut }) => {
    const didRunRef = React.useRef(false);
    const projectSettings = useProjectSettings.useProjectSettings();
    const initStatus = useInitStatus.useClientInitStatus();
    const logout = React.useCallback(
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (reason, params) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        logger.logger.info(`Logging out due to invalid session - Reason: ${reason}`, Object.assign({}, params));
        return handleLogOut();
    }), [handleLogOut]);
    const validateSession = React.useCallback((projectSettings) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const params = {
            environmentId,
            logout,
            projectSettings,
        };
        yield Promise.all([
            handleStoreAndEnvironmentMismatch.handleStoreAndEnvironmentMismatch(params),
            handleWalletInfoOutOfSync.handleWalletInfoOutOfSync(params),
        ]);
        // Regardless of whether we logged out or not, session validation is now complete
        loadingAndLifecycle.setSessionValidation(true);
    }), [logout, environmentId]);
    React.useEffect(() => {
        if (didRunRef.current || !projectSettings || initStatus !== 'finished')
            return;
        didRunRef.current = true;
        validateSession(projectSettings);
    }, [validateSession, projectSettings, initStatus]);
};

exports.useValidateSession = useValidateSession;
