{"version":3,"file":"getNesting.mjs","names":[],"sources":["../../../src/interpreter/getNesting.ts"],"sourcesContent":["import type { GetSubPath } from '@intlayer/types/dictionary';\nimport type {\n  DictionaryKeys,\n  DictionaryRegistryContent,\n} from '@intlayer/types/module_augmentation';\nimport type { ValidDotPathsFor } from '../transpiler';\nimport type {\n  DeepTransformContent,\n  IInterpreterPluginState,\n  NodeProps,\n} from './getContent';\nimport { getIntlayer } from './getIntlayer';\n\nexport type GetNestingResult<\n  K extends DictionaryKeys,\n  P = undefined,\n  S = IInterpreterPluginState,\n> = GetSubPath<DeepTransformContent<DictionaryRegistryContent<K>, S>, P>;\n\n/**\n * Extracts content from another dictionary by its key and an optional path.\n *\n * This allows for reusing content across different dictionaries.\n *\n * @param dictionaryKey - The key of the dictionary to nest.\n * @param path - Optional dot-separated path to a specific field within the nested dictionary.\n * @param props - Optional properties like locale and plugins.\n * @returns The nested content.\n *\n * @example\n * ```ts\n * const content = getNesting(\"common\", \"buttons.save\");\n * // 'Save'\n * ```\n */\nexport const getNesting = <const K extends DictionaryKeys, const P>(\n  dictionaryKey: K,\n  path?: P extends ValidDotPathsFor<K> ? P : never,\n  props?: NodeProps\n): GetNestingResult<K, P> => {\n  const dictionary = getIntlayer(dictionaryKey, props?.locale, props?.plugins);\n\n  if (typeof path === 'string') {\n    const pathArray = (path as string).split('.');\n    let current: any = dictionary;\n\n    for (const key of pathArray) {\n      // Safely traverse down the object using the path\n      current = current?.[key];\n      // If we cannot find the path, return the whole dictionary as a fallback\n      if (current === undefined) {\n        return dictionary as any;\n      }\n    }\n\n    return current;\n  }\n\n  // Default or error handling if path is not a string or otherwise undefined\n  return dictionary as any;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmCA,MAAa,cACX,eACA,MACA,UAC2B;CAC3B,MAAM,aAAa,YAAY,eAAe,OAAO,QAAQ,OAAO,QAAQ;AAE5E,KAAI,OAAO,SAAS,UAAU;EAC5B,MAAM,YAAa,KAAgB,MAAM,IAAI;EAC7C,IAAI,UAAe;AAEnB,OAAK,MAAM,OAAO,WAAW;AAE3B,aAAU,UAAU;AAEpB,OAAI,YAAY,OACd,QAAO;;AAIX,SAAO;;AAIT,QAAO"}