{"version":3,"file":"generateProductComponentsOrdering.mjs","sources":["../../../../../../../../web/src/ui/b2b/generateProductComponentsOrdering.ts"],"sourcesContent":["import { AuthFlowType } from '@stytch/core/public';\n\nimport { StytchB2BProduct } from './StytchB2BProduct';\nimport { ButtonComponent, MainScreenComponent } from './types/AppScreens';\nimport { FlowState } from './types/AppState';\n\nexport const isButtonsComponent = (component: MainScreenComponent): component is ButtonComponent =>\n  component === MainScreenComponent.OAuthButtons || component === MainScreenComponent.SSOButtons;\n\n/*\n  Here we are generating the ordering and which components will be displayed.\n  We use the MainScreenComponent enum this because there is some complex logic here, and\n  we want to be able to unit test.\n\n  Rules we want to follow when generating the components:\n  1. If we're displaying both an email-based method (email magic links and/or email OTPs) and passwords,\n  we need to display them together as a single wrapped component. The index of this wrapped component is\n  equivalent to the first index of either email-based method or passwords in the config products list.\n  2. If we have both buttons and input, we want to display a divider between the last 2 elements.\n  3. Some components have both a discovery and a non-discovery version. We want to display the correct version\n  based on the flow type (found in state).\n  4. We want to display the components in the order that they are listed in the config.\n\n  This function should be considered the source of truth for which components to render\n  and what order to render them in as of 6/21/23.\n*/\nexport const generateProductComponentsOrdering = (\n  products: StytchB2BProduct[],\n  flowState: FlowState,\n): MainScreenComponent[][] => {\n  const flowStateType = flowState.type;\n  const productsSet = new Set(products.map((p) => p.id));\n  const displayEmailAndPasswordsTogether =\n    (productsSet.has('emailMagicLinks') || productsSet.has('emailOtp')) && productsSet.has('passwords');\n  const emailPasswordCombinedComponent =\n    flowStateType === AuthFlowType.Organization\n      ? MainScreenComponent.PasswordEmailCombined\n      : MainScreenComponent.PasswordEmailCombinedDiscovery;\n\n  const productComponentsSet = products.reduce((components, product) => {\n    switch (product.id) {\n      case 'emailMagicLinks':\n      case 'emailOtp':\n        if (displayEmailAndPasswordsTogether) {\n          components.add(emailPasswordCombinedComponent);\n        } else {\n          components.add(\n            flowStateType === AuthFlowType.Organization\n              ? MainScreenComponent.EmailForm\n              : MainScreenComponent.EmailDiscoveryForm,\n          );\n        }\n        break;\n      case 'oauth':\n        components.add(MainScreenComponent.OAuthButtons);\n        break;\n      case 'sso': {\n        const isSsoValid =\n          flowStateType === AuthFlowType.Discovery ||\n          (flowStateType === AuthFlowType.Organization &&\n            (flowState.organization?.sso_active_connections?.length ?? 0) > 0);\n        if (isSsoValid) {\n          components.add(MainScreenComponent.SSOButtons);\n        }\n        break;\n      }\n      case 'passwords':\n        components.add(\n          displayEmailAndPasswordsTogether ? emailPasswordCombinedComponent : MainScreenComponent.PasswordsEmailForm,\n        );\n        break;\n    }\n    return components;\n  }, new Set<MainScreenComponent>());\n\n  return Array.from(productComponentsSet).reduce<MainScreenComponent[][]>((groups, component, i, arr) => {\n    const prevComponent = i > 0 ? arr[i - 1] : undefined;\n\n    // Group consecutive components with buttons\n    const shouldJoinPreviousGroup = prevComponent && isButtonsComponent(component) && isButtonsComponent(prevComponent);\n\n    if (shouldJoinPreviousGroup) {\n      groups[groups.length - 1].push(component);\n    } else {\n      // If the previous group was a different type than this group, prepend a divider\n      const prevGroup = groups.length > 0 ? groups[groups.length - 1] : undefined;\n      if (prevGroup && isButtonsComponent(prevGroup[0]) !== isButtonsComponent(component)) {\n        groups.push([MainScreenComponent.Divider]);\n      }\n\n      groups.push([component]);\n    }\n    return groups;\n  }, []);\n};\n"],"names":["isButtonsComponent","component","MainScreenComponent","OAuthButtons","SSOButtons","generateProductComponentsOrdering","products","flowState","flowStateType","type","productsSet","Set","map","p","id","displayEmailAndPasswordsTogether","has","emailPasswordCombinedComponent","AuthFlowType","Organization","PasswordEmailCombined","PasswordEmailCombinedDiscovery","productComponentsSet","reduce","components","product","add","EmailForm","EmailDiscoveryForm","isSsoValid","Discovery","organization","sso_active_connections","length","PasswordsEmailForm","Array","from","groups","i","arr","prevComponent","undefined","shouldJoinPreviousGroup","push","prevGroup","Divider"],"mappings":";;;AAMO,MAAMA,kBAAAA,GAAqB,CAACC,SAAAA,GACjCA,SAAAA,KAAcC,mBAAAA,CAAoBC,YAAY,IAA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