{"version":3,"file":"index.mjs","names":[],"sources":["../../src/ConfigProvider/index.tsx"],"sourcesContent":["'use client';\n\nimport {\n  createContext,\n  type ElementType,\n  memo,\n  type ReactNode,\n  use,\n  useEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\n\nimport {\n  type I18nContextValue,\n  type TranslationKey,\n  type TranslationResourcesInput,\n  type TranslationResourcesMap,\n} from '@/i18n/types';\nimport { MotionComponent, type MotionComponentType } from '@/MotionProvider';\nimport { type CDN, type CdnApi, genCdnUrl } from '@/utils/genCdnUrl';\n\nexport interface Config {\n  aAs?: ElementType;\n  customCdnFn?: CdnFn;\n  imgAs?: ElementType;\n  imgUnoptimized?: boolean;\n  proxy?: CDN | 'custom';\n}\n\nexport const ConfigContext = createContext<Config | null>(null);\n\n// Internal i18n context\nconst I18nContextInternal = createContext<I18nContextValue>({\n  locale: 'en',\n  t: (key: TranslationKey) => key,\n});\n\nexport interface ConfigProviderProps {\n  children: ReactNode;\n  config?: Config;\n  // i18n props - flattened at top level\n  locale?: string;\n  motion: MotionComponentType;\n  resources?: TranslationResourcesInput;\n}\n\nconst isThenable = (value: unknown): value is Promise<TranslationResourcesMap> =>\n  typeof (value as { then?: unknown })?.then === 'function';\n\nconst ConfigProvider = memo<ConfigProviderProps>(\n  ({ children, config, locale, resources, motion }) => {\n    const fallbackLocale = locale ?? 'en';\n    const [resolvedResources, setResolvedResources] = useState<TranslationResourcesMap | undefined>(\n      () => (resources && !isThenable(resources) ? resources : undefined),\n    );\n    const [resolvedLocale, setResolvedLocale] = useState(fallbackLocale);\n    const latestRequestId = useRef(0);\n\n    useEffect(() => {\n      const requestId = ++latestRequestId.current;\n\n      if (!resources) {\n        setResolvedResources(undefined);\n        setResolvedLocale(fallbackLocale);\n        return;\n      }\n\n      if (isThenable(resources)) {\n        const targetLocale = fallbackLocale;\n        resources\n          .then((nextResources) => {\n            if (latestRequestId.current !== requestId) return;\n            setResolvedResources(nextResources);\n            setResolvedLocale(targetLocale);\n          })\n          .catch(() => {\n            if (latestRequestId.current !== requestId) return;\n          });\n        return;\n      }\n\n      setResolvedResources(resources);\n      setResolvedLocale(fallbackLocale);\n    }, [fallbackLocale, resources]);\n\n    const currentResources = isThenable(resources) ? resolvedResources : resources;\n    const currentLocale = isThenable(resources) ? resolvedLocale : fallbackLocale;\n\n    const i18nValue = useMemo((): I18nContextValue => {\n      const resourceList = Array.isArray(currentResources)\n        ? currentResources\n        : currentResources\n          ? Object.values(currentResources)\n          : [];\n      const mergedResources = Object.assign({}, ...resourceList);\n      const t = (key: TranslationKey): string => mergedResources[key] || key;\n      return { locale: currentLocale, t };\n    }, [currentLocale, currentResources]);\n\n    return (\n      <I18nContextInternal value={i18nValue}>\n        <ConfigContext value={config ?? null}>\n          <MotionComponent value={motion}>{children}</MotionComponent>\n        </ConfigContext>\n      </I18nContextInternal>\n    );\n  },\n);\n\n// useCdnFn\nexport type CdnFn = ({ pkg, version, path }: CdnApi) => string;\n\nconst cdnFallback: CdnFn = ({ pkg, version, path }) =>\n  genCdnUrl({ path, pkg, proxy: 'aliyun', version });\n\nexport const useCdnFn = (): CdnFn => {\n  const config = use(ConfigContext);\n  if (!config) return cdnFallback;\n  if (config?.proxy !== 'custom')\n    return ({ pkg, version, path }) =>\n      genCdnUrl({ path, pkg, proxy: config.proxy as any, version });\n  return config?.customCdnFn || cdnFallback;\n};\n\n// useI18n hook\nexport const useI18n = () => use(I18nContextInternal);\n\n// Export I18nContext for external reference\nexport { I18nContextInternal as I18nContext };\n\nexport default ConfigProvider;\n"],"mappings":";;;;;;AA+BA,MAAa,gBAAgB,cAA6B,IAAI;AAG9D,MAAM,sBAAsB,cAAgC;CAC1D,QAAQ;CACR,IAAI,QAAwB;AAC9B,CAAC;AAWD,MAAM,cAAc,UAClB,OAAQ,OAA8B,SAAS;AAEjD,MAAM,iBAAiB,MACpB,EAAE,UAAU,QAAQ,QAAQ,WAAW,aAAa;CACnD,MAAM,iBAAiB,UAAU;CACjC,MAAM,CAAC,mBAAm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