{"version":3,"file":"FocusDictionaryContext.cjs","names":["useEditorStateManager","NodeTypes"],"sources":["../../src/FocusDictionaryContext.tsx"],"sourcesContent":["'use client';\n\nimport type { FileContent } from '@intlayer/editor';\nimport type { KeyPath } from '@intlayer/types/keyPath';\nimport * as NodeTypes from '@intlayer/types/nodeType';\nimport {\n  createContext,\n  type FC,\n  type PropsWithChildren,\n  useCallback,\n  useContext,\n  useEffect,\n  useState,\n} from 'react';\nimport { useEditorStateManager } from './EditorStateContext';\n\nexport type { FileContent } from '@intlayer/editor';\n\nexport type FocusDictionaryState = {\n  focusedContent: FileContent | null;\n};\n\nexport type FocusDictionaryActions = {\n  setFocusedContent: (value: FileContent | null) => void;\n  setFocusedContentKeyPath: (keyPath: KeyPath[]) => void;\n};\n\ntype FocusDictionaryContextType = FocusDictionaryState & FocusDictionaryActions;\n\n// Create native React context fallback\nconst FocusDictionaryReactContext = createContext<\n  FocusDictionaryContextType | undefined\n>(undefined);\n\n// Create the Provider\nexport const FocusDictionaryProvider: FC<PropsWithChildren> = ({\n  children,\n}) => {\n  const manager = useEditorStateManager();\n  const [fallbackContent, setFallbackContent] = useState<FileContent | null>(\n    null\n  );\n\n  const setFocusedContent = useCallback(\n    (value: FileContent | null) => {\n      if (manager) {\n        manager.focusedContent.set(value);\n      } else {\n        setFallbackContent(value);\n      }\n    },\n    [manager]\n  );\n\n  const setFocusedContentKeyPath = useCallback(\n    (keyPath: KeyPath[]) => {\n      if (manager) {\n        manager.setFocusedContentKeyPath(keyPath);\n      } else {\n        setFallbackContent((prev) => {\n          if (!prev) return null;\n          const filtered = keyPath.filter(\n            (key) => key.type !== NodeTypes.TRANSLATION\n          );\n          return { ...prev, keyPath: filtered };\n        });\n      }\n    },\n    [manager]\n  );\n\n  return (\n    <FocusDictionaryReactContext.Provider\n      value={{\n        focusedContent: manager?.focusedContent.value ?? fallbackContent,\n        setFocusedContent,\n        setFocusedContentKeyPath,\n      }}\n    >\n      {children}\n    </FocusDictionaryReactContext.Provider>\n  );\n};\n\n// 3. Update the hook to consume the fallback context\nexport const useFocusDictionary = (): FocusDictionaryState &\n  FocusDictionaryActions => {\n  const manager = useEditorStateManager();\n  const reactContext = useContext(FocusDictionaryReactContext);\n\n  const [focusedContent, setFocusedContentState] = useState<FileContent | null>(\n    manager?.focusedContent.value ?? reactContext?.focusedContent ?? null\n  );\n\n  useEffect(() => {\n    if (!manager) {\n      setFocusedContentState(reactContext?.focusedContent ?? null);\n      return;\n    }\n\n    const handler = (e: Event) =>\n      setFocusedContentState((e as CustomEvent<FileContent | null>).detail);\n    manager.focusedContent.addEventListener('change', handler);\n\n    return () => manager.focusedContent.removeEventListener('change', handler);\n  }, [manager, reactContext?.focusedContent]);\n\n  const setFocusedContent = useCallback(\n    (value: FileContent | null) => {\n      if (reactContext) {\n        reactContext.setFocusedContent(value);\n      } else if (manager) {\n        manager.focusedContent.set(value);\n      }\n    },\n    [reactContext, manager]\n  );\n\n  const setFocusedContentKeyPath = useCallback(\n    (keyPath: KeyPath[]) => {\n      if (reactContext) {\n        reactContext.setFocusedContentKeyPath(keyPath);\n      } else if (manager) {\n        manager.setFocusedContentKeyPath(keyPath);\n      }\n    },\n    [reactContext, manager]\n  );\n\n  return {\n    focusedContent,\n    setFocusedContent,\n    setFocusedContentKeyPath,\n  };\n};\n\nexport const useFocusDictionaryActions = (): FocusDictionaryActions => {\n  const { setFocusedContent, setFocusedContentKeyPath } = useFocusDictionary();\n  return { setFocusedContent, setFocusedContentKeyPath };\n};\n"],"mappings":";;;;;;;;;;;AA8BA,MAAM,uDAEJ,OAAU;AAGZ,MAAa,2BAAkD,EAC7D,eACI;CACJ,MAAM,UAAUA,kDAAuB;CACvC,MAAM,CAAC,iBAAiB,0CACtB,KACD;CAED,MAAM,4CACH,UAA8B;AAC7B,MAAI,QACF,SAAQ,eAAe,IAAI,MAAM;MAEjC,oBAAmB,MAAM;IAG7B,CAAC,QAAQ,CACV;CAED,MAAM,mDACH,YAAuB;AACtB,MAAI,QACF,SAAQ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