{"version":3,"file":"serverContext.cjs","names":[],"sources":["../../../src/server/serverContext.tsx"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer value={locale}>\n *      {children}\n * </IntlayerServer>\n */\nimport react, { type FC, type PropsWithChildren, type ReactNode } from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T,>(defaultValue?: T): ServerContext<T> => {\n  throwInClient();\n\n  /** @ts-ignore remove error Property 'cache' does not exist on type 'typeof React'. */\n  const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n  const getCache = cache(() => ({\n    value: undefined,\n  }));\n\n  const Provider: FC<PropsWithChildren<{ value?: T }>> = ({\n    children,\n    value,\n  }) => {\n    getCache().value = value;\n    return children;\n  };\n\n  const ServerContext = Provider as ServerContext<T>;\n  ServerContext.Provider = Provider;\n  ServerContext.Consumer = (props) => {\n    const store = getCache();\n    return props.children(store ? store.value : defaultValue);\n  };\n  ServerContext._storage = getCache;\n  ServerContext._defaultValue = defaultValue;\n\n  return ServerContext;\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T,>({\n  _storage,\n  _defaultValue,\n}: ServerContext<T>) => {\n  // throwInClient();\n  const store = _storage();\n  if (!store) return _defaultValue;\n  return store.value;\n};\n\ntype ServerContext<T> = FC<PropsWithChildren<{ value?: T }>> & {\n  Provider: FC<PropsWithChildren<{ value?: T }>>;\n  Consumer: FC<\n    PropsWithChildren<{ children: (context: T | undefined) => ReactNode }>\n  >;\n  _storage: () => { value: T | undefined };\n  _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): undefined | never => {\n  // If window.document is defined we're in a client component\n  if (typeof window !== 'undefined') {\n    throw new Error(`createServerContext only works in Server Components`);\n  }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,MAAM,6BAA6B,EAAE,OAAO,QAAW;AAEvD,MAAa,uBAA2B,iBAAuC;AAC7E,gBAAe;CAKf,MAAM,YAFQ,cAAM,SAAuB,sBAEb,EAC5B,OAAO,QACR,EAAE;CAEH,MAAM,YAAkD,EACtD,UACA,YACI;AACJ,YAAU,CAAC,QAAQ;AACnB,SAAO;;CAGT,MAAM,gBAAgB;AACtB,eAAc,WAAW;AACzB,eAAc,YAAY,UAAU;EAClC,MAAM,QAAQ,UAAU;AACxB,SAAO,MAAM,SAAS,QAAQ,MAAM,QAAQ,aAAa;;AAE3D,eAAc,WAAW;AACzB,eAAc,gBAAgB;AAE9B,QAAO;;;;;;;;;AAUT,MAAa,oBAAwB,EACnC,UACA,oBACsB;CAEtB,MAAM,QAAQ,UAAU;AACxB,KAAI,CAAC,MAAO,QAAO;AACnB,QAAO,MAAM;;;;;;AAgBf,MAAM,sBAAyC;AAE7C,KAAI,OAAO,WAAW,YACpB,OAAM,IAAI,MAAM,sDAAsD"}