{"version":3,"file":"EditedContentContext.mjs","names":[],"sources":["../../src/EditedContentContext.tsx"],"sourcesContent":["'use client';\n\nimport type { DictionaryContent } from '@intlayer/editor';\nimport type {\n  ContentNode,\n  Dictionary,\n  LocalDictionaryId,\n} from '@intlayer/types/dictionary';\nimport type { KeyPath } from '@intlayer/types/keyPath';\nimport { useEffect, useState } from 'react';\nimport { useEditorStateManager } from './EditorStateContext';\n\nexport type { DictionaryContent } from '@intlayer/editor';\n\ntype EditedContentActionsContextType = {\n  setEditedContentState: (editedContent: DictionaryContent) => void;\n  setEditedDictionary: (dict: Dictionary) => void;\n  setEditedContent: (\n    localDictionaryId: LocalDictionaryId,\n    newValue: Dictionary['content']\n  ) => void;\n  addEditedContent: (\n    localDictionaryId: LocalDictionaryId,\n    newValue: ContentNode<any>,\n    keyPath?: KeyPath[],\n    overwrite?: boolean\n  ) => void;\n  renameEditedContent: (\n    localDictionaryId: LocalDictionaryId,\n    newKey: KeyPath['key'],\n    keyPath?: KeyPath[]\n  ) => void;\n  removeEditedContent: (\n    localDictionaryId: LocalDictionaryId,\n    keyPath: KeyPath[]\n  ) => void;\n  restoreEditedContent: (localDictionaryId: LocalDictionaryId) => void;\n  clearEditedDictionaryContent: (localDictionaryId: LocalDictionaryId) => void;\n  clearEditedContent: () => void;\n  getEditedContentValue: (\n    localDictionaryIdOrKey: LocalDictionaryId | Dictionary['key'] | string,\n    keyPath: KeyPath[]\n  ) => ContentNode | undefined;\n};\n\nexport const useEditedContentActions = (): EditedContentActionsContextType => {\n  const manager = useEditorStateManager();\n\n  return {\n    setEditedContentState: (value: DictionaryContent) =>\n      manager?.editedContent.set(value),\n    setEditedDictionary: (dict: Dictionary) =>\n      manager?.setEditedDictionary(dict),\n    setEditedContent: (\n      localId: LocalDictionaryId,\n      value: Dictionary['content']\n    ) => manager?.setEditedContent(localId, value),\n    addEditedContent: (localId, value, keyPath, overwrite) =>\n      manager?.addContent(localId, value, keyPath, overwrite),\n    renameEditedContent: (localId, newKey, keyPath) =>\n      manager?.renameContent(localId, newKey, keyPath),\n    removeEditedContent: (localId, keyPath) =>\n      manager?.removeContent(localId, keyPath),\n    restoreEditedContent: (localId) => manager?.restoreContent(localId),\n    clearEditedDictionaryContent: (localId) => manager?.clearContent(localId),\n    clearEditedContent: () => manager?.clearAllContent(),\n    getEditedContentValue: (localIdOrKey, keyPath) =>\n      manager?.getContentValue(localIdOrKey, keyPath),\n  };\n};\n\nexport const useEditedContent = () => {\n  const manager = useEditorStateManager();\n  const [editedContent, setEditedContentState] = useState<\n    DictionaryContent | undefined\n  >(manager?.editedContent.value);\n\n  useEffect(() => {\n    if (!manager) return;\n    const handler = (e: Event) =>\n      setEditedContentState((e as CustomEvent<DictionaryContent>).detail);\n    manager.editedContent.addEventListener('change', handler);\n    return () => manager.editedContent.removeEventListener('change', handler);\n  }, [manager]);\n\n  const actions = useEditedContentActions();\n  return { editedContent, ...actions };\n};\n\nexport const usePostEditedContentState = <S,>(\n  onEventTriggered?: (data: S) => void\n) => {\n  const manager = useEditorStateManager();\n  useEffect(() => {\n    if (!onEventTriggered || !manager) return;\n    return manager.messenger.subscribe(\n      `INTLAYER_EDITED_CONTENT_CHANGED/post`,\n      onEventTriggered as (data: unknown) => void\n    );\n  }, [manager, onEventTriggered]);\n};\n\nexport const useGetEditedContentState = () => {\n  const manager = useEditorStateManager();\n  return () => {\n    manager?.messenger.send('INTLAYER_EDITED_CONTENT_CHANGED/get');\n  };\n};\n"],"mappings":";;;;;;AA6CA,MAAa,gCAAiE;CAC5E,MAAM,UAAU,uBAAuB;AAEvC,QAAO;EACL,wBAAwB,UACtB,SAAS,cAAc,IAAI,MAAM;EACnC,sBAAsB,SACpB,SAAS,oBAAoB,KAAK;EACpC,mBACE,SACA,UACG,SAAS,iBAAiB,SAAS,MAAM;EAC9C,mBAAmB,SAAS,OAAO,SAAS,cAC1C,SAAS,WAAW,SAAS,OAAO,SAAS,UAAU;EACzD,sBAAsB,SAAS,QAAQ,YACrC,SAAS,cAAc,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