{"version":3,"file":"renderDefaultChildren.shared.mjs","names":[],"sources":["../../../src/utils/rendering/renderDefaultChildren.shared.ts"],"sourcesContent":["import React, { ReactNode } from 'react';\nimport {\n  getVariableProps,\n  isVariableElementProps,\n} from '../variables/_getVariableProps';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport { TaggedChild, TaggedChildren, TaggedElement } from '../types';\nimport { getGTTag } from './getGTTag';\nimport { getPluralBranch } from '../plurals/getPluralBranch';\nimport type { RenderVariable } from '../types';\n\n// Shared implementation: the variable renderer is dependency-injected by a\n// factory so neither code path statically imports the other's variable\n// components. Callsites use the pre-instantiated renderPipeline /\n// renderPipeline.rsc modules instead of importing this directly.\n\ntype RenderDefaultChildrenArgs = {\n  children: TaggedChildren;\n  defaultLocale: string;\n  enableI18n: boolean;\n};\n\nexport type { RenderDefaultChildrenArgs };\n\nfunction createRenderDefaultChildren({\n  renderVariable,\n}: {\n  renderVariable: RenderVariable;\n}): (args: RenderDefaultChildrenArgs) => React.ReactNode {\n  return function renderDefaultChildren({\n    children,\n    defaultLocale = libraryDefaultLocale,\n    enableI18n,\n  }: RenderDefaultChildrenArgs): React.ReactNode {\n    const handleSingleChildElement = (child: TaggedElement): ReactNode => {\n      const generaltranslation = getGTTag(child);\n\n      // Variable\n      if (isVariableElementProps(child.props)) {\n        const { variableType, variableValue, variableOptions, injectionType } =\n          getVariableProps(child.props);\n        return renderVariable({\n          variableType,\n          variableValue,\n          variableOptions,\n          locales: [defaultLocale],\n          enableI18n,\n          injectionType,\n        });\n      }\n\n      // Plural\n      if (generaltranslation?.transformation === 'plural') {\n        const branches = generaltranslation.branches || {};\n        if (typeof child.props.n !== 'number') {\n          return child.props.children != null\n            ? handleChildren(child.props.children)\n            : null;\n        }\n        const resolvedBranch = getPluralBranch(\n          child.props.n,\n          [defaultLocale],\n          branches\n        );\n        return handleChildren(\n          (resolvedBranch != null\n            ? resolvedBranch\n            : child.props.children) as TaggedChildren\n        );\n      }\n\n      // Branch\n      if (generaltranslation?.transformation === 'branch') {\n        const { children, branch } = child.props;\n        const branches = generaltranslation.branches || {};\n        const branchKey =\n          branch == null || branch === '' ? undefined : branch.toString();\n        return handleChildren(\n          branchKey && branches[branchKey] !== undefined\n            ? branches[branchKey]\n            : children\n        );\n      }\n\n      // Fragment\n      if (generaltranslation?.transformation === 'fragment') {\n        return React.createElement(React.Fragment, {\n          key: child.props.key,\n          children: handleChildren(child.props.children),\n        });\n      }\n\n      // Default\n      if (child.props.children) {\n        return React.cloneElement(child, {\n          ...child.props,\n          'data-_gt': undefined,\n          children: handleChildren(child.props.children) as TaggedChildren,\n        });\n      }\n      return React.cloneElement(child, {\n        ...child.props,\n        'data-_gt': undefined,\n      });\n    };\n\n    const handleSingleChild = (child: TaggedChild): ReactNode => {\n      if (React.isValidElement(child)) {\n        return handleSingleChildElement(child);\n      }\n      return child;\n    };\n\n    const handleChildren = (children: TaggedChildren): ReactNode => {\n      return Array.isArray(children)\n        ? React.Children.map(children, handleSingleChild)\n        : handleSingleChild(children);\n    };\n\n    return handleChildren(children);\n  };\n}\n\nexport { createRenderDefaultChildren };\n"],"mappings":"qSAwBA,SAAS,EAA4B,CACnC,kBAGuD,CACvD,OAAO,SAA+B,CACpC,WACA,gBAAgB,EAChB,cAC6C,CAC7C,IAAM,EAA4B,GAAoC,CACpE,IAAM,EAAqB,EAAS,EAAM,CAG1C,GAAI,EAAuB,EAAM,MAAM,CAAE,CACvC,GAAM,CAAE,eAAc,gBAAe,kBAAiB,iBACpD,EAAiB,EAAM,MAAM,CAC/B,OAAO,EAAe,CACpB,eACA,gBACA,kBACA,QAAS,CAAC,EAAc,CACxB,aACA,gBACD,CAAC,CAIJ,GAAI,GAAoB,iBAAmB,SAAU,CACnD,IAAM,EAAW,EAAmB,UAAY,EAAE,CAWlD,OAVI,OAAO,EAAM,MAAM,GAAM,SAUtB,EALgB,EACrB,EAAM,MAAM,EACZ,CAAC,EAAc,CACf,EAGC,EAEG,EAAM,MAAM,SACjB,CAbQ,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