{"version":3,"file":"withGTStaticProps.mjs","names":[],"sources":["../../src/pages-dir/withGTStaticProps.ts"],"sourcesContent":["import type { GetStaticProps, GetStaticPropsContext, PreviewData } from 'next';\nimport type { ParsedUrlQuery } from 'querystring';\nimport { getI18nConfig } from '@generaltranslation/react-core/pure';\nimport { getTranslationsSnapshot } from 'gt-react';\nimport { withGTStaticPropsLocaleRoutingError } from '../errors/ssg';\nimport { resolvePagesRouterLocale } from './parseLocale';\n\ntype TranslationsSnapshot = Awaited<ReturnType<typeof getTranslationsSnapshot>>;\n\ntype GTStaticProps = {\n  locale: string;\n  translations: TranslationsSnapshot;\n};\n\nexport type WithGTStaticProps<\n  Props extends Record<string, unknown> = Record<string, unknown>,\n> = Props & GTStaticProps;\n\n/**\n * Wraps a Pages Router getStaticProps() function with locale-specific GT props.\n * Next.js Pages Router locale routing must be configured so getStaticProps()\n * runs once for each locale.\n */\nexport function withGTStaticProps<\n  Props extends Record<string, unknown> = Record<string, unknown>,\n  Params extends ParsedUrlQuery = ParsedUrlQuery,\n  Preview extends PreviewData = PreviewData,\n>(\n  getStaticProps?: GetStaticProps<Props, Params, Preview>\n): GetStaticProps<WithGTStaticProps<Props>, Params, Preview> {\n  return async (context: GetStaticPropsContext<Params, Preview>) => {\n    if (!context.locale && !context.defaultLocale) {\n      throw new Error(withGTStaticPropsLocaleRoutingError);\n    }\n\n    const result = getStaticProps\n      ? await getStaticProps(context)\n      : { props: {} as Props };\n\n    if (!('props' in result)) {\n      return result;\n    }\n\n    const props = await result.props;\n    const i18nConfig = getI18nConfig();\n    const defaultLocale = i18nConfig.getDefaultLocale();\n    const locale = resolvePagesRouterLocale(context);\n    const translations =\n      locale === defaultLocale ? {} : await getTranslationsSnapshot(locale);\n\n    return {\n      ...result,\n      props: {\n        ...props,\n        locale,\n        translations,\n      },\n    };\n  };\n}\n\nexport type WithGTStaticPropsFunction = typeof withGTStaticProps;\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,kBAKd,gBAC2D;AAC3D,QAAO,OAAO,YAAoD;AAChE,MAAI,CAAC,QAAQ,UAAU,CAAC,QAAQ,cAC9B,OAAM,IAAI,MAAM,oCAAoC;EAGtD,MAAM,SAAS,iBACX,MAAM,eAAe,QAAQ,GAC7B,EAAE,OAAO,EAAE,EAAW;AAE1B,MAAI,EAAE,WAAW,QACf,QAAO;EAGT,MAAM,QAAQ,MAAM,OAAO;EAE3B,MAAM,gBADa,eACa,CAAC,kBAAkB;EACnD,MAAM,SAAS,yBAAyB,QAAQ;EAChD,MAAM,eACJ,WAAW,gBAAgB,EAAE,GAAG,MAAM,wBAAwB,OAAO;AAEvE,SAAO;GACL,GAAG;GACH,OAAO;IACL,GAAG;IACH;IACA;IACD;GACF"}