import { useMemo, useState, useEffect } from 'react';
import { readB2CInternals } from '../../utils/internal.mjs';
import { useStytch } from './GlobalContextProvider.mjs';

const useBootstrap = ()=>{
    const stytchClient = useStytch();
    const [bootstrap, setBootstrap] = useState({
        bootstrap: readB2CInternals(stytchClient).bootstrap.getSync(),
        fromCache: true
    });
    useEffect(()=>{
        readB2CInternals(stytchClient).bootstrap.getAsync().then((data)=>setBootstrap({
                bootstrap: data,
                fromCache: false
            }));
    }, [
        stytchClient
    ]);
    return bootstrap;
};
function hasProduct(products, product) {
    return products.some((p)=>p.id === product);
}
function useProductComponents(products, screenType) {
    return useMemo(()=>{
        const map = {};
        for (const product of products){
            if (product[screenType]) Object.assign(map, product[screenType]);
        }
        return map;
    }, [
        products,
        screenType
    ]);
}
/**
 * In B2C, ResetPassword and PasskeyRegistration always need passwords and passkey products respectively,
 * so we inject these into config just in case the customer don't have it. If they are missing the components
 * just crash immediately.
 */ function addProduct(config, product) {
    if (config?.products?.includes(product)) return config;
    const products = config?.products ?? [];
    return {
        ...config ?? {},
        products: [
            ...products,
            product
        ]
    };
}

export { addProduct, hasProduct, useBootstrap, useProductComponents };
//# sourceMappingURL=utils.mjs.map
