{"version":3,"file":"useEditor.cjs","names":["isEnabled","inject","DestroyRef","Injector","INTLAYER_TOKEN","signal","runInInjectionContext","effect","provideAppInitializer"],"sources":["../../../src/editor/useEditor.ts"],"sourcesContent":["import {\n  DestroyRef,\n  effect,\n  Injector,\n  inject,\n  provideAppInitializer,\n  runInInjectionContext,\n  signal,\n} from '@angular/core';\nimport type { EditorStateManager } from '@intlayer/editor';\nimport { isEnabled } from '@intlayer/editor/isEnabled';\nimport type { Locale } from '@intlayer/types/allLocales';\n// Import from the standalone token file to avoid a circular dependency:\n//   installIntlayer.ts → useEditor.ts → ../client → installIntlayer.ts\nimport { INTLAYER_TOKEN, type IntlayerProvider } from '../client/intlayerToken';\nimport { setLocaleInStorage } from '../client/useLocaleStorage';\n\n/**\n * Initialises the Intlayer editor client singleton when the editor is enabled.\n * Syncs the current locale from the provided Intlayer client into the editor\n * manager so the editor always knows which locale the app is displaying.\n *\n * Must be called inside an Angular injection context (e.g. a component\n * constructor, `provideAppInitializer`, or `runInInjectionContext`).\n *\n * @param client - The IntlayerProvider instance to sync locale from.\n *   When omitted the function injects `INTLAYER_TOKEN` from the DI tree,\n *   so it still works when called directly from a component.\n */\nexport const useEditor = (client?: IntlayerProvider | null): void => {\n  if (process.env.INTLAYER_EDITOR_ENABLED === 'false' || !isEnabled) return;\n\n  const destroyRef = inject(DestroyRef, { optional: true });\n  const injector = inject(Injector);\n\n  // Resolve the client: use the passed-in reference or fall back to injection.\n  const resolvedClient =\n    client ??\n    inject<IntlayerProvider>(INTLAYER_TOKEN, { optional: true } as any);\n\n  // `manager` signal is set once the async import resolves.\n  // Using a signal lets an `effect()` react to it becoming available.\n  const manager = signal<EditorStateManager | null>(null);\n\n  // Guard: prevents the async callback from acting after the view is destroyed.\n  let stopped = false;\n\n  // Initialise the editor client\n  import('@intlayer/editor')\n    .then(({ initEditorClient }) => {\n      if (stopped) return;\n      manager.set(initEditorClient());\n    })\n    .catch(() => {});\n\n  // Keep the editor locale in sync with the Angular locale signal\n  const effectRef = runInInjectionContext(injector, () =>\n    effect(() => {\n      const m = manager();\n      const locale = resolvedClient?.locale();\n      if (m && locale) m.currentLocale.set(locale as Locale);\n    })\n  );\n\n  // Apply the locales requested by the editor\n  const localeRequestEffectRef = runInInjectionContext(injector, () =>\n    effect((onCleanup) => {\n      const m = manager();\n\n      if (!m || !resolvedClient) return;\n\n      onCleanup(\n        m.onLocaleChangeRequested((requestedLocale) => {\n          resolvedClient.setLocale(requestedLocale);\n          setLocaleInStorage(requestedLocale, true);\n        })\n      );\n    })\n  );\n\n  // Tear down on destroy\n  destroyRef?.onDestroy(() => {\n    stopped = true;\n    effectRef.destroy();\n    localeRequestEffectRef.destroy();\n    manager.set(null);\n    import('@intlayer/editor')\n      .then(({ stopEditorClient }) => {\n        stopEditorClient();\n      })\n      .catch(() => {});\n  });\n};\n\n/**\n * Angular provider that wires `useEditor` into the application initialisation\n * phase via `provideAppInitializer`.\n *\n * `provideIntlayer()` already calls this internally, so you only need this\n * function when you want to manage providers individually.\n *\n * @example\n * ```ts\n * // app.config.ts\n * import { provideIntlayer, provideIntlayerEditor } from 'angular-intlayer';\n *\n * export const appConfig: ApplicationConfig = {\n *   providers: [provideIntlayer(), provideIntlayerEditor()],\n * };\n * ```\n */\nexport const provideIntlayerEditor = (client?: IntlayerProvider | null) =>\n  provideAppInitializer(() => useEditor(client));\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA6BA,MAAa,aAAa,WAA2C;CACnE,IAAI,QAAQ,IAAI,4BAA4B,WAAW,CAACA,sCAAW;CAEnE,MAAM,iBAAaC,sBAAOC,0BAAY,EAAE,UAAU,KAAK,CAAC;CACxD,MAAM,eAAWD,sBAAOE,sBAAQ;CAGhC,MAAM,iBACJ,cACAF,sBAAyBG,6CAAgB,EAAE,UAAU,KAAK,CAAQ;CAIpE,MAAM,cAAUC,sBAAkC,IAAI;CAGtD,IAAI,UAAU;CAGd,OAAO,mBAAmB,CACvB,MAAM,EAAE,uBAAuB;EAC9B,IAA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