{"version":3,"file":"flowComponent.mjs","names":[],"sources":["../../../../../../../src/lib/componentFactory/flowComponent.tsx"],"sourcesContent":["import type {\n  FlowComponentName,\n  FlowComponentProps as FlowComponentPropsOfName,\n} from \"@/components/propTypes\";\nimport type {\n  ComponentProps,\n  ComponentType,\n  FunctionComponent,\n  ReactElement,\n  ReactNode,\n  RefAttributes,\n} from \"react\";\nimport { cloneElement, memo } from \"react\";\nimport type { PropsWithTunnel } from \"@/lib/types/props\";\nimport SlotContextProvider from \"@/lib/slotContext/SlotContextProvider\";\nimport { useProps } from \"@/lib/hooks/useProps\";\nimport { useComponentPropsContext } from \"@/lib/propsContext/propsContext\";\nimport { ComponentPropsContextProvider } from \"@/components/ComponentPropsContextProvider\";\nimport { ClearPropsContext } from \"@/components/ClearPropsContext\";\nimport ClearPropsContextView from \"@/views/ClearPropsContextView\";\nimport { UiComponentTunnelProvider } from \"@/components/UiComponentTunnel/UiComponentTunnelProvider\";\nimport { UiComponentTunnelEntry } from \"@/components/UiComponentTunnel/UiComponentTunnelEntry\";\nimport {\n  useReportComponentUsage,\n  ViewCompositionReset,\n} from \"@/components/ComponentUsageProvider\";\n\ntype RefType<T> = T extends RefAttributes<infer R> ? R : undefined;\n\nexport interface FlowComponentProps<R = HTMLDivElement>\n  extends PropsWithTunnel, RefAttributes<R> {\n  /**\n   * A React element the component is wrapped with. The element is cloned and\n   * receives the component as its only child — useful to render the component\n   * inside a link, a tooltip trigger or any other wrapper without changing the\n   * surrounding markup. `null` takes a wrapper a surrounding props context set\n   * back off, the way `tunnel: null` does for a tunnel.\n   */\n  wrapWith?: ReactElement | null;\n}\n\nexport type FlowComponentImplementationProps<C extends FlowComponentName> =\n  Omit<FlowComponentPropsOfName<C>, keyof FlowComponentProps> &\n    RefAttributes<RefType<FlowComponentPropsOfName<C>>>;\n\ntype FlowComponentImplementationType<C extends FlowComponentName> =\n  ComponentType<FlowComponentImplementationProps<C>>;\n\ntype FlowComponentType<C extends FlowComponentName> = FunctionComponent<\n  FlowComponentPropsOfName<C> &\n    RefAttributes<RefType<FlowComponentPropsOfName<C>>>\n>;\n\nexport type FlowComponentProvisionType = \"provider\" | \"ui\" | \"layout\";\n\ninterface Options {\n  type?: FlowComponentProvisionType;\n  isRemoteComponent?: boolean;\n}\n\nexport function flowComponent<C extends FlowComponentName>(\n  componentName: C,\n  ImplementationComponentType: FlowComponentImplementationType<C>,\n  options: Options = {},\n): FlowComponentType<C> {\n  type Props = FlowComponentPropsOfName<C> &\n    RefAttributes<RefType<FlowComponentPropsOfName<C>>>;\n\n  const { type = \"ui\", isRemoteComponent = false } = options;\n\n  const MemoizedImplementationComponentType = memo(ImplementationComponentType);\n\n  function Component(props: Props) {\n    const isViewComposition = useReportComponentUsage(componentName);\n\n    const { tunnel, wrapWith, ...propsWithContext } = useProps(\n      componentName,\n      props as FlowComponentPropsOfName<C>,\n    ) as FlowComponentProps<RefType<FlowComponentPropsOfName<C>>>;\n\n    const implementationTypeProps = propsWithContext as ComponentProps<\n      typeof ImplementationComponentType\n    >;\n\n    const componentProps = useComponentPropsContext(componentName);\n\n    ImplementationComponentType.displayName = `FlowComponentImpl(${componentName})`;\n\n    let element: ReactNode = (\n      <MemoizedImplementationComponentType {...implementationTypeProps} />\n    );\n\n    if (type === \"ui\" || type === \"layout\") {\n      element = (\n        <UiComponentTunnelProvider component={componentName}>\n          {element}\n        </UiComponentTunnelProvider>\n      );\n    }\n\n    element = (\n      <ComponentPropsContextProvider componentProps={componentProps}>\n        {element}\n      </ComponentPropsContextProvider>\n    );\n\n    if (type === \"ui\") {\n      /**\n       * Protect the inside of UI components for accidental prop changes through\n       * the Props Context.\n       */\n      if (isRemoteComponent) {\n        element = <ClearPropsContext>{element}</ClearPropsContext>;\n      } else {\n        /**\n         * In case of a UI component that does not have a remote counterpart\n         * (like the Modal component), the <ClearPropsContext> must be\n         * additionally applied on the host side by using the\n         * <ClearPropsContextView>.\n         *\n         * This prevents Props Contexts created by parent components on the host\n         * side affecting the children of this UI component.\n         */\n        element = (\n          <ClearPropsContext>\n            <ClearPropsContextView>{element}</ClearPropsContextView>\n          </ClearPropsContext>\n        );\n      }\n    }\n\n    if (\"slot\" in props && !!props.slot && typeof props.slot === \"string\") {\n      element = (\n        <SlotContextProvider slot={props.slot} component={componentName}>\n          {element}\n        </SlotContextProvider>\n      );\n    }\n\n    if (wrapWith) {\n      element = cloneElement(wrapWith, undefined, element);\n    }\n\n    if (tunnel) {\n      element = (\n        <UiComponentTunnelEntry\n          id={tunnel.id}\n          component={tunnel.component}\n          staticEntryId={tunnel.staticEntryId}\n        >\n          {element}\n        </UiComponentTunnelEntry>\n      );\n    }\n\n    if (isViewComposition) {\n      element = <ViewCompositionReset>{element}</ViewCompositionReset>;\n    }\n\n    return element;\n  }\n\n  Component.displayName = `FlowComponent(${componentName})`;\n  return memo(Component);\n}\n"],"mappings":";;;;;;;;;;;;;;AA4DA,SAAgB,cACd,eACA,6BACA,UAAmB,CAAC,GACE;CAItB,MAAM,EAAE,OAAO,MAAM,oBAAoB,UAAU;CAEnD,MAAM,sCAAsC,KAAK,2BAA2B;CAE5E,SAAS,UAAU,OAAc;EAC/B,MAAM,oBAAoB,wBAAwB,aAAa;EAE/D,MAAM,EAAE,QAAQ,UAAU,GAAG,qBAAqB,SAChD,eACA,KACF;EAEA,MAAM,0BAA0B;EAIhC,MAAM,iBAAiB,yBAAyB,aAAa;EAE7D,4BAA4B,cAAc,qBAAqB,cAAc;EAE7E,IAAI,UACF,oBAAC,qCAAD,EAAqC,GAAI,wBAA0B,CAAA;EAGrE,IAAI,SAAS,QAAQ,SAAS,UAC5B,UACE,oBAAC,2BAAD;GAA2B,WAAW;GACnC,UAAA;EACwB,CAAA;EAI/B,UACE,oBAAC,+BAAD;GAA+C;GAC5C,UAAA;EAC4B,CAAA;EAGjC,IAAI,SAAS,MAAM;;;;;GAKjB,IAAI,mBACF,UAAU,oBAAC,mBAAD,EAAA,UAAoB,QAA2B,CAAA;;;;;;;;;;;GAWzD,UACE,oBAAC,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