{"version":3,"file":"createServerEntry.cjs","names":["Fragment","Render"],"sources":["../../src/createEntry/createServerEntry.tsx"],"sourcesContent":["import { cloneElement, isValidElement, Fragment } from 'react';\nimport { renderToStaticMarkup, renderToString } from 'react-dom/server';\nimport { normalizeAsyncQueue } from '@pastweb/tools/createAsyncStore/normalizeAsyncQueue';\nimport { createEntry } from '@pastweb/tools/createEntry';\nimport { Render } from '../Render';\nimport type { ReactEntry, ReactEntryOptions } from './types';\n\nfunction getRenderComponent(entry: ReactEntry) {\n  const { Providers = Fragment, initData = {} } = entry.options;\n  const { EntryComponent } = entry;\n\n  const RootComponent = (initData: { [propName: string]: any }) => (\n    <Providers>\n      {isValidElement(EntryComponent) ?\n        cloneElement(EntryComponent, initData)\n        : <EntryComponent {...initData} />\n      }\n    </Providers>\n  );\n\n  const RenderComponent = () => (  \n    <RootComponent {...initData} />\n  );\n\n  return RenderComponent;\n}\n\n/**\n * Creates a server-side entry object with methods for rendering React components to static markup\n * or to a string, useful for server-side rendering (SSR).\n *\n * @param options - Configuration options for the React entry.\n * @param options.Providers - Optional provider components to wrap the entry component.\n * @param options.initData - Initial data to be passed as props to the entry component.\n * @param options.waitFor - Optional array of promises to wait for before rendering the entry component.\n * @param options.fallback - Optional fallback component to be rendered while waiting.\n *\n * @returns A `ReactEntry` object with methods for server-side rendering.\n * - `mount`: A method to render the entry component to a static markup or a string based on `isStatic`. \n *   If `isStatic` is true, it uses `renderToStaticMarkup`, otherwise it uses `renderToString`.\n *\n * @remarks\n * - The `getRenderComponent` function generates a component that includes the necessary providers, \n *   the entry component itself, and handles the `waitFor` and `fallback` props.\n * - `mount` method supports server-side rendering (SSR) by generating either static markup or a string \n *   representation of the rendered component.\n * \n * @example\n * // Example usage:\n * const serverEntry = createServerEntry({\n *   EntryComponent: MyComponent,\n *   Providers: MyProviders,\n *   initData: { someProp: 'value' },\n *   waitFor: [fetchData()],\n *   fallback: <LoadingSpinner />,\n * });\n * \n * const htmlString = serverEntry.mount({ isStatic: true });\n * // `htmlString` will contain the static markup of the rendered component.\n */\nexport function createServerEntry(options: ReactEntryOptions = {}): ReactEntry {\n  const entry = createEntry(options) as ReactEntry;\n  const { waitFor = [], fallback = Fragment, EntryComponent } = options;\n  const queue = normalizeAsyncQueue(waitFor);\n\n  entry.mount = (options = {}): void | string => {\n    const { isStatic = true } = options;\n\n    if (!EntryComponent) throw new Error('EntryComponent is required in createServerEntry options.');\n    \n    const RenderComponent = getRenderComponent(entry);\n    \n    const render = async () => {\n      let hasError = false;\n      \n      try {\n        if (queue.length) await Promise.all(queue);\n      } catch (error) {\n        console.error(error);\n        hasError = true;\n      }\n\n      if (isStatic) {\n        return renderToStaticMarkup(hasError ? <Render content={fallback} /> : <RenderComponent />);\n      }\n\n      return renderToString(hasError ? <Render content={fallback} /> : <RenderComponent />);\n    };\n\n    entry.memoSSR(render);\n\n    return entry.ssrId as string;\n  };\n\n  return entry as ReactEntry;\n}\n"],"mappings":";;;;;;;;AAOA,SAAS,mBAAmB,OAAmB;CAC7C,MAAM,EAAE,YAAYA,MAAAA,UAAU,WAAW,CAAC,MAAM,MAAM;CACtD,MAAM,EAAE,mBAAmB;CAE3B,MAAM,iBAAiB,aACrB,iBAAA,GAAA,kBAAA,IAAA,CAAC,WAAD,EAAA,WAAA,GAAA,MAAA,eAAA,CACkB,cAAc,KAAA,GAAA,MAAA,aAAA,CACf,gBAAgB,QAAQ,IACnC,iBAAA,GAAA,kBAAA,IAAA,CAAC,gBAAD,EAAgB,GAAI,SAAW,CAAA,EAE1B,CAAA;CAGb,MAAM,wBACJ,iBAAA,GAAA,kBAAA,IAAA,CAAC,eAAD,EAAe,GAAI,SAAW,CAAA;CAGhC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,kBAAkB,UAA6B,CAAC,GAAe;CAC7E,MAAM,SAAA,GAAA,2BAAA,YAAA,CAAoB,OAAO;CACjC,MAAM,EAAE,UAAU,CAAC,GAAG,WAAWA,MAAAA,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