{"version":3,"file":"removeInjectedT.mjs","names":[],"sources":["../../../src/utils/internal/removeInjectedT.ts"],"sourcesContent":["import { isAcceptedPluralForm } from 'generaltranslation/internal';\nimport { InjectionType, TransformationPrefix } from 'generaltranslation/types';\nimport {\n  ReactNode,\n  ReactElement,\n  isValidElement,\n  cloneElement,\n  Children,\n} from 'react';\n\n/**\n * Remove injected _T components at runtime. This is only for i18n-context T components to use.\n * This is necessary because when dealing with deriving fragmented content. The compiler will always\n * inject a `_T` component.\n *\n * Only remove if within a `<Derive>` component as this scopes this behavior\n * to only where it can actually appear.\n */\nexport function removeInjectedT(children: ReactNode): ReactNode {\n  return handleChildren(children, 0);\n}\n\n// ----- Core Logic ----- //\n\n/**\n * Traverses a single child element and removes the injected _T component.\n * @param child - The child element to traverse.\n * @param derivationDepth - The depth of the derivation.\n * @returns The traversed child element.\n *\n * Derivation depth is used for tracking whether or not to apply the _T removal transformation.\n *\n * Rules:\n * 1. Variable components (Var, Num, Currency, DateTime) - hands off\n * 2. Branching components (Branch, Plural) - explore respective branches\n * 3. Derivation components (Derive) - add/remove derivation depth\n * 4. Translation components (T) - remove _T if within a derivation context\n * 5. Then move on to processing the element's children\n */\nfunction handleSingleChildElement(\n  child: ReactElement,\n  derivationDepth: number\n): ReactNode {\n  const { type: elementType, props: elementProps } = child;\n  const transformation = getTransformation(elementType);\n  // unlikely edge case: encountered an element with props that cannot be processed\n  if (typeof elementProps !== 'object' || elementProps === null) {\n    return child;\n  }\n\n  if (transformation) {\n    const { componentType, injectionType } = transformation;\n\n    // (1) If the element is a variable component, hands off\n    if (componentType === 'variable') {\n      return child;\n    }\n\n    // (2) If the element is a branching component, explore respective branches\n    else if (componentType === 'branch') {\n      // Traverse into each branch (this also includes the children property)\n      const newProps = Object.entries(elementProps).reduce<\n        Record<string, unknown>\n      >((acc, [branchName, branch]) => {\n        if (branchName !== 'branch' && !branchName.startsWith('data-')) {\n          acc[branchName] = handleSingleChild(branch, derivationDepth);\n        } else {\n          // Skip recursion on non-translated branches\n          acc[branchName] = branch;\n        }\n        return acc;\n      }, {});\n\n      return cloneElement(child, {\n        ...newProps,\n      });\n    } else if (componentType === 'plural') {\n      // Traverse into each branch (this also includes the children property)\n      const newProps = Object.entries(elementProps).reduce<\n        Record<string, unknown>\n      >((acc, [branchName, branch]) => {\n        if (isAcceptedPluralForm(branchName) || branchName === 'children') {\n          acc[branchName] = handleSingleChild(branch, derivationDepth);\n        } else {\n          // Skip Recursion on non-translated branches\n          acc[branchName] = branch;\n        }\n        return acc;\n      }, {});\n\n      return cloneElement(child, {\n        ...newProps,\n      });\n    }\n\n    // (3) If the element is a derivation component, add/remove derivation depth\n    else if (componentType === 'derive') {\n      return cloneElement(child, {\n        ...elementProps,\n        ...('children' in elementProps && {\n          children: handleChildren(\n            elementProps.children as ReactNode,\n            derivationDepth + 1\n          ),\n        }),\n      });\n    }\n\n    // (4) If the element is a translation component, remove _T if within a derivation context, just return the children\n    else if (\n      componentType === 'translate' &&\n      injectionType === 'automatic' &&\n      derivationDepth > 0\n    ) {\n      return 'children' in elementProps\n        ? handleChildren(elementProps.children as ReactNode, derivationDepth)\n        : undefined;\n    }\n\n    // Note: componentType === 'translate': means that there is a <_T> inside of a <T>/<_T>\n    else if (componentType === 'translate' && injectionType === 'automatic') {\n      console.warn(warnNestedInternalTComponent);\n    }\n  }\n\n  // (5) Recurse into children\n  return cloneElement(child, {\n    ...elementProps,\n    ...('children' in elementProps && {\n      children: handleChildren(\n        elementProps.children as ReactNode,\n        derivationDepth\n      ),\n    }),\n  });\n}\n\n// ----- Traversal ----- //\n\n/**\n * Traverses a single child react node and removes the injected _T component.\n */\nfunction handleSingleChild(\n  child: ReactNode,\n  derivationDepth: number\n): ReactNode {\n  if (isValidElement(child)) {\n    return handleSingleChildElement(child, derivationDepth);\n  }\n  return child;\n}\n\n/**\n * Traverses an array of children and removes the injected _T component.\n *\n */\nfunction handleChildren(\n  children: ReactNode,\n  derivationDepth: number\n): ReactNode {\n  if (Array.isArray(children)) {\n    return Children.map(children, (child) =>\n      handleSingleChild(child, derivationDepth)\n    );\n  }\n  return handleSingleChild(children, derivationDepth);\n}\n\n// ----- Helper Functions ----- //\n\n/**\n * Extracts the transformation from the element type.\n * @param elementType - The element type to extract the transformation from.\n * @returns The transformation.\n */\nfunction getTransformation(elementType: ReactElement['type']):\n  | {\n      componentType: TransformationPrefix;\n      injectionType: InjectionType;\n    }\n  | undefined {\n  // Extract transformation string\n  const transformation =\n    typeof elementType === 'function' && '_gtt' in elementType\n      ? elementType._gtt\n      : undefined;\n  if (transformation == null || typeof transformation !== 'string')\n    return undefined;\n\n  // Extract metadata from transformation string\n  const parts = transformation.split('-');\n  const componentType = parts[0] as TransformationPrefix;\n  const injectionType =\n    parts[1] === 'automatic' || parts[2] === 'automatic'\n      ? 'automatic'\n      : 'manual';\n\n  return {\n    componentType,\n    injectionType,\n  };\n}\n\nconst warnNestedInternalTComponent = `'@generaltranslation/react-core Warning: A <_T> component was found injected outside of a <Derive> boundary. This may affect translation resolution for this component.`;\n"],"mappings":"0IAkBA,SAAgB,EAAgB,EAAgC,CAC9D,OAAO,EAAe,EAAU,EAAE,CAoBpC,SAAS,EACP,EACA,EACW,CACX,GAAM,CAAE,KAAM,EAAa,MAAO,GAAiB,EAC7C,EAAiB,EAAkB,EAAY,CAErD,GAAI,OAAO,GAAiB,WAAY,EACtC,OAAO,EAGT,GAAI,EAAgB,CAClB,GAAM,CAAE,gBAAe,iBAAkB,EAGzC,GAAI,IAAkB,WACpB,OAAO,KAIA,IAAkB,SAczB,OAAO,EAAa,EAAO,CACzB,GAbe,OAAO,QAAQ,EAAa,CAAC,QAE3C,EAAK,CAAC,EAAY,MACf,IAAe,UAAY,CAAC,EAAW,WAAW,QAAQ,CAC5D,EAAI,GAAc,EAAkB,EAAQ,EAAgB,CAG5D,EAAI,GAAc,EAEb,GACN,EAAE,CAGQ,CACZ,CAAC,IACO,IAAkB,SAc3B,OAAO,EAAa,EAAO,CACzB,GAbe,OAAO,QAAQ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