{
  "version": 3,
  "sources": ["../src/create-context.tsx"],
  "sourcesContent": ["import * as React from 'react';\n\ntype ContextProvider<ContextValueType extends object | null> = React.FC<\n  ContextValueType & {\n    children: React.ReactNode;\n  }\n>;\n\ninterface UseAssertedContext<ContextValueType extends object | null> {\n  (consumerName: string): ContextValueType;\n}\n\ninterface UseContext<ContextValueType extends object | null> {\n  (consumerName: string): ContextValueType;\n  (consumerName: string, options: { optional: true }): ContextValueType | undefined;\n}\n\nfunction createContext<ContextValueType extends object | null>(\n  rootComponentName: string,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>];\nfunction createContext<ContextValueType extends object | null>(\n  rootComponentName: string,\n  defaultContext: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseAssertedContext<ContextValueType>];\n\n/* @__NO_SIDE_EFFECTS__ */ function createContext<ContextValueType extends object | null>(\n  rootComponentName: string,\n  defaultContext?: ContextValueType,\n): readonly [ContextProvider<ContextValueType>, UseContext<ContextValueType>] {\n  const Context = React.createContext<ContextValueType | undefined>(defaultContext);\n  Context.displayName = rootComponentName + 'Context';\n\n  const Provider: ContextProvider<ContextValueType> = (props) => {\n    const { children, ...context } = props;\n    // Only re-memoize when prop values change\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n    const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n    return <Context.Provider value={value}>{children}</Context.Provider>;\n  };\n\n  Provider.displayName = rootComponentName + 'Provider';\n\n  function useContext(consumerName: string): ContextValueType;\n  function useContext(\n    consumerName: string,\n    options: { optional: true },\n  ): ContextValueType | undefined;\n\n  function useContext(\n    consumerName: string,\n    options: { optional?: boolean } = {},\n  ): ContextValueType | undefined {\n    const { optional = false } = options;\n    const context = React.useContext(Context);\n    if (context) return context;\n    if (defaultContext !== undefined) return defaultContext;\n    if (optional) return undefined;\n    // if a defaultContext wasn't specified, it's a required context.\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n  }\n\n  return [Provider, useContext];\n}\n\n/* -------------------------------------------------------------------------------------------------\n * createContextScope\n * -----------------------------------------------------------------------------------------------*/\n\ntype Scope<C = any> = { [scopeName: string]: React.Context<C>[] } | undefined;\ntype ScopeHook = (scope: Scope) => { [__scopeProp: string]: Scope };\ninterface CreateScope {\n  scopeName: string;\n  (): ScopeHook;\n}\n\ntype ScopedContextProvider<ContextValueType extends object | null> = React.FC<\n  ContextValueType & {\n    scope: Scope<ContextValueType>;\n    children: React.ReactNode;\n  }\n>;\n\ninterface UseScopedContext<ContextValueType extends object | null> {\n  (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n  (\n    consumerName: string,\n    scope: Scope<ContextValueType | undefined>,\n    options: { optional?: true },\n  ): ContextValueType | undefined;\n}\n\ninterface UseAssertedScopedContext<ContextValueType extends object | null> {\n  (consumerName: string, scope: Scope<ContextValueType | undefined>): ContextValueType;\n}\n\ninterface CreateScopedContext {\n  <ContextValueType extends object | null>(\n    rootComponentName: string,\n  ): readonly [ScopedContextProvider<ContextValueType>, UseScopedContext<ContextValueType>];\n  <ContextValueType extends object | null>(\n    rootComponentName: string,\n    defaultContext: ContextValueType,\n  ): readonly [ScopedContextProvider<ContextValueType>, UseAssertedScopedContext<ContextValueType>];\n}\n\n/* @__NO_SIDE_EFFECTS__ */ function createContextScope(\n  scopeName: string,\n  createContextScopeDeps: CreateScope[] = [],\n): readonly [CreateScopedContext, CreateScope] {\n  let defaultContexts: any[] = [];\n\n  /* -----------------------------------------------------------------------------------------------\n   * createContext\n   * ---------------------------------------------------------------------------------------------*/\n\n  function createContext<ContextValueType extends object | null>(\n    rootComponentName: string,\n    defaultContext?: ContextValueType,\n  ) {\n    const BaseContext = React.createContext<ContextValueType | undefined>(defaultContext);\n    BaseContext.displayName = rootComponentName + 'Context';\n    const index = defaultContexts.length;\n    defaultContexts = [...defaultContexts, defaultContext];\n\n    const Provider: ScopedContextProvider<ContextValueType> = (props) => {\n      const { scope, children, ...context } = props;\n      const Context = scope?.[scopeName]?.[index] || BaseContext;\n      // Only re-memoize when prop values change\n      // eslint-disable-next-line react-hooks/exhaustive-deps\n      const value = React.useMemo(() => context, Object.values(context)) as ContextValueType;\n      return <Context.Provider value={value}>{children}</Context.Provider>;\n    };\n\n    Provider.displayName = rootComponentName + 'Provider';\n\n    function useContext(\n      consumerName: string,\n      scope: Scope<ContextValueType | undefined>,\n    ): ContextValueType;\n    function useContext(\n      consumerName: string,\n      scope: Scope<ContextValueType | undefined>,\n      options: { optional: true },\n    ): ContextValueType | undefined;\n\n    function useContext(\n      consumerName: string,\n      scope: Scope<ContextValueType | undefined>,\n      options: { optional?: boolean } = {},\n    ): ContextValueType | undefined {\n      const { optional = false } = options;\n      const Context = scope?.[scopeName]?.[index] || BaseContext;\n      const context = React.useContext(Context);\n      if (context) return context;\n      if (defaultContext !== undefined) return defaultContext;\n      if (optional) return undefined;\n      // if a defaultContext wasn't specified, it's a required context.\n      throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n    }\n\n    return [Provider, useContext] as const;\n  }\n\n  /* -----------------------------------------------------------------------------------------------\n   * createScope\n   * ---------------------------------------------------------------------------------------------*/\n\n  const createScope: CreateScope = () => {\n    const scopeContexts = defaultContexts.map((defaultContext) => {\n      return React.createContext(defaultContext);\n    });\n    return function useScope(scope: Scope) {\n      const contexts = scope?.[scopeName] || scopeContexts;\n      return React.useMemo(\n        () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n        [scope, contexts],\n      );\n    };\n  };\n\n  createScope.scopeName = scopeName;\n  return [createContext, composeContextScopes(createScope, ...createContextScopeDeps)] as const;\n}\n\n/* -------------------------------------------------------------------------------------------------\n * composeContextScopes\n * -----------------------------------------------------------------------------------------------*/\n\nfunction composeContextScopes(...scopes: [CreateScope, ...CreateScope[]]): CreateScope {\n  const baseScope = scopes[0];\n  if (scopes.length === 1) return baseScope;\n\n  const createScope: CreateScope = () => {\n    const scopeHooks = scopes.map((createScope) => ({\n      useScope: createScope(),\n      scopeName: createScope.scopeName,\n    }));\n\n    return function useComposedScopes(overrideScopes) {\n      const nextScopes = scopeHooks.reduce((nextScopes, { useScope, scopeName }) => {\n        // We are calling a hook inside a callback which React warns against to avoid inconsistent\n        // renders, however, scoping doesn't have render side effects so we ignore the rule.\n        // eslint-disable-next-line react-hooks/rules-of-hooks\n        const scopeProps = useScope(overrideScopes);\n        const currentScope = scopeProps[`__scope${scopeName}`];\n        return { ...nextScopes, ...currentScope };\n      }, {});\n\n      return React.useMemo(() => ({ [`__scope${baseScope.scopeName}`]: nextScopes }), [nextScopes]);\n    };\n  };\n\n  createScope.scopeName = baseScope.scopeName;\n  return createScope;\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nexport { createContext, createContextScope };\nexport type {\n  CreateScope,\n  CreateScopedContext,\n  Scope,\n  UseAssertedContext,\n  UseAssertedScopedContext,\n  UseContext,\n  UseScopedContext,\n};\n"],
  "mappings": ";;;;AAAA,YAAY,WAAW;AAqCZ;AAAA;AAZgB,SAASA,eAClC,mBACA,gBAC4E;AAC5E,QAAM,UAAgB,oBAA4C,cAAc;AAChF,UAAQ,cAAc,oBAAoB;AAE1C,QAAM,WAA8C,wBAAC,UAAU;AAC7D,UAAM,EAAE,UAAU,GAAG,QAAQ,IAAI;AAGjC,UAAM,QAAc,cAAQ,MAAM,SAAS,OAAO,OAAO,OAAO,CAAC;AACjE,WAAO,oBAAC,QAAQ,UAAR,EAAiB,OAAe,UAAS;AAAA,EACnD,GANoD;AAQpD,WAAS,cAAc,oBAAoB;AAQ3C,WAASC,YACP,cACA,UAAkC,CAAC,GACL;AAC9B,UAAM,EAAE,WAAW,MAAM,IAAI;AAC7B,UAAM,UAAgB,iBAAW,OAAO;AACxC,QAAI,QAAS,QAAO;AACpB,QAAI,mBAAmB,OAAW,QAAO;AACzC,QAAI,SAAU,QAAO;AAErB,UAAM,IAAI,MAAM,KAAK,YAAY,4BAA4B,iBAAiB,IAAI;AAAA,EACpF;AAXS,SAAAA,aAAA;AAa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