{"version":3,"file":"addGTIdentifier.mjs","names":[],"sources":["../../../src/utils/internal/addGTIdentifier.ts"],"sourcesContent":["import React, { ReactElement, isValidElement, ReactNode } from 'react';\nimport { isAcceptedPluralForm } from 'generaltranslation/internal';\nimport {\n  GTTag,\n  TaggedChild,\n  TaggedChildren,\n  TaggedElement,\n  TaggedElementProps,\n} from '../types';\nimport {\n  Transformation,\n  TransformationPrefix,\n  VariableTransformationSuffix,\n} from 'generaltranslation/types';\n\ntype GTComponentType = {\n  _gtt?: Transformation;\n};\n\nexport function addGTIdentifier(\n  children: ReactNode,\n  startingIndex: number = 0\n): TaggedChildren {\n  // Object to keep track of the current index for GT IDs\n  let index = startingIndex;\n\n  /**\n   * Function to create a GTTag object for a ReactElement\n   * @param child - The ReactElement for which the GTTag is created\n   * @returns - The GTTag object\n   */\n  const createGTTag = (child: ReactElement<Record<string, unknown>>): GTTag => {\n    const { type, props } = child;\n    index += 1;\n    const result: GTTag = { id: index, injectionType: 'manual' };\n    let transformation: Transformation | undefined;\n    try {\n      transformation =\n        typeof type === 'function' ? (type as GTComponentType)._gtt : undefined;\n    } catch {\n      /* empty */\n    }\n    if (transformation) {\n      const transformationParts = transformation.split('-');\n      // If the component was inserted automatically by the compiler\n      if (\n        transformationParts[1] === 'automatic' ||\n        transformationParts[2] === 'automatic'\n      ) {\n        result.injectionType = 'automatic';\n      }\n\n      if (transformationParts[0] === 'translate') {\n        // Convert nested <T> to fragments\n        // This will nullify translation specific attributes of child, i.e. id, context, etc.\n        transformationParts[0] = 'fragment';\n      }\n      if (transformationParts[0] === 'variable') {\n        result.variableType =\n          (transformationParts?.[1] as VariableTransformationSuffix) ||\n          'variable';\n      }\n      if (transformationParts[0] === 'plural') {\n        const pluralBranches = Object.entries(props).reduce(\n          (acc, [branchName, branch]) => {\n            if (isAcceptedPluralForm(branchName)) {\n              (acc as Record<string, TaggedChildren>)[branchName] =\n                addGTIdentifier(branch as ReactNode, index);\n            }\n            return acc;\n          },\n          {} as Record<string, TaggedChildren>\n        );\n        if (Object.keys(pluralBranches).length)\n          result.branches = pluralBranches;\n      }\n      if (transformationParts[0] === 'branch') {\n        const { children: _children, branch: _branch, ...branches } = props;\n        // Filter out data-* attributes injected by build tools\n        const filteredBranches = Object.fromEntries(\n          Object.entries(branches).filter(([key]) => !key.startsWith('data-'))\n        );\n        const resultBranches = Object.entries(filteredBranches).reduce(\n          (acc, [branchName, branch]) => {\n            (acc as Record<string, TaggedChildren>)[branchName] =\n              addGTIdentifier(branch as ReactNode, index);\n            return acc;\n          },\n          {} as Record<string, TaggedChildren>\n        );\n        if (Object.keys(resultBranches).length)\n          result.branches = resultBranches;\n      }\n      result.transformation = transformationParts[0] as TransformationPrefix;\n    }\n    return result;\n  };\n\n  function handleSingleChildElement(\n    child: ReactElement<Record<string, unknown>>\n  ): TaggedElement {\n    const { props } = child;\n\n    // Create new props for the element, including the GT identifier and a key\n    const generaltranslation: GTTag = createGTTag(child);\n    const newProps: TaggedElementProps = {\n      ...props,\n      'data-_gt': generaltranslation,\n    };\n    if (props.children && !generaltranslation.variableType) {\n      newProps.children = handleChildren(props.children as ReactNode);\n    }\n    if (child.type === React.Fragment) {\n      newProps['data-_gt'].transformation = 'fragment';\n    }\n    return React.cloneElement(child, newProps) as TaggedElement;\n  }\n\n  function handleSingleChild(child: ReactNode): TaggedChild {\n    if (isValidElement(child)) {\n      return handleSingleChildElement(\n        child as ReactElement<Record<string, unknown>>\n      );\n    }\n    return child;\n  }\n\n  function handleChildren(children: ReactNode): TaggedChildren {\n    if (Array.isArray(children)) {\n      return React.Children.map(children, handleSingleChild);\n    } else {\n      return handleSingleChild(children);\n    }\n  }\n\n  return handleChildren(children);\n}\n"],"mappings":"6GAmBA,SAAgB,EACd,EACA,EAAwB,EACR,CAEhB,IAAI,EAAQ,EAON,EAAe,GAAwD,CAC3E,GAAM,CAAE,OAAM,SAAU,EACxB,GAAS,EACT,IAAM,EAAgB,CAAE,GAAI,EAAO,cAAe,SAAU,CACxD,EACJ,G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