'use client'
import { __awaiter } from '../../../../../_virtual/_tslib.js';
import { useRef, useCallback, useEffect } from 'react';
import { logger } from '../../../shared/logger.js';
import { setSessionValidation } from '../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import '@dynamic-labs-sdk/client/core';
import '@dynamic-labs/sdk-api-core';
import '../../../client/client.js';
import { useClientInitStatus } from '../../../client/extension/hooks/useClientInitStatus/useInitStatus.js';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../../context/ViewContext/ViewContext.js';
import '@dynamic-labs/wallet-book';
import '@dynamic-labs/utils';
import '../../constants/colors.js';
import '../../constants/values.js';
import '../../../shared/consts/index.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../events/dynamicEvents.js';
import { useProjectSettings } from '../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.js';
import { handleStoreAndEnvironmentMismatch } from './handleStoreAndEnvironmentMismatch/handleStoreAndEnvironmentMismatch.js';
import { handleWalletInfoOutOfSync } from './handleWalletInfoOutOfSync/handleWalletInfoOutOfSync.js';

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

export { useValidateSession };
