{"version":3,"file":"getLocale.mjs","names":[],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { DEFAULT_LOCALE } from '@intlayer/config/defaultValues';\nimport { localeDetector } from '@intlayer/core/localization';\nimport { getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\n/**\n * Helper function to extract the current locale from Next.js headers and cookies.\n *\n * This function is designed to be used in Server Components, Server Actions, or Route Handlers\n * to determine the locale preferred by the user or stored in their preferences.\n *\n * @returns A promise that resolves to the detected `Locale`.\n *\n * @example\n * ```tsx\n * import { getLocale } from 'next-intlayer/server';\n *\n * export default async function MyServerComponent() {\n *   const locale = await getLocale();\n *   // ...\n * }\n * ```\n */\nexport const getLocale = async (): Promise<Locale> => {\n  const defaultLocale = internationalization?.defaultLocale ?? DEFAULT_LOCALE;\n\n  // Try locale from header\n  const headersList = await headers();\n  const cookiesList = await cookies();\n\n  const storedLocale = getLocaleFromStorageServer({\n    getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n    getHeader: (name: string) => headersList.get(name) ?? null,\n  });\n\n  if (storedLocale) return storedLocale as Locale;\n\n  // Fallback to Accept-Language negotiation\n  const negotiatorHeaders: Record<string, string> = {};\n  headersList.forEach((value, key) => {\n    negotiatorHeaders[key] = value;\n  });\n\n  const userFallbackLocale = localeDetector(negotiatorHeaders);\n  if (userFallbackLocale) return userFallbackLocale as Locale;\n\n  // Default locale\n  return defaultLocale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBAAgB,sBAAsB,iBAAiB;CAG7D,MAAM,cAAc,MAAM,QAAQ;CAClC,MAAM,cAAc,MAAM,QAAQ;CAElC,MAAM,eAAe,2BAA2B;EAC9C,YAAY,SAAiB,YAAY,IAAI,IAAI,GAAG,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,IAAI,KAAK;CACxD,CAAC;CAED,IAAI,cAAc,OAAO;CAGzB,MAAM,oBAA4C,CAAC;CACnD,YAAY,SAAS,OAAO,QAAQ;EAClC,kBAAkB,OAAO;CAC3B,CAAC;CAED,MAAM,qBAAqB,eAAe,iBAAiB;CAC3D,IAAI,oBAAoB,OAAO;CAG/B,OAAO;AACT"}