{"version":3,"file":"IntlayerProvider.cjs","names":["createContext","getLocaleInStorage","internationalization","useContext","useState","useCallback","localeResolver","useMemo","EditorProvider","AnalyticsProvider"],"sources":["../../../src/client/IntlayerProvider.tsx"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { setIntlayerIdentifier } from '@intlayer/config/client';\nimport { localeResolver } from '@intlayer/core/localization';\nimport type {\n  LocalesValues,\n  ProviderVariant,\n} from '@intlayer/types/module_augmentation';\nimport {\n  type ComponentChild,\n  createContext,\n  type FunctionComponent,\n} from 'preact';\nimport {\n  useCallback,\n  useContext,\n  useEffect,\n  useMemo,\n  useState,\n} from 'preact/hooks';\nimport { AnalyticsProvider } from '../analytics/AnalyticsProvider';\nimport { EditorProvider } from '../editor/EditorProvider';\nimport { getLocaleInStorage, setLocaleInStorage } from './useLocaleStorage';\n\ntype IntlayerValue = {\n  locale: LocalesValues;\n  setLocale: (newLocale: LocalesValues) => void;\n  /**\n   * Ambient variant applied to every dictionary read below the provider, the\n   * same way `locale` is. Overridden per call by an explicit selector.\n   */\n  variant?: ProviderVariant;\n  isCookieEnabled?: boolean;\n};\n\n/**\n * Context that store the current locale on the client side\n */\nexport const IntlayerClientContext = createContext<IntlayerValue>({\n  // Read on demand: only a tree rendered without a provider needs it\n  get locale() {\n    return getLocaleInStorage() ?? internationalization?.defaultLocale;\n  },\n  setLocale: () => null,\n});\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayerContext = () => useContext(IntlayerClientContext) ?? {};\n\nexport type IntlayerProviderProps = {\n  children?: ComponentChild;\n  locale?: LocalesValues;\n  defaultLocale?: LocalesValues;\n  /**\n   * Ambient variant applied to every dictionary read below this provider — for\n   * a dimension that is fixed for the whole session (tenant, school type, plan\n   * tier…) and that therefore no component should have to pass by hand.\n   *\n   * Accepts three forms:\n   * - `variant=\"school1\"` — one named variant for every key\n   * - `variant={['school1', 'default']}` — an ordered preference chain: the\n   *   first variant the key actually declares wins\n   * - `variant={{ key1: 'school1', default: 'base' }}` — per dictionary key,\n   *   with `default` covering every key not listed\n   *\n   * A plain object is always read as the per-key map. To pin a structured\n   * variant globally, nest it: `variant={{ default: { id: 'prod_abc' } }}`.\n   *\n   * A call-site selector always wins.\n   */\n  variant?: ProviderVariant;\n  setLocale?: (locale: LocalesValues) => void;\n  isCookieEnabled?: boolean;\n};\n\n/**\n * Provider that store the current locale on the client side\n */\nexport const IntlayerProviderContent: FunctionComponent<\n  IntlayerProviderProps\n> = ({\n  locale: localeProp,\n  defaultLocale: defaultLocaleProp,\n  variant,\n  children,\n  setLocale: setLocaleProp,\n  isCookieEnabled,\n}) => {\n  const { defaultLocale: defaultLocaleConfig, locales: availableLocales } =\n    internationalization ?? {};\n\n  // Storage is only read when no locale is passed, and only on mount\n  const [currentLocale, setCurrentLocale] = useState<LocalesValues>(\n    () =>\n      (localeProp ??\n        getLocaleInStorage() ??\n        defaultLocaleProp ??\n        defaultLocaleConfig) as LocalesValues\n  );\n\n  useEffect(() => {\n    setIntlayerIdentifier();\n  }, []);\n\n  // The prop wins over the state. It is read directly instead of being mirrored\n  // into state by an effect, which re-rendered the subtree after every change.\n  const activeLocale = localeProp ?? currentLocale;\n\n  const setLocaleBase = useCallback(\n    (newLocale: LocalesValues) => {\n      if (activeLocale.toString() === newLocale.toString()) return;\n\n      if (!availableLocales?.map(String).includes(newLocale)) {\n        console.error(`Locale ${newLocale} is not available`);\n        return;\n      }\n\n      setCurrentLocale(newLocale); // Update state\n      setLocaleInStorage(newLocale, isCookieEnabled ?? true); // Optionally set cookie for persistence\n    },\n    [activeLocale, availableLocales, isCookieEnabled]\n  );\n\n  const setLocale = setLocaleProp ?? setLocaleBase;\n\n  const resolvedLocale = localeResolver(activeLocale);\n\n  // Stable value so a parent re-render does not re-render every consumer\n  const contextValue = useMemo<IntlayerValue>(\n    () => ({\n      locale: resolvedLocale,\n      setLocale,\n      variant,\n      isCookieEnabled,\n    }),\n    [resolvedLocale, setLocale, variant, isCookieEnabled]\n  );\n\n  return (\n    <IntlayerClientContext.Provider value={contextValue}>\n      {children}\n    </IntlayerClientContext.Provider>\n  );\n};\n\n/**\n * Main provider for Intlayer in Preact applications.\n *\n * It provides the Intlayer context to your application, allowing the use\n * of hooks like `useIntlayer` and `useLocale`.\n *\n * @param props - The provider props.\n * @returns The provider component.\n *\n * @example\n * ```tsx\n * import { IntlayerProvider } from 'preact-intlayer';\n *\n * const App = () => (\n *   <IntlayerProvider>\n *     <MyComponent />\n *   </IntlayerProvider>\n * );\n * ```\n */\nexport const IntlayerProvider: FunctionComponent<IntlayerProviderProps> = ({\n  children,\n  ...props\n}) => (\n  <IntlayerProviderContent {...props}>\n    {/* Build flags drop these, and their modules, when the feature is off */}\n    {process.env.INTLAYER_EDITOR_ENABLED !== 'false' && <EditorProvider />}\n    {process.env.INTLAYER_ANALYTICS_ENABLED !== 'false' && (\n      <AnalyticsProvider />\n    )}\n    {children}\n  </IntlayerProviderContent>\n);\n"],"mappings":";;;;;;;;;;;;;;;AAqCA,MAAa,4BAAwBA,sBAA6B;CAEhE,IAAI,SAAS;EACX,OAAOC,mDAAmB,KAAKC,6CAAsB;CACvD;CACA,iBAAiB;AACnB,CAAC;;;;AAKD,MAAa,+BAA2BC,yBAAW,qBAAqB,KAAK,CAAC;;;;AA+B9E,MAAa,2BAER,EACH,QAAQ,YACR,eAAe,mBACf,SACA,UACA,WAAW,eACX,sBACI;CACJ,MAAM,EAAE,eAAe,qBAAqB,SAAS,qBACnDD,+CAAwB,CAAC;CAG3B,MAAM,CAAC,eAAe,wBAAoBE,6BAErC,cACCH,mDAAmB,KACnB,qBACA,mBACN;CAEA,kCAAgB;EACd,mDAAsB;CACxB,GAAG,CAAC,CAAC;CAIL,MAAM,eAAe,cAAc;CAEnC,MAAM,oBAAgBI,2BACnB,cAA6B;EAC5B,IAAI,aAAa,SAAS,MAAM,UAAU,SAAS,GAAG;EAEtD,IAAI,CAAC,kBAAkB,IAAI,MAAM,CAAC,CAAC,SAAS,SAAS,GAAG;GACtD,QAA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