{"version":3,"sources":["../../src/i18n/client.tsx"],"sourcesContent":["\"use client\";\n\nimport { useMemo, useSyncExternalStore } from \"react\";\nimport { createFarmLocaleCookie } from \"./resolver\";\nimport { localizeFarmHref } from \"./routing\";\nimport {\n  _hydrateFarmI18n,\n  createFarmClientTranslator,\n  getFarmI18nClientState,\n  subscribeFarmI18n,\n  t,\n} from \"./client-runtime\";\nimport type { FarmI18nClientSnapshot, FarmI18nLocale, FarmTranslator } from \"./types\";\n\nexport interface FarmLocaleState {\n  locale: FarmI18nLocale;\n  locales: readonly FarmI18nLocale[];\n  direction: \"ltr\" | \"rtl\";\n  setLocale(locale: FarmI18nLocale): void;\n}\n\ninterface FarmListFormatOptions {\n  localeMatcher?: \"lookup\" | \"best fit\";\n  type?: \"conjunction\" | \"disjunction\" | \"unit\";\n  style?: \"long\" | \"short\" | \"narrow\";\n}\n\nexport function useLocale(): FarmLocaleState {\n  const snapshot = useFarmI18nSnapshot();\n  return useMemo(\n    () => ({\n      locale: snapshot.locale as FarmI18nLocale,\n      locales: snapshot.locales as readonly FarmI18nLocale[],\n      direction: snapshot.direction,\n      setLocale,\n    }),\n    [snapshot],\n  );\n}\n\nexport function useTranslations(): FarmTranslator {\n  useFarmI18nSnapshot();\n  return createFarmClientTranslator();\n}\n\nexport function getLocale(): FarmI18nLocale {\n  return requireSnapshot().locale as FarmI18nLocale;\n}\n\nexport function getLocaleSource(): FarmI18nClientSnapshot[\"source\"] {\n  return requireSnapshot().source;\n}\n\nexport function createTranslator(): FarmTranslator {\n  return createFarmClientTranslator();\n}\n\nexport function setLocale(locale: FarmI18nLocale): void {\n  const snapshot = requireSnapshot();\n  if (!snapshot.locales.includes(locale)) {\n    throw new Error(`Unsupported Farm i18n locale \"${locale}\".`);\n  }\n  if (typeof window === \"undefined\") return;\n\n  document.cookie = createFarmLocaleCookie(locale, { cookie: snapshot.cookie });\n\n  const currentHref = `${window.location.pathname}${window.location.search}${window.location.hash}`;\n  const nextHref = localizeFarmHref(currentHref, locale, snapshot);\n  window.location.assign(nextHref);\n}\n\nexport function _setFarmI18nClientSnapshot(snapshot: FarmI18nClientSnapshot): void {\n  _hydrateFarmI18n(snapshot);\n}\n\nexport const format = {\n  number(value: number, options?: Intl.NumberFormatOptions): string {\n    return new Intl.NumberFormat(requireSnapshot().locale, options).format(value);\n  },\n  currency(\n    value: number,\n    currency: string,\n    options: Omit<Intl.NumberFormatOptions, \"style\" | \"currency\"> = {},\n  ): string {\n    return new Intl.NumberFormat(requireSnapshot().locale, {\n      ...options,\n      style: \"currency\",\n      currency,\n    }).format(value);\n  },\n  date(value: Date | number, options?: Intl.DateTimeFormatOptions): string {\n    return new Intl.DateTimeFormat(requireSnapshot().locale, options).format(value);\n  },\n  relativeTime(\n    value: number,\n    unit: Intl.RelativeTimeFormatUnit,\n    options?: Intl.RelativeTimeFormatOptions,\n  ): string {\n    return new Intl.RelativeTimeFormat(requireSnapshot().locale, options).format(value, unit);\n  },\n  list(values: Iterable<string>, options?: FarmListFormatOptions): string {\n    const ListFormat = (Intl as any).ListFormat;\n    return new ListFormat(requireSnapshot().locale, options).format(Array.from(values));\n  },\n};\n\nfunction useFarmI18nSnapshot(): FarmI18nClientSnapshot {\n  const snapshot = useSyncExternalStore(\n    subscribeFarmI18n,\n    getFarmI18nClientState,\n    getFarmI18nClientState,\n  );\n  if (!snapshot) {\n    throw new Error(\"Farm i18n is not configured or has not been hydrated.\");\n  }\n  return snapshot;\n}\n\nfunction requireSnapshot(): FarmI18nClientSnapshot {\n  const snapshot = getFarmI18nClientState();\n  if (!snapshot) {\n    throw new Error(\"Farm i18n is not configured or has not been hydrated.\");\n  }\n  return snapshot;\n}\n\nexport { t };\nexport type {\n  FarmI18nClientSnapshot,\n  FarmI18nLocale,\n  FarmI18nMessageArgs,\n  FarmI18nMessageKey,\n} from \"./types\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,SAAS,4BAA4B;AAyBvC,SAAS,YAA6B;AAC3C,QAAM,WAAW,o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