{"version":3,"file":"utils.mjs","sources":["../../../../../../../../web/src/ui/b2c/utils.ts"],"sourcesContent":["import { BootstrapData } from '@stytch/core';\nimport { useEffect, useMemo, useState } from 'react';\n\nimport { readB2CInternals } from '../../utils/internal';\nimport { useStytch } from './GlobalContextProvider';\nimport { ProductId, StytchProduct } from './StytchProduct';\n\nexport const useBootstrap = (): { bootstrap: BootstrapData; fromCache: boolean } => {\n  const stytchClient = useStytch();\n  const [bootstrap, setBootstrap] = useState({\n    bootstrap: readB2CInternals(stytchClient).bootstrap.getSync(),\n    fromCache: true,\n  });\n\n  useEffect(() => {\n    readB2CInternals(stytchClient)\n      .bootstrap.getAsync()\n      .then((data) =>\n        setBootstrap({\n          bootstrap: data,\n          fromCache: false,\n        }),\n      );\n  }, [stytchClient]);\n\n  return bootstrap;\n};\n\nexport function hasProduct(products: StytchProduct[], product: ProductId) {\n  return products.some((p) => p.id === product);\n}\n\nexport function useProductComponents<Type extends 'screens' | 'tabs' | 'mainScreen'>(\n  products: StytchProduct[],\n  screenType: Type,\n) {\n  return useMemo(() => {\n    const map = {} as Required<StytchProduct[Type]>;\n    for (const product of products) {\n      if (product[screenType]) Object.assign(map, product[screenType]);\n    }\n    return map;\n  }, [products, screenType]);\n}\n\n/**\n * In B2C, ResetPassword and PasskeyRegistration always need passwords and passkey products respectively,\n * so we inject these into config just in case the customer don't have it. If they are missing the components\n * just crash immediately.\n */\nexport function addProduct<Config extends { products?: StytchProduct[] }>(\n  config: Config | undefined,\n  product: StytchProduct,\n): Config {\n  if (config?.products?.includes(product)) return config;\n\n  const products = config?.products ?? [];\n  return {\n    ...(config ?? {}),\n    products: [...products, product],\n  } as Config;\n}\n"],"names":["useBootstrap","stytchClient","useStytch","bootstrap","setBootstrap","useState","readB2CInternals","getSync","fromCache","useEffect","getAsync","then","data","hasProduct","products","product","some","p","id","useProductComponents","screenType","useMemo","map","Object","assign","addProduct","config","includes"],"mappings":";;;;;MAOaA,YAAAA,GAAe,IAAA;AAC1B,IAA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