{"version":3,"file":"writeChildrenAsObjects.cjs","names":["HTML_CONTENT_PROPS","getVariableName","isValidTaggedElement"],"sources":["../../../src/utils/internal/writeChildrenAsObjects.ts"],"sourcesContent":["import { getVariableName } from '../variables/getVariableName';\nimport { TaggedChild, TaggedChildren, TaggedElement } from '../../utils/types';\nimport { isValidTaggedElement } from '../../utils/utils';\nimport { minifyVariableType } from 'generaltranslation/internal';\nimport {\n  GTProp,\n  HTML_CONTENT_PROPS,\n  HtmlContentPropKeysRecord,\n  JsxChild,\n  JsxChildren,\n  JsxElement,\n  Variable,\n} from '@generaltranslation/format/types';\nimport type { Transformation } from 'generaltranslation/types';\n\n/**\n * Gets the tag name of a React element.\n * @param {ReactElement} child - The React element.\n * @returns {string} - The tag name of the React element.\n */\nconst getTagName = (child: TaggedElement): string => {\n  if (!child) return '';\n  const { type, props } = child;\n  if (type && typeof type === 'function') {\n    if (\n      'displayName' in type &&\n      typeof type.displayName === 'string' &&\n      type.displayName\n    )\n      return type.displayName;\n    if ('name' in type && typeof type.name === 'string' && type.name)\n      return type.name;\n  }\n  if (type && typeof type === 'string') return type;\n  if (props.href) return 'a';\n  if (props['data-_gt']?.id) return `C${props['data-_gt'].id}`;\n  return 'function';\n};\nconst createGTProp = (\n  transformation: Transformation,\n  props: Record<string, unknown>,\n  branches?: Record<string, TaggedChildren>\n): GTProp | undefined => {\n  // Add translatable HTML content props\n  let newGTProp: GTProp = Object.entries(HTML_CONTENT_PROPS).reduce<GTProp>(\n    (acc, [minifiedName, fullName]) => {\n      const value = props[fullName];\n      if (typeof value === 'string') {\n        acc[minifiedName as keyof HtmlContentPropKeysRecord] = value;\n      }\n      return acc;\n    },\n    {}\n  );\n\n  // Check if plural or branch\n  if (\n    (transformation === 'plural' || transformation === 'branch') &&\n    branches\n  ) {\n    const newBranches: Record<string, JsxChildren> = {};\n    Object.entries(branches).forEach(\n      ([key, value]: [string, TaggedChildren]) => {\n        newBranches[key] = writeChildrenAsObjects(value);\n      }\n    );\n    newGTProp = {\n      ...newGTProp,\n      b: newBranches,\n      t: transformation === 'plural' ? 'p' : 'b',\n    };\n  }\n\n  return Object.keys(newGTProp).length ? newGTProp : undefined;\n};\n\n/**\n * Handles a single child element.\n * @param {TaggedElement} child - The child to handle.\n * @returns {JsxElement | Variable} The minified element.\n */\nconst handleSingleChildElement = (\n  child: TaggedElement\n): JsxElement | Variable => {\n  const { props } = child;\n  const minifiedElement: JsxElement = {\n    t: getTagName(child),\n  };\n  if (props['data-_gt']) {\n    // Get generaltranslation props\n    const generaltranslation = props['data-_gt'];\n\n    // Check if variable\n    const transformation = generaltranslation.transformation;\n    if (transformation === 'variable') {\n      const variableType = generaltranslation.variableType || 'variable';\n      const variableName = getVariableName(props, variableType);\n      const minifiedVariableType = minifyVariableType(variableType);\n      return {\n        i: generaltranslation.id,\n        k: variableName,\n        v: minifiedVariableType,\n      };\n    }\n\n    // Add id\n    minifiedElement.i = generaltranslation.id;\n\n    // Add GT prop\n    minifiedElement.d = createGTProp(\n      transformation as Transformation,\n      props,\n      generaltranslation.branches\n    );\n  }\n  if (props.children) {\n    minifiedElement.c = writeChildrenAsObjects(props.children);\n  }\n  return minifiedElement;\n};\n\nconst handleSingleChild = (child: TaggedChild): JsxChild => {\n  if (isValidTaggedElement(child)) {\n    return handleSingleChildElement(child);\n  }\n  if (typeof child === 'number') return child.toString();\n  return child as JsxChild;\n};\n\n/**\n * Transforms children elements into objects, processing each child recursively if needed.\n * TaggedChildren are transformed into JsxChildren\n * @param {Children} children - The children to process.\n * @returns {object} The processed children as objects.\n */\nexport function writeChildrenAsObjects(children: TaggedChildren): JsxChildren {\n  const result = Array.isArray(children)\n    ? children.map(handleSingleChild)\n    : handleSingleChild(children);\n  return result;\n}\n"],"mappings":"yNAoBA,MAAM,EAAc,GAAiC,CACnD,GAAI,CAAC,EAAO,MAAO,GACnB,GAAM,CAAE,OAAM,SAAU,EACxB,GAAI,GAAQ,OAAO,GAAS,WAAY,CACtC,GACE,gBAAiB,GACjB,OAA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