{"version":3,"file":"useLocalePageRouter.cjs","names":["useRouter","useMemo","getPathWithoutLocale","useCallback","getLocalizedUrl","useLocaleReact"],"sources":["../../../src/client/useLocalePageRouter.ts"],"sourcesContent":["'use client';\n\nimport {\n  getLocalizedUrl,\n  getPathWithoutLocale,\n} from '@intlayer/core/localization';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type {\n  DeclaredLocales,\n  LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { useRouter } from 'next/router.js';\nimport { useCallback, useMemo } from 'react';\nimport { useLocale as useLocaleReact } from 'react-intlayer';\nimport type { UseLocaleProps, UseLocaleResult } from './useLocale';\n\n/**\n * Hook to manage the current locale in Next.js Page Router.\n *\n * This hook provides locale management functionality tailored for the Next.js Page Router,\n * handling redirections and page reloads upon locale changes.\n *\n * @returns An object containing the current locale, path without locale, and functions to update the locale.\n *\n * @example\n * ```tsx\n * import { useLocalePageRouter } from 'next-intlayer';\n *\n * const MyComponent = () => {\n *   const { setLocale } = useLocalePageRouter();\n *\n *   return (\n *     <button onClick={() => setLocale('fr')}>Switch to French</button>\n *   );\n * };\n * ```\n */\nexport const useLocalePageRouter = ({\n  onLocaleChange,\n  isCookieEnabled,\n}: UseLocaleProps = {}): UseLocaleResult => {\n  const { push, pathname, reload } = useRouter();\n  const pathWithoutLocale = useMemo(\n    () => getPathWithoutLocale(pathname),\n    [pathname]\n  );\n\n  const redirectionFunction = useCallback(\n    (locale: LocalesValues) => {\n      // `redirectionFunction` accepts any string, while `getLocalizedUrl` is\n      // constrained to the declared locales.\n      const pathWithLocale = getLocalizedUrl(pathWithoutLocale, locale);\n\n      push(pathWithLocale);\n\n      onLocaleChange?.(locale as Locale);\n\n      return reload();\n    },\n    [pathWithoutLocale, onLocaleChange, reload]\n  );\n\n  const reactLocaleHook = useLocaleReact({\n    onLocaleChange: redirectionFunction,\n    isCookieEnabled,\n  });\n\n  return {\n    ...reactLocaleHook,\n    pathWithoutLocale,\n  } as UseLocaleResult;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,MAAa,uBAAuB,EAClC,gBACA,oBACkB,CAAC,MAAuB;CAC1C,MAAM,EAAE,MAAM,UAAU,eAAWA,0BAAU;CAC7C,MAAM,wBAAoBC,yBAClBC,kDAAqB,QAAQ,GACnC,CAAC,QAAQ,CACX;CAEA,MAAM,0BAAsBC,oBACzB,WAA0B;EAGzB,MAAM,qBAAiBC,6CAAgB,mBAAmB,MAAM;EAEhE,KAAK,cAAc;EAEnB,iBAAiB,MAAgB;EAEjC,OAAO,OAAO;CAChB,GACA;EAAC;EAAmB;EAAgB;CAAM,CAC5C;CAOA,OAAO;EACL,OANsBC,0BAAe;GACrC,gBAAgB;GAChB;EACF,CAGmB;EACjB;CACF;AACF"}