{"version":3,"file":"useEditor.cjs","names":["useContext","IntlayerClientContext","useRef","isEnabled"],"sources":["../../../src/editor/useEditor.tsx"],"sourcesContent":["'use client';\n\nimport type { EditorStateManager } from '@intlayer/editor';\nimport { isEnabled } from '@intlayer/editor/isEnabled';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { useContext, useEffect, useRef } from 'react';\nimport { IntlayerClientContext } from '../client/IntlayerProvider';\n\n/**\n * Initializes the Intlayer editor client singleton when the editor is enabled.\n * Syncs the current locale from the Intlayer context into the editor manager so\n * the editor always knows which locale the app is displaying.\n */\nexport const useEditor = () => {\n  const { locale, setLocale } = useContext(IntlayerClientContext) ?? {};\n  const managerRef = useRef<EditorStateManager | null>(null);\n  // Read through a ref: the subscription outlives the render that created it\n  const setLocaleRef = useRef(setLocale);\n  setLocaleRef.current = setLocale;\n\n  useEffect(() => {\n    if (process.env.INTLAYER_EDITOR_ENABLED === 'false' || !isEnabled) return;\n\n    let unsubscribeLocaleRequest: (() => void) | null = null;\n    let isStopped = false;\n\n    import('@intlayer/editor')\n      .then(({ initEditorClient }) => {\n        if (isStopped) return;\n\n        const manager = initEditorClient();\n        managerRef.current = manager;\n\n        if (locale) manager.currentLocale.set(locale as Locale);\n\n        unsubscribeLocaleRequest = manager.onLocaleChangeRequested(\n          (requestedLocale) => setLocaleRef.current?.(requestedLocale)\n        );\n      })\n      .catch(() => {});\n\n    return () => {\n      isStopped = true;\n      unsubscribeLocaleRequest?.();\n      managerRef.current = null;\n      import('@intlayer/editor')\n        .then(({ stopEditorClient }) => {\n          stopEditorClient();\n        })\n        .catch(() => {});\n    };\n  }, []);\n\n  useEffect(() => {\n    if (!locale || !managerRef.current) return;\n\n    managerRef.current.currentLocale.set(locale as Locale);\n  }, [locale]);\n};\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAa,kBAAkB;CAC7B,MAAM,EAAE,QAAQ,kBAAcA,kBAAWC,qDAAqB,KAAK,CAAC;CACpE,MAAM,iBAAaC,cAAkC,IAAI;CAEzD,MAAM,mBAAeA,cAAO,SAAS;CACrC,aAAa,UAAU;CAEvB,2BAAgB;EACd,IAAI,QAAQ,IAAI,4BAA4B,WAAW,CAACC,sCAAW;EAEnE,IAAI,2BAAgD;EACpD,IAAI,YAAY;EAEhB,OAAO,mBAAmB,CACvB,MAAM,EAAE,uBAAuB;GAC9B,IAAI,WAAW;GAEf,MAAM,UAAU,iBAAiB;GACjC,WAAW,UAAU;GAErB,IAAI,QAAQ,QAAQ,cAAc,IAAI,MAAgB;GAEtD,2BAA2B,QAAQ,yBAChC,oBAAoB,aAAa,UAAU,eAAe,CAC7D;EACF,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EAEjB,aAAa;GACX,YAAY;GACZ,2BAA2B;GAC3B,WAAW,UAAU;GACrB,OAAO,mBAAmB,CACvB,MAAM,EAAE,uBAAuB;IAC9B,iBAAiB;GACnB,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACnB;CACF,GAAG,CAAC,CAAC;CAEL,2BAAgB;EACd,IAAI,CAAC,UAAU,CAAC,WAAW,SAAS;EAEpC,WAAW,QAAQ,cAAc,IAAI,MAAgB;CACvD,GAAG,CAAC,MAAM,CAAC;AACb"}