{"version":3,"file":"useFocusUnmergedDictionary.mjs","names":[],"sources":["../../src/useFocusUnmergedDictionary.tsx"],"sourcesContent":["'use client';\n\nimport { getContentNodeByKeyPath } from '@intlayer/core/dictionaryManipulator';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { Dictionary, LocalDictionaryId } from '@intlayer/types/dictionary';\nimport type { KeyPath } from '@intlayer/types/keyPath';\nimport { useMemo } from 'react';\nimport { useDictionariesRecord } from './DictionariesRecordContext';\nimport { type FileContent, useFocusDictionary } from './FocusDictionaryContext';\nimport { useEditorLocale } from './useEditorLocale';\n\ntype UnmergedKeyPath = {\n  keyPath: KeyPath[];\n  dictionaryLocalId?: LocalDictionaryId;\n};\n\n/**\n * Converts merged keypath format to unmerged format.\n * Merged: [{type: \"translation\", key: \"fr\"}, {type: \"object\", key: \"title\"}]\n * Unmerged: [{type: \"object\", key: \"title\"}, {type: \"translation\", key: \"fr\"}]\n */\nconst mergedKeyPathToUnmergedKeyPath = (\n  keyPath: KeyPath[],\n  dictionaries: Dictionary[],\n  locale?: Locale\n): UnmergedKeyPath | undefined => {\n  // If we have a dictionary, verify the path exists\n  // Try to find the correct position for the translation key\n  // by checking which path actually exists in the dictionary\n  for (const dictionary of dictionaries) {\n    try {\n      const result = getContentNodeByKeyPath(\n        dictionary.content,\n        keyPath ?? [],\n        locale\n      );\n\n      if (result) {\n        return { keyPath, dictionaryLocalId: dictionary.localId };\n      }\n    } catch {\n      // Continue to next candidate\n    }\n  }\n};\n\nexport const useFocusUnmergedDictionary = () => {\n  const { localeDictionaries } = useDictionariesRecord();\n  const currentLocale = useEditorLocale();\n  const {\n    setFocusedContent,\n    setFocusedContentKeyPath,\n    focusedContent: mergedFocusedContent,\n  } = useFocusDictionary();\n\n  const focusedContent = useMemo<FileContent | null>(() => {\n    if (!mergedFocusedContent) return mergedFocusedContent;\n    if (!localeDictionaries) return mergedFocusedContent;\n    if (mergedFocusedContent.dictionaryLocalId) return mergedFocusedContent;\n\n    const dictionaries = Object.values(localeDictionaries).filter(\n      (dictionary) => dictionary.key === mergedFocusedContent.dictionaryKey\n    );\n\n    const unmergedKeyPath = mergedKeyPathToUnmergedKeyPath(\n      mergedFocusedContent.keyPath ?? [],\n      dictionaries,\n      currentLocale\n    );\n\n    return {\n      ...mergedFocusedContent,\n      ...unmergedKeyPath,\n    };\n  }, [mergedFocusedContent, localeDictionaries, currentLocale]);\n\n  return {\n    focusedContent,\n    setFocusedContent,\n    setFocusedContentKeyPath,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;AAqBA,MAAM,kCACJ,SACA,cACA,WACgC;AAIhC,MAAK,MAAM,cAAc,aACvB,KAAI;AAOF,MANe,wBACb,WAAW,SACX,WAAW,EAAE,EACb,OAGQ,CACR,QAAO;GAAE;GAAS,mBAAmB,WAAW;GAAS;SAErD;;AAMZ,MAAa,mCAAmC;CAC9C,MAAM,EAAE,uBAAuB,uBAAuB;CACtD,MAAM,gBAAgB,iBAAiB;CACvC,MAAM,EACJ,mBACA,0BACA,gBAAgB,yBACd,oBAAoB;AAuBxB,QAAO;EACL,gBAtBqB,cAAkC;AACvD,OAAI,CAAC,qBAAsB,QAAO;AAClC,OAAI,CAAC,mBAAoB,QAAO;AAChC,OAAI,qBAAqB,kBAAmB,QAAO;GAEnD,MAAM,eAAe,OAAO,OAAO,mBAAmB,CAAC,QACpD,eAAe,WAAW,QAAQ,qBAAqB,cACzD;GAED,MAAM,kBAAkB,+BACtB,qBAAqB,WAAW,EAAE,EAClC,cACA,cACD;AAED,UAAO;IACL,GAAG;IACH,GAAG;IACJ;KACA;GAAC;GAAsB;GAAoB;GAAc,CAG5C;EACd;EACA;EACD"}