{"version":3,"file":"create-Dn1m6fyc.cjs","names":["createVueI18n","createVitePressRouterAdapter","require_adapter","_i18n_micro_vue","ROUTE_HOOKS","applyRouteMessages","routeMessages","routeName","byLocale","locale","translations","plugin","createI18n","defaultLocale","options","locales","syncWithVitePress","paths","pathMethods","pageRouteNames","byApp","ctx","state","app","adapter","route","router","href","navOptions","sync","nextLocale","path","derived","hook","current","handler","to","url"],"sources":["../src/runtime/create.ts"],"sourcesContent":["import type { Locale, PluralFunc, Translations } from '@i18n-micro/types'\n\nimport { createI18n as createVueI18n, type I18nPlugin, type VueI18nOptions } from '@i18n-micro/vue'\nimport type { App, Ref } from 'vue'\nimport { createVitePressRouterAdapter, type PathMethods, type VitePressRouterAdapter, type VitePressRouterLike } from '../router/adapter'\n\nexport type { PathMethods }\n\nexport interface CreateI18nOptions {\n  locale: string\n  fallbackLocale?: string\n  locales?: Locale[]\n  defaultLocale?: string\n  messages?: Record<string, Translations>\n  /**\n   * Page-scoped dictionaries keyed by route name (`guide-demo`), then locale.\n   * Loaded from `locales/pages/**` when using `withI18n`.\n   */\n  routeMessages?: Record<string, Record<string, Translations>>\n  plural?: PluralFunc\n  missingWarn?: boolean\n  numberFormats?: VueI18nOptions['numberFormats']\n  datetimeFormats?: VueI18nOptions['datetimeFormats']\n  timeZone?: string\n  missingHandler?: (locale: string, key: string, routeName: string) => void\n  /**\n   * Sync i18n locale from the VitePress URL path on every navigation.\n   * @default true\n   */\n  syncWithVitePress?: boolean\n  /**\n   * Map VitePress locale keys to i18n codes (`root` → default locale code).\n   */\n  localeKeyToCode?: Record<string, string>\n  /**\n   * VitePress `site.base`. Needed so SSG paths (`withBase`) and lang switcher\n   * do not treat the base segment as content / double-prefix links.\n   */\n  base?: string\n}\n\nexport interface VitePressSiteDataLike {\n  locales?: Record<string, { lang?: string; link?: string; label?: string }>\n}\n\nexport type CreateI18nResult = I18nPlugin &\n  PathMethods & {\n    /** Same as `.global` (VueI18n + path methods). */\n    i18n: I18nPlugin['global'] & PathMethods\n    enhanceApp: (ctx: { app: App; router: VitePressRouterLike; siteData?: Ref<VitePressSiteDataLike> | VitePressSiteDataLike }) => void\n  }\n\n// VitePress before 1.6 calls only `onAfterRouteChanged`; 1.6 prefers `onAfterRouteChange`, and 2 knows only that one.\n// No version calls both, so sync runs once per navigation.\nconst ROUTE_HOOKS = ['onAfterRouteChange', 'onAfterRouteChanged'] as const\ntype RouteHook = (typeof ROUTE_HOOKS)[number]\ntype RouteHandler = (to: string) => unknown\n\nfunction applyRouteMessages(\n  plugin: I18nPlugin,\n  root: Record<string, Translations> | undefined,\n  routeMessages: Record<string, Record<string, Translations>> | undefined,\n): void {\n  if (!routeMessages) return\n  for (const [routeName, byLocale] of Object.entries(routeMessages)) {\n    for (const [locale, translations] of Object.entries(byLocale)) {\n      plugin.global.addRouteTranslations(locale, routeName, translations, false)\n    }\n  }\n}\n\n/**\n * Universal VitePress i18n: Vue plugin + path helpers + `enhanceApp` sync.\n * Path methods are the router adapter’s own functions (no wrapper layer).\n */\nexport function createI18n(options: CreateI18nOptions): CreateI18nResult {\n  const defaultLocale = options.defaultLocale || options.locale\n  const locales = options.locales ?? []\n  const syncWithVitePress = options.syncWithVitePress !== false\n\n  const plugin = createVueI18n({\n    locale: options.locale,\n    fallbackLocale: options.fallbackLocale ?? defaultLocale,\n    messages: options.messages,\n    plural: options.plural,\n    missingWarn: options.missingWarn,\n    missingHandler: options.missingHandler,\n    numberFormats: options.numberFormats,\n    datetimeFormats: options.datetimeFormats,\n    timeZone: options.timeZone,\n    locales,\n    defaultLocale,\n  })\n\n  applyRouteMessages(plugin, options.messages, options.routeMessages)\n\n  const paths = createVitePressRouterAdapter({\n    locales,\n    defaultLocale,\n    localeKeyToCode: options.localeKeyToCode,\n    base: options.base,\n  })\n  const pathMethods: PathMethods = {\n    localizePath: paths.localizePath,\n    switchLocalePath: paths.switchLocalePath,\n    getLocaleFromPath: paths.getLocaleFromPath,\n    removeLocaleFromPath: paths.removeLocaleFromPath,\n    routeNameFromPath: paths.routeNameFromPath,\n  }\n  plugin.global.extend(pathMethods)\n\n  const pageRouteNames = new Set(Object.keys(options.routeMessages ?? {}))\n\n  const byApp = new WeakMap<\n    object,\n    {\n      adapter: VitePressRouterAdapter\n      boundSyncHandlers: Partial<Record<RouteHook, RouteHandler>>\n      chainedPrevious: Partial<Record<RouteHook, RouteHandler>>\n    }\n  >()\n\n  const enhanceApp = (ctx: { app: App; router: VitePressRouterLike; siteData?: Ref<VitePressSiteDataLike> | VitePressSiteDataLike }) => {\n    const { app, router } = ctx\n\n    let state = byApp.get(app)\n    if (!state) {\n      const adapter = createVitePressRouterAdapter({\n        locales,\n        defaultLocale,\n        localeKeyToCode: options.localeKeyToCode,\n        base: options.base,\n        getPath: () => {\n          const route = router.route\n          return `${route.path}${route.query || ''}${route.hash || ''}`\n        },\n        go: (href, navOptions) => router.go(href, navOptions),\n      })\n\n      app.use(plugin)\n      plugin.setRoutingStrategy(adapter)\n      state = { adapter, boundSyncHandlers: {}, chainedPrevious: {} }\n      byApp.set(app, state)\n    }\n\n    if (!syncWithVitePress) return\n\n    const { adapter } = state\n    const sync = (path = router.route.path) => {\n      const nextLocale = adapter.getLocaleFromPath(path)\n      if (plugin.global.getLocale() !== nextLocale) {\n        plugin.global.locale = nextLocale\n      }\n      // the route is a page's name only when it has a dictionary (root keys resolve either way)\n      const derived = adapter.routeNameFromPath(path)\n      plugin.global.setRoute(pageRouteNames.has(derived) ? derived : 'index')\n    }\n\n    sync()\n\n    for (const hook of ROUTE_HOOKS) {\n      const current = router[hook]\n      if (current !== state.boundSyncHandlers[hook]) {\n        state.chainedPrevious[hook] = typeof current === 'function' ? current : undefined\n      }\n      const handler = async (to: string) => {\n        await state.chainedPrevious[hook]?.(to)\n        const path = to.startsWith('http')\n          ? (() => {\n              const url = new URL(to)\n              return url.pathname + url.search + url.hash\n            })()\n          : to\n        sync(path)\n      }\n      state.boundSyncHandlers[hook] = handler\n      router[hook] = handler\n    }\n  }\n\n  return Object.assign(plug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