{"version":3,"file":"insertContentInDictionary.mjs","names":[],"sources":["../../../src/deepTransformPlugins/insertContentInDictionary.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/types/dictionary';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport * as NodeTypes from '@intlayer/types/nodeType';\n\n// {\n//     {\n//       title: 'このページ',\n//       linkLabel: 'セクションへ移動',\n//       collapseButton: { label: '折りたたむ' }\n//     },\n//   }\n//   {\n//     dictionary: {\n//       key: 'aside-navigation',\n//       content: {\n//         title: {\n//          nodeType: 'translation',\n//          translation: {\n//             ar: 'في هذه الصفحة',\n//             de: 'Auf dieser Seite',\n//             en: 'In this page',\n//             'en-GB': 'On this page',\n//             es: 'En esta página',\n//             fr: 'Dans cette page',\n//             hi: 'इस पृष्ठ में',\n//             it: 'In questa pagina',\n//             ko: '이 페이지에서',\n//             pt: 'Nesta página',\n//             ru: 'На этой странице',\n//             tr: 'Bu sayfada',\n//             zh: '在此页面'\n//           }\n//         },\n//       },\n//       localId: 'aside-navigation::local::src/components/AsideNavigation/asideNavigation.content.ts',\n//       location: 'local',\n//       filePath: 'src/components/AsideNavigation/asideNavigation.content.ts'\n//     }\n//   }\n\n/**\n * Deep merge helper that handles translation blocks\n * @param target - The target object (dictionary content)\n * @param source - The source object (new content to merge)\n * @param locale - Optional locale to target specific translation\n * @returns Merged content\n */\nconst deepMergeContent = (\n  target: any,\n  source: any,\n  locale?: LocalesValues\n): any => {\n  // If source is null or undefined, return target\n  if (source === null || source === undefined) return target;\n\n  // If target is null or undefined, initialize based on source type\n  if (target === null || target === undefined) {\n    if (Array.isArray(source)) {\n      target = [];\n    } else if (typeof source === 'object') {\n      target = {};\n    } else {\n      return source;\n    }\n  }\n\n  // Handle translation nodes FIRST (before arrays) to support arrays within translations\n  if (\n    target &&\n    typeof target === 'object' &&\n    target.nodeType === NodeTypes.TRANSLATION\n  ) {\n    // Target is a translation block\n    if (locale) {\n      // Update only the specific locale - create new object to preserve order\n      const existingLocaleContent = target[NodeTypes.TRANSLATION][locale];\n      let newLocaleContent: any;\n\n      if (typeof source === 'object' && !Array.isArray(source)) {\n        // Source is an object, need to deep merge for this locale\n        newLocaleContent = deepMergeContent(\n          existingLocaleContent,\n          source,\n          undefined // Don't pass locale down for nested content\n        );\n      } else if (Array.isArray(source)) {\n        // Source is an array, set it directly for this locale\n        newLocaleContent = source;\n      } else {\n        // Source is a primitive, directly set it\n        newLocaleContent = source;\n      }\n\n      // Return new object with locale appended at the end (preserve insertion order)\n      return {\n        ...target,\n        [NodeTypes.TRANSLATION]: {\n          ...target[NodeTypes.TRANSLATION],\n          [locale]: newLocaleContent,\n        },\n      };\n    } else {\n      // No locale specified, replace/merge the entire translation\n      if (\n        typeof source === 'object' &&\n        !Array.isArray(source) &&\n        source.nodeType === NodeTypes.TRANSLATION\n      ) {\n        // Source is also a translation node, merge translations\n        return {\n          ...target,\n          [NodeTypes.TRANSLATION]: {\n            ...target[NodeTypes.TRANSLATION],\n            ...source[NodeTypes.TRANSLATION],\n          },\n        };\n      } else {\n        // Source is not a translation node, wrap it\n        return processNewContent(source, locale);\n      }\n    }\n  }\n\n  // Handle arrays\n  if (Array.isArray(source)) {\n    if (locale && Array.isArray(target)) {\n      // When locale is specified and target is also an array, merge element by element\n      const result = [];\n      const maxLength = Math.max(source.length, target.length);\n\n      for (let i = 0; i < maxLength; i++) {\n        if (i < source.length) {\n          if (i < target.length && target[i] !== undefined) {\n            // Both source and target have element at this index - merge them\n            result[i] = deepMergeContent(target[i], source[i], locale);\n          } else {\n            // Only source has element at this index - process new content\n            result[i] = processNewContent(source[i], locale);\n          }\n        } else {\n          // Only target has element at this index - keep it\n          result[i] = target[i];\n        }\n      }\n\n      return result;\n    } else {\n      // No locale or target is not an array - replace entirely\n      return source.map((item) => {\n        // Process each item in case it needs locale wrapping\n        if (\n          item &&\n          typeof item === 'object' &&\n          item.nodeType === NodeTypes.TRANSLATION\n        ) {\n          return item;\n        }\n        return processNewContent(item, locale);\n      });\n    }\n  }\n\n  // Handle objects\n  if (typeof source === 'object' && !Array.isArray(source)) {\n    if (typeof target !== 'object' || Array.isArray(target)) {\n      target = {};\n    }\n\n    // Create new object to preserve key order and avoid mutation\n    const result: any = { ...target };\n\n    for (const key in source) {\n      if (Object.hasOwn(source, key)) {\n        if (target[key] !== undefined) {\n          result[key] = deepMergeContent(target[key], source[key], locale);\n        } else {\n          result[key] = processNewContent(source[key], locale);\n        }\n      }\n    }\n\n    return result;\n  }\n\n  // For primitives, just return the source\n  return source;\n};\n\n/**\n * Process new content that doesn't exist in target\n * @param content - New content to process\n * @param locale - Optional locale\n * @returns Processed content\n */\nconst processNewContent = (content: any, locale?: LocalesValues): any => {\n  // Handle null and undefined\n  if (content === null || content === undefined) {\n    return content;\n  }\n\n  // If content is already a translation node, return as-is\n  if (\n    content &&\n    typeof content === 'object' &&\n    content.nodeType === NodeTypes.TRANSLATION\n  ) {\n    return content;\n  }\n\n  // Handle arrays\n  if (Array.isArray(content)) {\n    return content.map((item) => processNewContent(item, locale));\n  }\n\n  // Handle objects\n  if (content && typeof content === 'object') {\n    const result: any = {};\n\n    for (const key in content) {\n      if (Object.hasOwn(content, key)) {\n        result[key] = processNewContent(content[key], locale);\n      }\n    }\n    return result;\n  }\n\n  // Handle primitives\n  if (locale) {\n    // Wrap in translation node with the specific locale\n    return {\n      nodeType: NodeTypes.TRANSLATION,\n      [NodeTypes.TRANSLATION]: {\n        [locale]: content,\n      },\n    };\n  } else {\n    // If no locale, just return the content as-is (don't wrap)\n    return content;\n  }\n};\n\n/**\n * Insert content into a dictionary with deep merge support\n * Handles translation blocks and nested structures\n *\n * @param dictionary - The dictionary to insert content into\n * @param content - The content to insert\n * @param locale - Optional locale to target specific translation\n * @returns Updated dictionary\n *\n * @example\n * // Without locale - deep merge all content\n * insertContentInDictionary(dictionary, { title: 'New Title' })\n *\n * @example\n * // With locale - insert content for specific locale\n * insertContentInDictionary(dictionary, { title: 'このページ' }, 'ja')\n */\nexport const insertContentInDictionary = (\n  dictionary: Dictionary,\n  content: Dictionary['content'],\n  locale?: LocalesValues\n): Dictionary => {\n  const mergedContent = deepMergeContent(\n    Array.isArray(dictionary.content)\n      ? [...dictionary.content]\n      : { ...dictionary.content },\n    content,\n    locale\n  );\n\n  return {\n    ...dictionary,\n    content: mergedContent,\n  };\n};\n"],"mappings":";;;;;;;;;;AA+CA,MAAM,oBACJ,QACA,QACA,WACQ;AAER,KAAI,WAAW,QAAQ,WAAW,OAAW,QAAO;AAGpD,KAAI,WAAW,QAAQ,WAAW,OAChC,KAAI,MAAM,QAAQ,OAAO,CACvB,UAAS,EAAE;UACF,OAAO,WAAW,SAC3B,UAAS,EAAE;KAEX,QAAO;AAKX,KACE,UACA,OAAO,WAAW,YAClB,OAAO,aAAa,UAAU,YAG9B,KAAI,QAAQ;EAEV,MAAM,wBAAwB,OAAO,UAAU,aAAa;EAC5D,IAAI;AAEJ,MAAI,OAAO,WAAW,YAAY,CAAC,MAAM,QAAQ,OAAO,CAEtD,oBAAmB,iBACjB,uBACA,QACA,OACD;WACQ,MAAM,QAAQ,OAAO,CAE9B,oBAAmB;MAGnB,oBAAmB;AAIrB,SAAO;GACL,GAAG;IACF,UAAU,cAAc;IACvB,GAAG,OAAO,UAAU;KACnB,SAAS;IACX;GACF;YAIC,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,OAAO,IACtB,OAAO,aAAa,UAAU,YAG9B,QAAO;EACL,GAAG;GACF,UAAU,cAAc;GACvB,GAAG,OAAO,UAAU;GACpB,GAAG,OAAO,UAAU;GACrB;EACF;KAGD,QAAO,kBAAkB,QAAQ,OAAO;AAM9C,KAAI,MAAM,QAAQ,OAAO,CACvB,KAAI,UAAU,MAAM,QAAQ,OAAO,EAAE;EAEnC,MAAM,SAAS,EAAE;EACjB,MAAM,YAAY,KAAK,IAAI,OAAO,QAAQ,OAAO,OAAO;AAExD,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,IAC7B,KAAI,IAAI,OAAO,OACb,KAAI,IAA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