{
  "version": 3,
  "sources": ["src/react.tsx"],
  "sourcesContent": ["import {\n  MinimalComponentProps,\n  getComponent,\n  getMergedComponentProperties,\n  isWebComponent,\n  registeredDatasources,\n  registered,\n  toKebabCase\n} from '@sitecore/byoc'\nimport React from 'react'\nexport * from '@sitecore/byoc'\n\nvar NextClientsideRenderer: any = null\nvar NextDynamicFunction: any = null\n\nexport interface ComponentProps extends MinimalComponentProps {\n  fallbackWrapper?: boolean\n  fallback?: any\n  clientFallback?: any\n}\n\n/**\n * A special case of Component designed for Next.js.\n * It uses `next/dynamic` to control rendering on the client, server, or both.\n *\n * @param {ComponentProps} props  - The properties for the component.\n * @returns {JSX.Element} The JSX element representing the component.\n */\nexport function NextComponent(props: ComponentProps) {\n  // See if component is registered in current context\n  const { componentName, clientFallback, ...attributes } = props\n  const Component = getComponent(componentName)?.component\n\n  // Render component as is on the server\n  const Regular = React.useMemo<any>(() => <RegularComponent {...attributes} componentName={componentName} />, [])\n\n  // maintain identity of wrapped component\n  const External = React.useMemo<any>(\n    () =>\n      NextDynamicFunction(() => Promise.resolve(NextClientsideRenderer), {\n        ssr: false,\n        // Show server-rendered component during page load\n        loading: () => Regular\n      }),\n    []\n  )\n\n  // remove functions from props to avoid them to be passed from server to client which is illegal\n  var sanitizedAttributes = typeof window == 'undefined' ? JSON.parse(JSON.stringify(attributes)) : attributes\n\n  return (\n    <External\n      {...sanitizedAttributes}\n      componentName={componentName}\n      fallbackWrapper={!Component}\n      fallback={\n        Component\n          ? // If there's no client component, keep the server-rendered component displayed\n            Regular\n          : // wrap clientFallback into a dynamic() call to make it render clientside hydration\n          clientFallback\n          ? React.createElement(NextDynamicFunction(() => Promise.resolve(() => clientFallback), { ssr: false }))\n          : props.fallback\n      }\n    ></External>\n  )\n}\n\n/**\n * Enables Next.js client/server rendering options for registered components.\n * The `component` argument should be a function exported from a file with a `use client` directive.\n *\n * @param {any} dynamic    - The Next dynamic function.\n * @param {any} component  - The component function.\n * @returns {any} Augmented bundle.\n */\nexport function enableNextClientsideComponents(dynamic: any, component: any) {\n  NextDynamicFunction = dynamic\n  NextClientsideRenderer = component\n  return component\n}\n\n/**\n * Renders a registered external component in a React tree. If enableNextClientsideComponents was called previously,\n * the component is rendered using NextComponent\u00A0codepath, which allows rendering separate component on client and\n * server.\n *\n * @param {ComponentProps} props  - The properties for the component.\n * @returns {JSX.Element} The JSX element representing the component.\n */\nexport function Component(props: ComponentProps) {\n  if (Object.keys(props).length == 0) {\n    return <></>\n  }\n  // _dynamic property ensures the component does not endlessly loop in pre-app-router setup\n  if (NextDynamicFunction && !props._dynamic) {\n    return NextComponent({ _dynamic: true, ...props })\n  }\n  return RegularComponent(props)\n}\n\n/**\n * Pass list of server side component registrations to clients, and embed clientside components to the page.\n */\nexport function Bundle() {\n  return (\n    <>\n      <byoc-registration\n        components={JSON.stringify(Object.values(registered))}\n        datasources={JSON.stringify(Object.values(registeredDatasources))}\n        suppressHydrationWarning\n      />\n      {/** Will not be rendered, but needs to be referenced */}\n      {<NextClientsideRenderer />}\n    </>\n  )\n}\n\n/**\n * A wrapper over External component that accepts a fallback for rendering in case of a missing component.\n * It accepts properties in camelcase format and ensures no hydration errors on the client-side.\n *\n * @param {ComponentProps} props  - The properties for the component.\n * @returns {JSX.Element} The JSX element representing the component.\n */\nexport function RegularComponent(props: ComponentProps) {\n  const { componentName, fallback, fallbackWrapper } = props\n  const definition = getComponent(componentName)\n  const Component = definition?.component as any\n  const { attributes, properties, merged } = getMergedComponentProperties(props)\n  if ((!Component && fallback) || !componentName) {\n    if (fallbackWrapper === false) return <>{fallback}</>\n    return (\n      <feaas-external {...attributes} hydrate='false'>\n        {fallback}\n      </feaas-external>\n    )\n  }\n  if (definition && Component && isWebComponent(Component)) {\n    const webComponentName = 'byoc-' + toKebabCase(definition.id)\n    return React.createElement(webComponentName, {\n      ...attributes,\n      ref: (el: { sitecoreContextCallback: (context: typeof properties) => {} }) => {\n        if (el && typeof window != 'undefined') {\n          // web components can define sitecoreContextCallback defined that recieves props directly\n          window.customElements?.whenDefined(webComponentName).then(() => {\n            el.sitecoreContextCallback?.({ ...merged })\n          })\n        }\n      }\n    })\n  }\n  return (\n    <>\n      <feaas-external {...attributes} hydrate='false'>\n        {Component == null ? null : <Component {...merged} />}\n      </feaas-external>\n    </>\n  )\n}\ndeclare global {\n  namespace JSX {\n    interface IntrinsicElements {\n      'feaas-external': {\n        'data-external-id': string\n        children?: any\n        dangerouslySetInnerHTML?: { __html: string }\n        [key: string]: any\n      }\n    }\n  }\n}\n"],
  "mappings": "yvBAAA,IAAAA,EAQO,0BACPC,EAAkB,sBAClBC,EAAAC,EAAc,yXAEVC,EAA8B,KAC9BC,EAA2B,KAezB,SAAUC,EAAcC,EAAqB,OAEjD,GAAM,CAAE,cAAAC,EAAe,eAAAC,CAAc,EAAoBF,EAAfG,EAAUC,EAAKJ,EAAnD,CAAA,gBAAA,gBAAA,CAAgD,EAChDK,GAAYC,KAAA,gBAAaL,CAAa,KAAC,MAAAK,IAAA,OAAA,OAAAA,EAAE,UAGzCC,EAAU,EAAAC,QAAM,QAAa,IAAM,EAAAA,QAAA,cAACC,EAAgB,OAAA,OAAA,CAAA,EAAKN,EAAU,CAAE,cAAeF,CAAa,CAAA,CAAA,EAAM,CAAA,CAAE,EAGzGS,EAAW,EAAAF,QAAM,QACrB,IACEV,EAAoB,IAAM,QAAQ,QAAQD,CAAsB,EAAG,CACjE,IAAK,GAEL,QAAS,IAAMU,EAChB,EACH,CAAA,CAAE,EAIJ,IAAII,EAAsB,OAAO,QAAU,YAAc,KAAK,MAAM,KAAK,UAAUR,CAAU,CAAC,EAAIA,EAElG,OACE,EAAAK,QAAA,cAACE,EAAQ,OAAA,OAAA,CAAA,EACHC,EAAmB,CACvB,cAAeV,EACf,gBAAiB,CAACI,EAClB,SACEA,EAEIE,EAEFL,EACE,EAAAM,QAAM,cAAcV,EAAoB,IAAM,QAAQ,QAAQ,IAAMI,CAAc,EAAG,CAAE,IAAK,EAAK,CAAE,CAAC,EACpGF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cAAca,EAAgB,OAAA,OAAA,OAAA,OAAA,CAAA,EACtClB,CAAU,EAAA,CACb,IAAMmB,GAAuE,OACvEA,GAAM,OAAO,QAAU,eAEzBhB,EAAA,OAAO,kBAAc,MAAAA,IAAA,QAAAA,EAAE,YAAYe,CAAgB,EAAE,KAAK,IAAK,QAC7Df,EAAAgB,EAAG,2BAAuB,MAAAhB,IAAA,QAAAA,EAAA,KAAAgB,EAAA,OAAA,OAAA,CAAA,EAAQF,CAAM,CAAA,CAC1C,CAAC,EAEL,CAAC,CAAA,CAAA,CAEL,CACA,OACE,EAAAZ,QAAA,cAAA,EAAAA,QAAA,SAAA,KACE,EAAAA,QAAA,cAAA,iBAAA,OAAA,OAAA,CAAA,EAAoBL,EAAU,CAAE,QAAQ,OAAO,CAAA,EAC5CE,GAAa,KAAO,KAAO,EAAAG,QAAA,cAACH,EAAS,OAAA,OAAA,CAAA,EAAKe,CAAM,CAAA,CAAI,CACtC,CAGvB",
  "names": ["import_byoc", "import_react", "__reExport", "react_exports", "NextClientsideRenderer", "NextDynamicFunction", "NextComponent", "props", "componentName", "clientFallback", "attributes", "__rest", "Component", "_a", "Regular", "React", "RegularComponent", "External", "sanitizedAttributes", "enableNextClientsideComponents", "dynamic", "component", "Bundle", "fallback", "fallbackWrapper", "definition", "properties", "merged", "webComponentName", "el"]
}
