import React__default, { useState, useEffect, useMemo } from 'react';
import { messages } from '../../messages/b2b/en.mjs';
import { readB2BInternals } from '../../utils/internal.mjs';
import { I18nProviderWrapper } from '../components/atoms/I18nProviderWrapper.mjs';
import { usePresentationWithDefault, PresentationContext } from '../components/PresentationConfig.mjs';
import AppContainer from './Container.mjs';
import { DEFAULT_STATE, GlobalContextProvider } from './GlobalContextProvider.mjs';
import { AppScreens } from './types/AppScreens.mjs';
import { RedirectURLType, AuthFlowType } from '../../../../core/src/public/b2b/ui.mjs';

const InternalAppContainer = ({ client, presentation, callbacks, config, strings })=>{
    /**
   * Read the watermark out of sync storage, then read it out of async storage
   */ const [displayWatermark, setDisplayWatermark] = useState(()=>{
        const { displayWatermark } = readB2BInternals(client).bootstrap.getSync();
        return displayWatermark;
    });
    useEffect(()=>{
        readB2BInternals(client).bootstrap.getAsync().then((data)=>{
            readB2BInternals(client).networkClient.logEvent({
                name: 'render_b2b_login_screen',
                details: {
                    options: config,
                    bootstrap: data
                }
            });
            setDisplayWatermark(data.displayWatermark);
        });
    // eslint-disable-next-line react-hooks/exhaustive-deps -- SDK-1354
    }, [
        client
    ]);
    const getRedirectTypeFromUrl = ()=>{
        const url = new URL(window.location.href);
        const redirectType = url.searchParams.get('stytch_redirect_type');
        return redirectType;
    };
    const redirectType = getRedirectTypeFromUrl();
    const authFlowType = redirectType === RedirectURLType.ResetPassword ? AuthFlowType.PasswordReset : config.authFlowType;
    const initialState = {
        ...DEFAULT_STATE,
        screen: authFlowType === AuthFlowType.PasswordReset ? AppScreens.PasswordResetForm : AppScreens.Main,
        flowState: {
            ...DEFAULT_STATE.flowState,
            type: authFlowType
        }
    };
    const configWithDefaults = useMemo(()=>({
            ...config,
            // While `sessionOptions` is required in the types, we still want to handle the error gracefully if it's not provided.
            // We set it to an empty object here so that future checks for `sessionOptions.sessionDurationMinutes` throw a `can't read property of undefined` error,
            // and instead throw an error regarding a missing `sessionDurationMinutes` error from the headless client.
            sessionOptions: config.sessionOptions ?? {}
        }), [
        config
    ]);
    const presentationValue = usePresentationWithDefault(presentation, displayWatermark, configWithDefaults, 'B2BProduct');
    return /*#__PURE__*/ React__default.createElement(GlobalContextProvider, {
        client: client,
        callbacks: callbacks,
        config: configWithDefaults,
        initialState: initialState
    }, /*#__PURE__*/ React__default.createElement(PresentationContext.Provider, {
        value: presentationValue
    }, /*#__PURE__*/ React__default.createElement(I18nProviderWrapper, {
        messages: messages,
        overrides: strings
    }, /*#__PURE__*/ React__default.createElement(AppContainer, null))));
};

export { InternalAppContainer };
//# sourceMappingURL=InternalAppContainer.mjs.map
