{"version":3,"file":"EntryAdapter.cjs","names":["useIsland","isServer","renderServerEntryAdapter"],"sources":["../../src/EntryAdapter/EntryAdapter.tsx"],"sourcesContent":["import { forwardRef, useCallback, useEffect, useRef } from 'react';\nimport { isServer } from '@pastweb/tools/envs';\nimport { isEntry } from '@pastweb/tools/createEntry';\nimport { useBeforeMount } from '../useBeforeMount';\nimport { useBeforeUnmount } from '../useBeforeUnmount';\nimport { useIsland } from '../Island';\nimport { setRef } from '../setRef';\nimport { renderServerEntryAdapter } from './utils';\nimport type { EntryAdapterProps, GenericEntry } from './types';\n\n/**\n * The `EntryAdapter` component serves as a bridge to integrate `Entry` objects with React components.\n * It handles mounting, updating, and unmounting of entry elements and supports server-side rendering (SSR) if applicable.\n *\n * @param props - The props for the `EntryAdapter` component.\n * @param props.entry - A function that returns an `Entry` object. This is used to interact with the entry system.\n * @param props.ssrEntry - Optional async server-only entry loader, resolved through the SSR async task queue.\n * @param rest - Any additional props to be passed to the entry's `mergeOptions` method.\n *\n * @returns A `div` element with a `ref` attached for mounting the entry, or an SSR ID if server-side rendering is enabled.\n *\n * @throws When `entry` or the resolved `ssrEntry` is not a valid Pastweb entry.\n *\n * @example\n * ```tsx\n * import { createEntry, EntryAdapter } from '@pastweb/react';\n *\n * const createClientEntry = () => createEntry({ EntryComponent: Widget });\n *\n * <EntryAdapter\n *   entry={createClientEntry}\n *   {...(import.meta.env.SSR\n *     ? {\n *         ssrEntry: () =>\n *           import('./widget.server-entry').then(module => module.createServerEntry()),\n *       }\n *     : {})}\n *   widgetId=\"main\"\n * />\n * ```\n */\nexport const EntryAdapter = forwardRef<HTMLDivElement, EntryAdapterProps>(function EntryAdapter(props, forwardedRef) {\n  const { entry: _entry, ssrEntry: _ssrEntry, ...rest } = props;\n  const isIsland = useIsland();\n  const entry = useRef<GenericEntry>(_entry());\n  const entryElement = useRef<HTMLDivElement | null>(null);\n\n  if (isServer) {\n    return renderServerEntryAdapter(entry.current, _ssrEntry, rest);\n  }\n\n  useBeforeMount(() => {\n    if (!isEntry(entry.current)) {\n      throw Error('The entry must be generate via \"createEntry\" function.');\n    }\n\n    entry.current.mergeOptions({ initData: { ...rest } });\n  });\n\n  const ref = useCallback((node: HTMLDivElement | null) => {\n    if (node) {\n      entryElement.current = node;\n      entry.current.setEntryElement(entryElement.current);\n      entry.current.emit('mount', { hydrate: isIsland });\n    }\n    setRef(forwardedRef, node);\n  }, [forwardedRef, isIsland]);\n\n  useEffect(() => entry.current.emit('update', rest), [rest]);\n\n  useBeforeUnmount(() => queueMicrotask(() => entry.current.emit('unmount')));\n\n  return <div ref={ref} style={{ display: 'contents' }} />;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,MAAa,gBAAA,GAAA,MAAA,WAAA,CAA6D,SAAS,aAAa,OAAO,cAAc;CACnH,MAAM,EAAE,OAAO,QAAQ,UAAU,WAAW,GAAG,SAAS;CACxD,MAAM,WAAWA,yBAAAA,UAAU;CAC3B,MAAM,SAAA,GAAA,MAAA,OAAA,CAA6B,OAAO,CAAC;CAC3C,MAAM,gBAAA,GAAA,MAAA,OAAA,CAA6C,IAAI;CAEvD,IAAIC,oBAAAA,UACF,OAAOC,2BAAAA,yBAAyB,MAAM,SAAS,WAAW,IAAI;CAGhE,sCAAA,qBAAqB;EACnB,IAAI,EAAA,GAAA,2BAAA,QAAA,CAAS,MAAM,OAAO,GACxB,MAAM,MAAM,0DAAwD;EAGtE,MAAM,QAAQ,aAAa,EAAE,UAAU,EAAE,GAAG,KAAK,EAAE,CAAC;CACtD,CAAC;CAED,MAAM,OAAA,GAAA,MAAA,YAAA,EAAmB,SAAgC;EACvD,IAAI,MAAM;GACR,aAAa,UAAU;GACvB,MAAM,QAAQ,gBAAgB,aAAa,OAAO;GAClD,MAAM,QAAQ,KAAK,SAAS,EAAE,SAAS,SAAS,CAAC;EACnD;EACA,sBAAA,OAAO,cAAc,IAAI;CAC3B,GAAG,CAAC,cAAc,QAAQ,CAAC;CAE3B,CAAA,GAAA,MAAA,UAAA,OAAgB,MAAM,QAAQ,KAAK,UAAU,IAAI,GAAG,CAAC,IAAI,CAAC;CAE1D,0CAAA,uBAAuB,qBAAqB,MAAM,QAAQ,KAAK,SAAS,CAAC,CAAC;CAE1E,OAAO,iBAAA,GAAA,kBAAA,IAAA,CAAC,OAAD;EAAU;EAAK,OAAO,EAAE,SAAS,WAAW;CAAI,CAAA;AACzD,CAAC"}