{"version":3,"file":"StytchB2BContext.mjs","sources":["../../../../../../../../../web/src/ui/react/b2b/StytchB2BContext.tsx"],"sourcesContent":["import {\n  Member,\n  MemberSession,\n  Organization,\n  PermissionsMap,\n  StytchProjectConfigurationInput,\n} from '@stytch/core/public';\nimport { mergeWithStableProps } from '@stytch/js-utils';\nimport React, {\n  ComponentType,\n  createContext,\n  ReactNode,\n  useCallback,\n  useContext,\n  useEffect,\n  useMemo,\n  useRef,\n} from 'react';\n\nimport { StytchB2BClient } from '../../../b2b/StytchB2BClient';\nimport { isStytchSSRProxy } from '../bindings/StytchSSRProxy';\nimport { useAsyncState } from '../utils/async';\nimport { noProviderError } from '../utils/errors';\nimport { invariant } from '../utils/invariant';\n\n// There's no user facing strings in this file\n/* eslint-disable lingui/no-unlocalized-strings */\n\ntype StytchB2BContext<TProjectConfiguration extends StytchProjectConfigurationInput> =\n  | {\n      isMounted: false;\n    }\n  | {\n      isMounted: true;\n      client: StytchB2BClient<TProjectConfiguration>;\n    };\n\ntype SWRMemberUninitialized = {\n  /**\n   * Either the active {@link Member} object, or null if the member is not logged in.\n   */\n  member: null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRMemberInitialized = {\n  /**\n   * Either the active {@link Member} object, or null if the member is not logged in.\n   */\n  member: Member | null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: true;\n};\n\ntype SWRMember<TAlwaysInitialized extends boolean = boolean> = TAlwaysInitialized extends true\n  ? SWRMemberInitialized\n  : SWRMemberInitialized | SWRMemberUninitialized;\n\nconst initialMember: SWRMember = {\n  member: null,\n  fromCache: false,\n  isInitialized: false,\n};\n\ntype SWRMemberSessionUninitialized = {\n  /**\n   * Either the active {@link MemberSession} object, or null if the member is not logged in.\n   */\n  session: null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRMemberSessionInitialized = {\n  /**\n   * Either the active {@link MemberSession} object, or null if the member is not logged in.\n   */\n  session: MemberSession | null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: true;\n};\n\ntype SWRMemberSession<TAlwaysInitialized extends boolean = boolean> = TAlwaysInitialized extends true\n  ? SWRMemberSessionInitialized\n  : SWRMemberSessionInitialized | SWRMemberSessionUninitialized;\n\nconst initialMemberSession: SWRMemberSession = {\n  session: null,\n  fromCache: false,\n  isInitialized: false,\n};\n\ntype SWROrganizationUninitialized = {\n  /**\n   * Either the active {@link Organization} object, or null if the member is not logged in.\n   */\n  organization: null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWROrganizationInitialized = {\n  /**\n   * Either the active {@link Organization} object, or null if the member is not logged in.\n   */\n  organization: Organization | null;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: true;\n};\n\ntype SWROrganization<TAlwaysInitialized extends boolean = boolean> = TAlwaysInitialized extends true\n  ? SWROrganizationInitialized\n  : SWROrganizationInitialized | SWROrganizationUninitialized;\n\nconst initialOrganization: SWROrganization = {\n  organization: null,\n  fromCache: false,\n  isInitialized: false,\n};\n\nconst StytchB2BContext = createContext<StytchB2BContext<StytchProjectConfigurationInput>>({ isMounted: false });\nconst StytchMemberContext = createContext<SWRMember>(initialMember);\nconst StytchMemberSessionContext = createContext<SWRMemberSession>(initialMemberSession);\nconst StytchOrganizationContext = createContext<SWROrganization>(initialOrganization);\n\nexport const useIsMounted__INTERNAL = (): boolean => useContext(StytchB2BContext).isMounted;\n\n/**\n * Returns the active Member.\n * The Stytch SDKs are used for client-side authentication and session management.\n * Check the isInitialized property to determine if the SDK has completed initialization.\n * Check the fromCache property to determine if the member data is from persistent storage.\n * @example\n * const {member, isInitialized, fromCache} = useStytchMember();\n * if (!isInitialized) {\n *     return <p>Loading...</p>;\n * }\n * return (<h1>Welcome, {member.name}</h1>);\n */\nexport const useStytchMember = <TAssumeHydrated extends boolean = false>(): SWRMember<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMember', 'StytchB2BProvider'));\n  return useContext(StytchMemberContext) as SWRMember<TAssumeHydrated>;\n};\n\n/**\n * Returns the active member's Stytch member session.\n * The Stytch SDKs are used for client-side authentication and session management.\n * Check the isInitialized property to determine if the SDK has completed initialization.\n * Check the fromCache property to determine if the session data is from persistent storage.\n * @example\n * const {session, isInitialized, fromCache} = useStytchMemberSession();\n * useEffect(() => {\n *   if (!isInitialized) {\n *     return;\n *   }\n *   if (!session) {\n *     router.replace('/login')\n *   }\n * }, [session, isInitialized]);\n */\nexport const useStytchMemberSession = <\n  TAssumeHydrated extends boolean = false,\n>(): SWRMemberSession<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchMemberSession', 'StytchB2BProvider'));\n  return useContext(StytchMemberSessionContext) as SWRMemberSession<TAssumeHydrated>;\n};\n\n/**\n * Returns the active Stytch organization.\n * The Stytch SDKs are used for client-side authentication and session management.\n * Check the isInitialized property to determine if the SDK has completed initialization.\n * Check the fromCache property to determine if the organization data is from persistent storage.\n * @example\n * const {organization, isInitialized, fromCache} = useStytchOrganization();\n * if (!isInitialized) {\n *     return <p>Loading...</p>;\n * }\n * return (<p>Welcome to {organization.organization_name}</p>);\n */\nexport const useStytchOrganization = <TAssumeHydrated extends boolean = false>(): SWROrganization<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchOrganization', 'StytchB2BProvider'));\n  return useContext(StytchOrganizationContext) as SWROrganization<TAssumeHydrated>;\n};\n\ntype SWRIsAuthorizedUninitialized = {\n  /**\n   * Whether the logged-in member is allowed to perform the specified action on the specified resource.\n   */\n  isAuthorized: false;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: false;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: false;\n};\n\ntype SWRIsAuthorizedInitialized = {\n  /**\n   * Whether the logged-in member is allowed to perform the specified action on the specified resource.\n   */\n  isAuthorized: boolean;\n  /**\n   * If true, indicates that the value returned is from the application cache and a state refresh is in progress.\n   */\n  fromCache: boolean;\n  /**\n   * If true, indicates that the SDK has completed initialization.\n   */\n  isInitialized: boolean;\n};\n\ntype SWRIsAuthorized<TAlwaysInitialized extends boolean> = TAlwaysInitialized extends true\n  ? SWRIsAuthorizedInitialized\n  : SWRIsAuthorizedInitialized | SWRIsAuthorizedUninitialized;\n\n/**\n * Determines whether the logged-in member is allowed to perform the specified action on the specified resource.\n * Returns `true` if the member can perform the action, `false` otherwise.\n *\n * If the member is not logged in, this method will always return false.\n * If the resource or action provided are not valid for the configured RBAC policy, this method will return false.\n *\n * Remember - authorization checks for sensitive actions should always occur on the backend as well.\n * @example\n * const { isAuthorized } = useStytchIsAuthorized('documents', 'edit');\n * return <button disabled={!isAuthorized}>Edit</button>\n */\nexport const useStytchIsAuthorized = <TAssumeHydrated extends boolean = false>(\n  resourceId: string,\n  action: string,\n): SWRIsAuthorized<TAssumeHydrated> => {\n  invariant(useIsMounted__INTERNAL(), noProviderError('useStytchIsAuthorized', 'StytchB2BProvider'));\n  const client = useStytchB2BClient();\n  const { session } = useStytchMemberSession();\n  const [isAuthorized, setIsAuthorized] = useAsyncState<SWRIsAuthorized<boolean>>({\n    isInitialized: false,\n    fromCache: false,\n    isAuthorized: false,\n  });\n\n  useEffect(() => {\n    if (isStytchSSRProxy(client)) {\n      return;\n    }\n\n    setIsAuthorized({\n      isInitialized: true,\n      fromCache: true,\n      isAuthorized: client.rbac.isAuthorizedSync(resourceId, action),\n    });\n  }, [action, client, resourceId, setIsAuthorized]);\n\n  useEffect(() => {\n    if (isStytchSSRProxy(client)) {\n      return;\n    }\n\n    client.rbac.isAuthorized(resourceId, action).then((isAuthorized) => {\n      setIsAuthorized({ isAuthorized, fromCache: false, isInitialized: true });\n    });\n  }, [client, session?.roles, resourceId, action, setIsAuthorized]);\n\n  return isAuthorized;\n};\n\n/**\n * Returns the Stytch B2B client stored in the Stytch context.\n *\n * @example\n * const stytch = useStytchB2BClient();\n * useEffect(() => {\n *   stytch.magicLinks.authenticate('...')\n * }, [stytch]);\n */\nexport const useStytchB2BClient = <\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>(): StytchB2BClient<TProjectConfiguration> => {\n  const ctx = useContext(StytchB2BContext);\n  invariant(ctx.isMounted, noProviderError('useStytchB2BClient', 'StytchB2BProvider'));\n  return ctx.client as StytchB2BClient<TProjectConfiguration>;\n};\n\nexport const withStytchB2BClient = <T extends object, TProjectConfiguration extends StytchProjectConfigurationInput>(\n  Component: ComponentType<T & { stytch: StytchB2BClient<TProjectConfiguration> }>,\n): ComponentType<T> => {\n  const WithStytch: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchB2BClient', 'StytchB2BProvider'));\n    return <Component {...props} stytch={useStytchB2BClient()} />;\n  };\n  WithStytch.displayName = `withStytch(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytch;\n};\n\nexport const withStytchMember = <T extends object, TAssumeHydrated extends boolean = false>(\n  Component: ComponentType<\n    T & { stytchMember: Member | null; stytchMemberIsInitialized: boolean; stytchMemberIsFromCache: boolean }\n  >,\n): ComponentType<T> => {\n  const WithStytchUser: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchMember', 'StytchB2BProvider'));\n    const { member, isInitialized, fromCache } = useStytchMember<TAssumeHydrated>();\n    return (\n      <Component\n        {...props}\n        stytchMember={member}\n        stytchMemberIsInitialized={isInitialized}\n        stytchMemberIsFromCache={fromCache}\n      />\n    );\n  };\n  WithStytchUser.displayName = `withStytchMember(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytchUser;\n};\n\nexport const withStytchMemberSession = <T extends object, TAssumeHydrated extends boolean = false>(\n  Component: ComponentType<\n    T & {\n      stytchMemberSession: MemberSession | null;\n      stytchMemberSessionIsInitialized: boolean;\n      stytchMemberSessionIsFromCache: boolean;\n    }\n  >,\n): ComponentType<T> => {\n  const WithStytchSession: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchMemberSession', 'StytchB2BProvider'));\n    const { session, isInitialized, fromCache } = useStytchMemberSession<TAssumeHydrated>();\n    return (\n      <Component\n        {...props}\n        stytchMemberSession={session}\n        stytchMemberSessionIsInitialized={isInitialized}\n        stytchMemberSessionIsFromCache={fromCache}\n      />\n    );\n  };\n  WithStytchSession.displayName = `withStytchMemberSession(${Component.displayName || Component.name || 'Component'})`;\n  return WithStytchSession;\n};\n\nexport const withStytchOrganization = <T extends object, TAssumeHydrated extends boolean = false>(\n  Component: ComponentType<\n    T & {\n      stytchOrganization: Organization | null;\n      stytchOrganizationIsInitialized: boolean;\n      stytchOrganizationIsFromCache: boolean;\n    }\n  >,\n): ComponentType<T> => {\n  const WithStytchOrganization: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('withStytchOrganization', 'StytchB2BProvider'));\n    const { organization, isInitialized, fromCache } = useStytchOrganization<TAssumeHydrated>();\n    return (\n      <Component\n        {...props}\n        stytchOrganization={organization}\n        stytchOrganizationIsInitialized={isInitialized}\n        stytchOrganizationIsFromCache={fromCache}\n      />\n    );\n  };\n  WithStytchOrganization.displayName = `withStytchOrganization(${\n    Component.displayName || Component.name || 'Component'\n  })`;\n  return WithStytchOrganization;\n};\n\ntype PermissionsLoaded<Permissions extends Record<string, string>> =\n  | {\n      loaded: false;\n      value: null;\n    }\n  | {\n      loaded: true;\n      value: PermissionsMap<Permissions>;\n    };\n\n/**\n * Wrap your component with this HOC in order to receive the permissions for the logged-in member.\n * Evaluates all permissions granted to the logged-in member.\n * Returns a Record<RoleId, Record<Action, boolean>> response indicating the member's permissions.\n * Each boolean will be `true` if the member can perform the action, `false` otherwise.\n *\n * If the member is not logged in, all values will be false.\n *\n * Remember - authorization checks for sensitive actions should always occur on the backend as well.\n * @example\n * type Permissions = {\n *   document: 'create' | 'read' | 'write\n *   image: 'create' | 'read'\n * }\n *\n * const MyComponent = (props) => {\n *   const canEditDocuments = props.stytchPermissions.document.edit;\n *   const canReadImages = props.stytchPermissions.image.read;\n * }\n * return withStytchPermissions<Permissions>(MyComponent)\n */\nexport const withStytchPermissions = <Permissions extends Record<string, string>, T extends object>(\n  Component: ComponentType<T & { stytchPermissions: PermissionsMap<Permissions> }>,\n): ComponentType<T> => {\n  const WithStytchPermissions: ComponentType<T> = (props) => {\n    invariant(useIsMounted__INTERNAL(), noProviderError('useRBACPermissions', 'StytchB2BProvider'));\n    const client = useStytchB2BClient();\n    const { session } = useStytchMemberSession();\n    const [permissions, setPermissions] = useAsyncState<PermissionsLoaded<Permissions>>({ loaded: false, value: null });\n    useEffect(() => {\n      client.rbac\n        .allPermissions<Permissions>()\n        .then((permissions) => setPermissions({ loaded: true, value: permissions }));\n    }, [client, session?.roles, setPermissions]);\n\n    if (!permissions.loaded) {\n      return null;\n    }\n    return <Component {...props} stytchPermissions={permissions.value} />;\n  };\n  WithStytchPermissions.displayName = `withStytchPermissions(${\n    Component.displayName || Component.name || 'Component'\n  })`;\n  return WithStytchPermissions;\n};\n\nexport interface StytchB2BProviderProps<\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n> {\n  /**\n   * A Stytch client instance, created using either {@link createStytchB2BClient} or {@link createStytchB2BClient}\n   */\n  stytch: StytchB2BClient<TProjectConfiguration>;\n  /**\n   * When true, the provider will assume that the component will only be\n   * rendered in a browser environment, either in a single-page application or\n   * after completing hydration of a server-rendered application. This allows\n   * cached values to be retrieved from the browser on the first render, meaning\n   * that the `isInitialized` value returned from Stytch hooks will be `true`\n   * starting from the first render.\n   *\n   * When `false`, the provider will defer initialization until after the first\n   * render, and `isInitialized` will initially be `false`.\n   *\n   * This value defaults to `false` in `@stytch/nextjs`.\n   */\n  assumeHydrated?: boolean;\n  children?: ReactNode;\n}\n\n/**\n * The Stytch Context Provider.\n * Wrap your application with this component in order to use Stytch everywhere in your app.\n * @example\n * const stytch = createStytchB2BClient('public-token-<find yours in the stytch dashboard>')\n *\n * return (\n *   <StytchB2BProvider stytch={stytch}>\n *     <App />\n *   </StytchB2BProvider>\n * )\n */\nexport const StytchB2BProvider = <\n  TProjectConfiguration extends StytchProjectConfigurationInput = Stytch.DefaultProjectConfiguration,\n>({\n  stytch,\n  children,\n  assumeHydrated = false,\n}: StytchB2BProviderProps<TProjectConfiguration>): JSX.Element => {\n  invariant(!useIsMounted__INTERNAL(), 'You cannot render a <StytchB2BProvider> inside another <StytchB2BProvider>.');\n  invariant(\n    !assumeHydrated || typeof window !== 'undefined',\n    'The `assumeHydrated` prop must be set to `false` when using StytchB2BProvider in a server environment.',\n  );\n\n  const ctx = useMemo(() => ({ client: stytch, isMounted: true }), [stytch]);\n\n  type ClientState<TAlwaysInitialized extends boolean> = {\n    member: SWRMember<TAlwaysInitialized>;\n    session: SWRMemberSession<TAlwaysInitialized>;\n    organization: SWROrganization<TAlwaysInitialized>;\n  };\n\n  const getHydratedState = useCallback(() => {\n    return {\n      member: { ...stytch.self.getInfo(), isInitialized: true },\n      session: { ...stytch.session.getInfo(), isInitialized: true },\n      organization: { ...stytch.organization.getInfo(), isInitialized: true },\n    } satisfies ClientState<true>;\n  }, [stytch]);\n\n  const getInitialState = () => {\n    return {\n      member: initialMember,\n      session: initialMemberSession,\n      organization: initialOrganization,\n    } satisfies ClientState<false>;\n  };\n\n  const [{ member, session, organization }, setClientState] = useAsyncState<ClientState<boolean>>(() =>\n    assumeHydrated ? getHydratedState() : (getInitialState() as ClientState<boolean>),\n  );\n\n  // Store the initial value of `assumeHydrated` in a ref, because it is\n  // logically only relevant for the first render\n  const assumeHydratedRef = useRef(assumeHydrated);\n  useEffect(() => {\n    if (isStytchSSRProxy(stytch)) {\n      return;\n    }\n\n    const updateState = () => {\n      setClientState((oldState) => mergeWithStableProps(oldState, getHydratedState()));\n    };\n\n    if (!assumeHydratedRef.current) {\n      updateState();\n    }\n\n    return stytch.onStateChange(updateState);\n  }, [getHydratedState, setClientState, stytch]);\n\n  return (\n    <StytchB2BContext.Provider value={ctx}>\n      <StytchOrganizationContext.Provider value={organization}>\n        <StytchMemberContext.Provider value={member}>\n          <StytchMemberSessionContext.Provider value={session}>{children}</StytchMemberSessionContext.Provider>\n        </StytchMemberContext.Provider>\n      </StytchOrganizationContext.Provider>\n    </StytchB2BContext.Provider>\n  );\n};\n"],"names":["initialMember","member","fromCache","isInitialized","initialMemberSession","session","initialOrganization","organization","StytchB2BContext","createContext","isMounted","StytchMemberContext","StytchMemberSessionContext","StytchOrganizationContext","useIsMounted__INTERNAL","useContext","useStytchMember","invariant","noProviderError","useStytchMemberSession","useStytchOrganization","useStytchIsAuthorized","resourceId","action","client","useStytchB2BClient","isAuthorized","setIsAuthorized","useAsyncState","useEffect","isStytchSSRProxy","rbac","isAuthorizedSync","then","roles","ctx","withStytchB2BClient","Component","WithStytch","props","React","stytch","displayName","name","withStytchMember","WithStytchUser","stytchMember","stytchMemberIsInitialized","stytchMemberIsFromCache","withStytchMemberSession","WithStytchSession","stytchMemberSession","stytchMemberSessionIsInitialized","stytchMemberSessionIsFromCache","withStytchOrganization","WithStytchOrganization","stytchOrganization","stytchOrganizationIsInitialized","stytchOrganizationIsFromCache","withStytchPermissions","WithStytchPermissions","permissions","setPermissions","loaded","value","allPermissions","stytchPermissions","StytchB2BProvider","children","assumeHydrated","window","useMemo","getHydratedState","useCallback","self","getInfo","getInitialState","setClientState","assumeHydratedRef","useRef","updateState","oldState","mergeWithStableProps","current","onStateChange","Provider"],"mappings":";;;;;;;AAuEA,MAAMA,aAAAA,GAA2B;IAC/BC,MAAAA,EAAQ,IAAA;IACRC,SAAAA,EAAW,KAAA;IACXC,aAAAA,EAAe;AACjB,CAAA;AAoCA,MAAMC,oBAAAA,GAAyC;IAC7CC,OAAAA,EAAS,IAAA;IACTH,SAAAA,EAAW,KAAA;IACXC,aAAAA,EAAe;AACjB,CAAA;AAoCA,MAAMG,mBAAAA,GAAuC;IAC3CC,YAAAA,EAAc,IAAA;IACdL,SAAAA,EAAW,KAAA;IACXC,aAAAA,EAAe;AACjB,CAAA;AAEA,MAAMK,iCAAmBC,aAAAA,CAAiE;IAAEC,SAAAA,EAAW;AAAM,CAAA,CAAA;AAC7G,MAAMC,oCAAsBF,aAAAA,CAAyBT,aAAAA,CAAAA;AACrD,MAAMY,2CAA6BH,aAAAA,CAAgCL,oBAAAA,CAAAA;AACnE,MAAMS,0CAA4BJ,aAAAA,CAA+BH,mBAAAA,CAAAA;MAEpDQ,sBAAAA,GAAyB,IAAeC,UAAAA,CAAWP,gBAAAA,CAAAA,CAAkBE;AAElF;;;;;;;;;;;UAYaM,eAAAA,GAAkB,IAAA;IAC7BC,SAAAA,CAAUH,sBAAAA,EAAAA,EAA0BI,gBAAgB,iBAAA,EAAmB,mBAAA,CAAA,CAAA;AACvE,IAAA,OAAOH,UAAAA,CAAWJ,mBAAAA,CAAAA;AACpB;AAEA;;;;;;;;;;;;;;;UAgBaQ,sBAAAA,GAAyB,IAAA;IAGpCF,SAAAA,CAAUH,sBAAAA,EAAAA,EAA0BI,gBAAgB,wBAAA,EAA0B,mBAAA,CAAA,CAAA;AAC9E,IAAA,OAAOH,UAAAA,CAAWH,0BAAAA,CAAAA;AACpB;AAEA;;;;;;;;;;;UAYaQ,qBAAAA,GAAwB,IAAA;IACnCH,SAAA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