{"version":3,"file":"getMultilingualDictionary.cjs","names":["deepTransformNode","t"],"sources":["../../../src/deepTransformPlugins/getMultilingualDictionary.ts"],"sourcesContent":["import type { Dictionary, TypedNode } from '@intlayer/types/dictionary';\nimport type { KeyPath } from '@intlayer/types/keyPath';\nimport type { NodeType } from '@intlayer/types/nodeType';\nimport {\n  deepTransformNode,\n  type NodeProps,\n  type Plugins,\n} from '../interpreter';\nimport { t } from '../transpiler';\n\n/**\n * Transform a per-locale dictionary into a multilingual dictionary.\n *\n * Example:\n * ```json\n * {\n *   \"key\": \"about-page\",\n *   \"locale\": \"en\",\n *   \"content\": {\n *     \"myContent\": \"English content\"\n *   }\n * }\n * ```\n *\n * ```json\n * {\n *   \"key\": \"about-page\",\n *   \"content\": {\n *     \"myContent\": t({\n *       \"en\": \"English content\",\n *     })\n *   }\n * }\n * ```\n */\nexport const getMultilingualDictionary = (\n  dictionary: Dictionary\n): Dictionary => {\n  if (!dictionary.locale) {\n    return dictionary;\n  }\n\n  const locale = dictionary.locale;\n\n  const wrapPrimitiveInTranslationPlugin: Plugins = {\n    id: 'wrap-primitive-in-translation',\n    canHandle: (node) =>\n      typeof node === 'string' ||\n      typeof node === 'number' ||\n      typeof node === 'boolean',\n    transform: (node) => t({ [locale]: node } as Record<string, unknown>),\n  };\n\n  const traverseTypedNodePlugin: Plugins = {\n    id: 'traverse-typed-node-plugin',\n    canHandle: (node) =>\n      typeof node === 'object' && typeof (node as any)?.nodeType === 'string',\n    transform: (node: TypedNode, props, transformFn) => {\n      const nodeType = (node as any).nodeType as NodeType;\n      const inner = structuredClone(\n        (node as any)[nodeType as unknown as keyof TypedNode] as any\n      );\n\n      if (typeof inner !== 'object' || inner === null) {\n        const transformed = transformFn(inner, {\n          ...props,\n          children: inner,\n          keyPath: [\n            ...props.keyPath,\n            { type: nodeType, key: nodeType } as KeyPath,\n          ],\n        });\n        return {\n          ...node,\n          [nodeType as unknown as keyof TypedNode]: transformed,\n        };\n      }\n\n      for (const key in inner) {\n        const childProps: NodeProps = {\n          ...props,\n          children: inner[key as unknown as keyof typeof inner],\n          keyPath: [...props.keyPath, { type: nodeType, key } as KeyPath],\n        };\n        inner[key as unknown as keyof typeof inner] = transformFn(\n          inner[key as unknown as keyof typeof inner],\n          childProps\n        );\n      }\n\n      return { ...node, [nodeType as unknown as keyof TypedNode]: inner };\n    },\n  };\n\n  const transformedContent = deepTransformNode(\n    JSON.parse(JSON.stringify(dictionary.content)),\n    {\n      dictionaryKey: dictionary.key,\n      keyPath: [],\n      plugins: [traverseTypedNodePlugin, wrapPrimitiveInTranslationPlugin],\n    }\n  );\n\n  const { locale: _omitLocale, ...rest } = dictionary as any;\n  return {\n    ...rest,\n    content: transformedContent,\n  } as Dictionary;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,6BACX,eACe;AACf,KAAI,CAAC,WAAW,OACd,QAAO;CAGT,MAAM,SAAS,WAAW;CAoD1B,MAAM,qBAAqBA,+DACzB,KAAK,MAAM,KAAK,UAAU,WAAW,QAAQ,CAAC,EAC9C;EACE,eAAe,WAAW;EAC1B,SAAS,EAAE;EACX,SAAS,CAAC;GA7CZ,IAAI;GACJ,YAAY,SACV,OAAO,SAAS,YAAY,OAAQ,MAAc,aAAa;GACjE,YAAY,MAAiB,OAAO,gBAAgB;IAClD,MAAM,WAAY,KAAa;IAC/B,MAAM,QAAQ,gBACX,KAAa,UACf;AAED,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;KAC/C,MAAM,cAAc,YAAY,OAAO;MACrC,GAAG;MACH,UAAU;MACV,SAAS,CACP,GAAG,MAAM,SACT;OAAE,MAAM;OAAU,KAAK;OAAU,CAClC;MACF,CAAC;AACF,YAAO;MACL,GAAG;OACF,WAAyC;MAC3C;;AAGH,SAAK,MAAM,OAAO,OAAO;KACvB,MAAM,aAAwB;MAC5B,GAAG;MACH,UAAU,MAAM;MAChB,SAAS,CAAC,GAAG,MAAM,SAAS;OAAE,MAAM;OAAU;OAAK,CAAY;MAChE;AACD,WAAM,OAAwC,YAC5C,MAAM,MACN,WACD;;AAGH,WAAO;KAAE,GAAG;MAAO,WAAyC;KAAO;;GASlC,EAAE;GAtDrC,IAAI;GACJ,YAAY,SACV,OAAO,SAAS,YAChB,OAAO,SAAS,YAChB,OAAO,SAAS;GAClB,YAAY,SAASC,6CAAE,GAAG,SAAS,MAAM,CAA4B;GAiDA,CAAC;EACrE,CACF;CAED,MAAM,EAAE,QAAQ,aAAa,GAAG,SAAS;AACzC,QAAO;EACL,GAAG;EACH,SAAS;EACV"}