{"version":3,"file":"renderPreparedT.shared.mjs","names":[],"sources":["../../../src/utils/rendering/renderPreparedT.shared.ts"],"sourcesContent":["import type { RenderDefaultChildrenArgs } from './renderDefaultChildren.shared';\nimport type { RenderTranslatedChildrenArgs } from './renderTranslatedChildren.shared';\nimport type { JsxChildren } from 'generaltranslation/types';\nimport {\n  cloneElement,\n  createElement,\n  Fragment,\n  isValidElement,\n  type ReactElement,\n  type ReactNode,\n} from 'react';\nimport type { TaggedChildren } from '../types';\nimport { idTaggingEnabled } from '../translation/resolveTagHash';\n\n// Opt-in id-tagging: when a `hash` is provided, the translation hash is exposed\n// on the DOM as `data-_gt-hash` so tooling can map a rendered node back to its\n// published translation.\n//\n// Span injection is kept to the minimum necessary: when the <T> renders a single\n// host element we put the attribute directly on THAT element (no wrapper), so <T>\n// keeps copying the source 1:1. Only when there is no element to carry it — bare\n// text, a fragment, or a component root — do we inject a layout-neutral span\n// (`display: contents`, so it generates no box and children lay out as before).\n//\n// DOM-only: skipped on React Native (there is no `span`/DOM attribute analog).\n// The rest of this module stays platform-agnostic; this is a runtime guard, not a\n// native import.\nconst TAG_STYLE = { display: 'contents' } as const;\n// Canonical React Native detection (RN sets navigator.product = 'ReactNative').\n// False in the browser AND in Node SSR/RSC, so the span is still emitted server-\n// side (where the harvest reads it).\nconst IS_REACT_NATIVE =\n  (globalThis as { navigator?: { product?: string } }).navigator?.product ===\n  'ReactNative';\n\n// Whether a rendered node produces NO DOM — so id-tagging must leave it untouched\n// (wrapping it in a hash span would create an empty <span>, altering :empty/child\n// structure and injecting an invalid span under restricted parents). Covers the\n// scalar non-renderers (null / undefined / booleans / ''), arrays whose every\n// element renders nothing (`items.map(...)` over an empty list, `[null, false]`,\n// nested empties — TaggedChildren accepts arrays), and empty React fragments.\n// NB: 0 and NaN DO render text, so they are intentionally NOT treated as empty.\nfunction rendersNothing(node: ReactNode): boolean {\n  if (node == null || typeof node === 'boolean' || node === '') return true;\n  if (Array.isArray(node)) return !node.some((n) => !rendersNothing(n));\n  if (isValidElement(node) && node.type === Fragment) {\n    const children = (node.props as { children?: ReactNode }).children;\n    return children == null || rendersNothing(children);\n  }\n  return false;\n}\n\n// Opt-in id-tagging (powers RRWeb localized replay / in-context tooling): expose\n// the translation hash on the DOM as `data-_gt-hash`. Factored out of the render\n// path so it's clear this whole block is dedicated to id-tagging, not core <T>\n// behavior. Called only when a `hash` was produced (see resolveTagHash).\nfunction applyIdTag(rendered: ReactNode, hash: string): ReactNode {\n  // `hash` is only produced when tagging is on, but re-assert the config here so a\n  // stray hash can never inject DOM; and never on React Native (no span/DOM attr\n  // analog — IS_REACT_NATIVE is checked first so the config isn't read there).\n  if (IS_REACT_NATIVE || !idTaggingEnabled()) return rendered;\n  // Single host element (e.g. `<T><td>…</td></T>`) → annotate it directly, no\n  // wrapper. Valid wherever the element itself is valid — including inside\n  // <tr>/<select>/<ul> — and avoids the span entirely.\n  if (isValidElement(rendered) && typeof rendered.type === 'string') {\n    return cloneElement(rendered as ReactElement<Record<string, unknown>>, {\n      'data-_gt-hash': hash,\n    });\n  }\n  // Output that renders no DOM stays no-DOM: an empty <span data-_gt-hash> would\n  // change :empty/child structure and inject an (invalid, under restricted\n  // parents) span even for an empty branch.\n  if (rendersNothing(rendered)) return rendered;\n  // No element to carry the attribute (bare text / fragment / component root) →\n  // wrap in a layout-neutral span. This is the only case that injects one.\n  return createElement(\n    'span',\n    { 'data-_gt-hash': hash, style: TAG_STYLE },\n    rendered\n  );\n}\n\n// Shared rendering logic. The child renderers are injected so the RSC code path\n// never statically imports the hook-based variable components, and so the\n// pipeline can build them once and share them. This module must stay free of\n// hook/context imports so it can be reached from the components-rsc entrypoint.\n\ntype RenderPreparedTParams = {\n  taggedSourceChildren: TaggedChildren;\n  targetJsxChildren: JsxChildren | null | undefined;\n  locale: string;\n  defaultLocale: string;\n  enableI18n: boolean;\n  shouldTranslate: boolean;\n  // The translation hash for this <T> (see resolveTagHash) — the same key the\n  // published-translation cache/CDN is keyed by. Set only when id-tagging is\n  // enabled; when undefined the output is rendered untouched. Presence of `hash`\n  // is the single signal to tag (there is no separate boolean).\n  hash?: string;\n};\n\nfunction createRenderPreparedT({\n  renderDefaultChildren,\n  renderTranslatedChildren,\n}: {\n  renderDefaultChildren: (args: RenderDefaultChildrenArgs) => ReactNode;\n  renderTranslatedChildren: (args: RenderTranslatedChildrenArgs) => ReactNode;\n}): (args: RenderPreparedTParams) => ReactNode {\n  function renderPreparedT({\n    taggedSourceChildren,\n    targetJsxChildren,\n    locale,\n    defaultLocale,\n    enableI18n,\n    shouldTranslate,\n    hash,\n  }: RenderPreparedTParams): ReactNode {\n    const rendered =\n      !shouldTranslate || targetJsxChildren == null\n        ? renderSource({\n            taggedSourceChildren,\n            defaultLocale,\n            enableI18n,\n          })\n        : renderTarget({\n            taggedSourceChildren,\n            targetJsxChildren,\n            locales: [locale, defaultLocale],\n            enableI18n,\n          });\n\n    // Opt-in id-tagging is entirely handled by applyIdTag; `hash` presence is the\n    // enable signal (produced by resolveTagHash only when tagging is on).\n    return hash ? applyIdTag(rendered, hash) : rendered;\n  }\n\n  function renderSource({\n    taggedSourceChildren,\n    defaultLocale,\n    enableI18n,\n  }: {\n    taggedSourceChildren: TaggedChildren;\n    defaultLocale: string;\n    enableI18n: boolean;\n  }): ReactNode {\n    return renderDefaultChildren({\n      children: taggedSourceChildren,\n      defaultLocale,\n      enableI18n,\n    });\n  }\n\n  function renderTarget({\n    taggedSourceChildren,\n    targetJsxChildren,\n    locales,\n    enableI18n,\n  }: {\n    taggedSourceChildren: TaggedChildren;\n    targetJsxChildren: JsxChildren;\n    locales: string[];\n    enableI18n: boolean;\n  }): ReactNode {\n    return renderTranslatedChildren({\n      source: taggedSourceChildren,\n      // The shared persisted wire also admits empty-rendering boolean/null\n      // values. The outer null check and existing renderer fallback own those\n      // cases; this narrower internal renderer type describes its recursive\n      // element/string grammar without changing React runtime behavior.\n      target: targetJsxChildren as RenderTranslatedChildrenArgs['target'],\n      locales,\n      enableI18n,\n    });\n  }\n\n  return renderPreparedT;\n}\n\nexport { createRenderPreparedT };\nexport type { RenderPreparedTParams };\n"],"mappings":"+JA2BA,MAAM,EAAY,CAAE,QAAS,WAAY,CAInC,EACH,WAAoD,WAAW,UAChE,cASF,SAAS,EAAe,EAA0B,CAChD,GAAI,GAAQ,MAAQ,OAAO,GAAS,WAAa,IAAS,GAAI,MAAO,GACrE,GAAI,MAAM,QAAQ,EAAK,CAAE,MAAO,CAAC,EAAK,KAAM,GAAM,CAAC,EAAe,EAAE,CAAC,CACrE,GAAI,EAAe,EAAK,EAAI,EAAK,OAAS,EAAU,CAClD,IAAM,EAAY,EAAK,MAAmC,SAC1D,OAAO,GAAY,MAAQ,EAAe,EAAS,CAErD,MAAO,GAOT,SAAS,EAAW,EAAqB,EAAyB,CAmBhE,OAfI,GAAmB,CAAC,GAAkB,CAAS,EAI/C,EAAe,EAAS,EAAI,OAAO,EAAS,MAAS,SAChD,EAAa,EAAmD,CACrE,gBAAiB,EAClB,CAAC,CAKA,EAAe,EAAS,CAAS,EAG9B,EACL,OACA,CAAE,gBAAiB,EAAM,MAAO,EAAW,CAC3C,EACD,CAsBH,SAAS,EAAsB,CAC7B,wBACA,4BAI6C,CAC7C,SAAS,EAAgB,CACvB,uBACA,oBACA,SACA,gBACA,aACA,kBACA,QACmC,CACnC,IAAM,EACJ,CAAC,GAAmB,GAAqB,KACrC,EAAa,CACX,uBACA,gBACA,aACD,CAAC,CACF,EAAa,CACX,uBACA,oBACA,QAAS,CAAC,EAAQ,EAAc,CAChC,aACD,CAAC,CAIR,OAAO,EAAO,EAAW,EAAU,EAAK,CAAG,EAG7C,SAAS,EAAa,CACpB,uBACA,gBACA,cAKY,CACZ,OAAO,EAAsB,CAC3B,SAAU,EACV,gBACA,aACD,CAAC,CAGJ,SAAS,EAAa,CACpB,uBACA,oBACA,UACA,cAMY,CACZ,OAAO,EAAyB,CAC9B,OAAQ,EAKR,OAAQ,EACR,UACA,aACD,CAAC,CAGJ,OAAO"}