{"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/ProviderContext/index.ts","../src/errors.ts","../src/hooks/useNamedProvider.ts","../src/ProviderScope/index.tsx","../src/Consumer/index.tsx","../src/MultiProvider/index.tsx","../src/Provider/index.tsx","../src/hooks/useProvider.ts"],"sourcesContent":["import { Create } from './types'\n\nexport function getObjectRuntimeName(o: any) {\n  if (typeof o === 'string') {\n    return String.name\n  }\n  // if (typeof o === 'bigint') {\n  //   return BigInt.name\n  // }\n  if (typeof o === 'boolean') {\n    return Boolean.name\n  }\n  // if (typeof o === 'function') {\n  //   return Function.name\n  // }\n  if (typeof o === 'number') {\n    return Number.name\n  }\n  // if (typeof o === 'symbol') {\n  //   return Symbol.name\n  // }\n  if (typeof o === 'object') {\n    return o.constructor.name\n  }\n  return o.name\n}\n\nexport function isCreate<T>(source: Create<T> | T): source is Create<T> {\n  return typeof source === 'function'\n}\n","import { createContext } from 'react'\n\ntype ContextData = {\n  data: Map<any, any>\n}\n\nconst ProviderContext = createContext<ContextData | undefined>(undefined)\n\nexport type { ContextData }\nexport { ProviderContext }\n","class ResourcesNotProvidedError extends Error {\n  constructor(name: string) {\n    super(`Can't find ${name} in scope, make sure it is provided.`)\n    Object.setPrototypeOf(this, ResourcesNotProvidedError.prototype)\n  }\n}\n\nexport { ResourcesNotProvidedError }\n","import { useContext } from 'react'\nimport { ProviderContext } from '../ProviderContext'\nimport { ResourcesNotProvidedError } from '../errors'\n\ninterface UseNamedProviderConfig {\n  allowUndef?: boolean\n}\n\ninterface UseNamedProviderConfigAllowUndef extends UseNamedProviderConfig {\n  allowUndef: true\n}\n\ninterface UseNamedProviderConfigDisAllowUndef extends UseNamedProviderConfig {\n  allowUndef?: false\n}\nfunction useNamedProvider<T>(name: string): T\nfunction useNamedProvider<T>(name: string, configs: UseNamedProviderConfigAllowUndef): T | undefined\nfunction useNamedProvider<T>(name: string, configs: UseNamedProviderConfigDisAllowUndef): T\nfunction useNamedProvider<T>(name: string, configs?: UseNamedProviderConfig): T | undefined\nfunction useNamedProvider<T>(name: string, configs?: UseNamedProviderConfig): T | undefined {\n  const key = name\n  const parentDataContext = useContext(ProviderContext)\n  const parentData = parentDataContext?.data\n  const parentDataExisted = parentData?.has(key) ?? false\n  if (!parentDataExisted) {\n    if (configs?.allowUndef === true) {\n      return undefined\n    }\n    throw new ResourcesNotProvidedError(key)\n  }\n  const result = parentData?.get(key)\n  return result\n}\n\nexport default useNamedProvider\nexport type { UseNamedProviderConfigDisAllowUndef, UseNamedProviderConfig, UseNamedProviderConfigAllowUndef }\n","import { PropsWithChildren, useContext } from 'react'\nimport { ProviderContext } from '../ProviderContext'\nimport { getObjectRuntimeName } from '../utils'\n\ntype ProviderScopeProps<T> = {\n  value: T\n  name?: string\n}\n\nfunction ProviderScope<T>({ children, value, name }: PropsWithChildren<ProviderScopeProps<T>>) {\n  const parentDataContext = useContext(ProviderContext)\n  let parentData: Map<any, any>\n  if (parentDataContext?.data === undefined) {\n    parentData = new Map()\n  } else {\n    parentData = new Map(parentDataContext.data)\n  }\n\n  const key = name ?? getObjectRuntimeName(value)\n  parentData.set(key, value)\n  return <ProviderContext.Provider value={{ data: parentData }}>{children}</ProviderContext.Provider>\n}\nexport { ProviderScope }\nexport type { ProviderScopeProps }\n","import { useNamedProvider } from '../hooks'\nimport { Constructor } from '../types'\nimport { getObjectRuntimeName } from '../utils'\n\ninterface ConsumerProps<T> {\n  children: (data: T) => JSX.Element\n  name?: string\n  ctor?: Constructor<any>\n  allowUndef?: boolean\n}\n\ninterface NamedConsumerProps<T> extends ConsumerProps<T> {\n  name: string\n  ctor?: undefined\n}\n\ninterface NamedConsumerAllowUndefProps<T> extends NamedConsumerProps<T | undefined> {\n  allowUndef: true\n}\n\ninterface NamedConsumerDisAllowUndefProps<T> extends NamedConsumerProps<T> {\n  allowUndef?: false\n}\n\ninterface CtorConsumerProps<T> extends ConsumerProps<T> {\n  ctor: Constructor<T>\n}\n\ninterface CtorConsumerAllowUndefProps<T> extends CtorConsumerProps<T | undefined> {\n  allowUndef: true\n}\n\ninterface CtorConsumerDisAllowUndefProps<T> extends CtorConsumerProps<T> {\n  allowUndef?: false\n}\n\ninterface StringConsumerProps<T> extends ConsumerProps<T> {\n  ctor: StringConstructor\n}\n\ninterface StringConsumerAllowUndefProps extends StringConsumerProps<string | undefined> {\n  allowUndef: true\n}\n\ninterface StringConsumerDisAllowUndefProps extends StringConsumerProps<string> {\n  allowUndef?: false\n}\ninterface BooleanConsumerProps<T> extends ConsumerProps<T> {\n  ctor: BooleanConstructor\n}\ninterface BooleanConsumerAllowUndefProps extends BooleanConsumerProps<boolean | undefined> {\n  allowUndef: true\n}\n\ninterface BooleanConsumerDisAllowUndefProps extends BooleanConsumerProps<boolean> {\n  allowUndef?: false\n}\ninterface NumberConsumerProps<T> extends ConsumerProps<T> {\n  ctor: NumberConstructor\n}\ninterface NumberConsumerAllowUndefProps extends NumberConsumerProps<number | undefined> {\n  allowUndef: true\n}\ninterface NumberConsumerDisAllowUndefProps extends NumberConsumerProps<number> {\n  allowUndef?: false\n}\n\nfunction Consumer(props: NumberConsumerAllowUndefProps): JSX.Element\nfunction Consumer(props: NumberConsumerDisAllowUndefProps): JSX.Element\nfunction Consumer(props: BooleanConsumerAllowUndefProps): JSX.Element\nfunction Consumer(props: BooleanConsumerDisAllowUndefProps): JSX.Element\nfunction Consumer(props: StringConsumerAllowUndefProps): JSX.Element\nfunction Consumer(props: StringConsumerDisAllowUndefProps): JSX.Element\nfunction Consumer<T>(props: CtorConsumerAllowUndefProps<T>): JSX.Element\nfunction Consumer<T>(props: CtorConsumerDisAllowUndefProps<T>): JSX.Element\nfunction Consumer<T>(props: NamedConsumerAllowUndefProps<T>): JSX.Element\nfunction Consumer<T>(props: NamedConsumerDisAllowUndefProps<T>): JSX.Element\nfunction Consumer<T>(props: ConsumerProps<T | undefined>): JSX.Element {\n  const { children, name, ctor, allowUndef } = props\n  const queryName = name ?? getObjectRuntimeName(ctor)\n  const data = useNamedProvider<T>(queryName, { allowUndef })\n  return children(data)\n}\n\nexport { Consumer }\nexport type {\n  NumberConsumerAllowUndefProps,\n  NumberConsumerDisAllowUndefProps,\n  BooleanConsumerAllowUndefProps,\n  BooleanConsumerDisAllowUndefProps,\n  StringConsumerAllowUndefProps,\n  StringConsumerDisAllowUndefProps,\n  CtorConsumerAllowUndefProps,\n  CtorConsumerDisAllowUndefProps,\n  NamedConsumerDisAllowUndefProps,\n  NamedConsumerAllowUndefProps,\n}\n","import React from 'react'\nimport {\n  CElement,\n  DOMElement,\n  DetailedReactHTMLElement,\n  FunctionComponentElement,\n  PropsWithChildren,\n  ReactElement,\n  ReactHTMLElement,\n  ReactNode,\n  ReactSVGElement,\n} from 'react'\n\ntype ReceivableElement =\n  | DetailedReactHTMLElement<any, any>\n  | ReactHTMLElement<any>\n  | ReactSVGElement\n  | DOMElement<any, any>\n  | FunctionComponentElement<any>\n  | CElement<any, any>\n  | ReactElement<any>\ntype MultiProviderProps = {\n  providers: ReceivableElement[]\n}\nconst MultiProvider: React.FC<PropsWithChildren<MultiProviderProps>> = ({ providers, children }) => {\n  const renderNested = (nestedElements: ReceivableElement[], nestedChildren?: ReactNode): ReactElement | ReactNode => {\n    const [currentElement, ...remainingElement] = nestedElements\n    if (currentElement) {\n      return React.cloneElement(currentElement, undefined, renderNested(remainingElement, nestedChildren))\n    }\n    return children\n  }\n  return renderNested(providers, children)\n}\nexport type { ReceivableElement, MultiProviderProps }\nexport default MultiProvider\n","import { ReactNode, useEffect, useRef } from 'react'\nimport { Create } from '../types'\nimport { isCreate } from '../utils'\nimport { ProviderScope } from '../ProviderScope'\n\ninterface ProviderProps<T> {\n  source: Create<T> | T\n  cleanUp?: (data: T) => void\n  name?: string\n  children?: ReactNode\n}\n\ninterface CreateProviderProps<T> extends ProviderProps<T> {\n  source: Create<T>\n}\ninterface ValueProviderProps<T> extends ProviderProps<T> {\n  source: T\n  cleanUp?: undefined\n}\n\nfunction Provider<T>(props: ValueProviderProps<T>): JSX.Element\nfunction Provider<T>(props: CreateProviderProps<T>): JSX.Element\nfunction Provider<T>({ source, cleanUp, children, name }: ProviderProps<T>): JSX.Element {\n  const createdData = useRef(isCreate(source) ? source() : undefined)\n  const valueData: T | undefined = isCreate(source) ? undefined : source\n  useEffect(() => {\n    return () => {\n      // eslint-disable-next-line react-hooks/exhaustive-deps\n      const currentData = createdData.current\n      if (currentData !== undefined) {\n        cleanUp?.(currentData)\n      }\n    }\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [])\n  return (\n    <ProviderScope name={name} value={createdData.current ?? valueData}>\n      {children}\n    </ProviderScope>\n  )\n}\n\nexport type { ProviderProps, CreateProviderProps, ValueProviderProps }\nexport { Provider }\n","import { Constructor } from '../types'\nimport { getObjectRuntimeName } from '../utils'\nimport useNamedProvider from './useNamedProvider'\n\ninterface UseProviderConfigs {\n  allowUndef?: boolean\n  name?: string\n}\n\ninterface UseProviderConfigsAllowUndef extends UseProviderConfigs {\n  allowUndef: true\n}\ninterface UseProviderConfigsDisAllowUndef extends UseProviderConfigs {\n  allowUndef?: false\n}\n\nfunction useProvider(ctor: StringConstructor, configs: UseProviderConfigsAllowUndef): string | undefined\nfunction useProvider(ctor: StringConstructor, configs: UseProviderConfigsDisAllowUndef): string\nfunction useProvider(ctor: StringConstructor, name?: string): string\nfunction useProvider(ctor: BooleanConstructor, configs: UseProviderConfigsAllowUndef): boolean | undefined\nfunction useProvider(ctor: BooleanConstructor, configs: UseProviderConfigsDisAllowUndef): boolean\nfunction useProvider(ctor: BooleanConstructor, name?: string): boolean\nfunction useProvider(ctor: NumberConstructor, configs: UseProviderConfigsAllowUndef): number | undefined\nfunction useProvider(ctor: NumberConstructor, configs: UseProviderConfigsDisAllowUndef): number\nfunction useProvider(ctor: NumberConstructor, name?: string): number\nfunction useProvider<T>(ctor: Constructor<T>, configs: UseProviderConfigsAllowUndef): T | undefined\nfunction useProvider<T>(ctor: Constructor<T>, configs: UseProviderConfigsDisAllowUndef): T\nfunction useProvider<T>(ctor: Constructor<T>, name?: string): T\nfunction useProvider<T>(ctor: Constructor<T>, configs?: string | UseProviderConfigs): T | undefined\nfunction useProvider<T>(ctor: Constructor<T>, configs?: string | UseProviderConfigs): T | undefined {\n  const name = typeof configs === 'string' ? configs : configs?.name\n  const allowUndef = typeof configs === 'string' ? false : configs?.allowUndef ?? false\n  const key = name ?? getObjectRuntimeName(ctor)\n  return useNamedProvider(key, { allowUndef })\n}\n\nexport default useProvider\nexport type { UseProviderConfigsAllowUndef, UseProviderConfigsDisAllowUndef, UseProviderConfigs }\n"],"names":["getObjectRuntimeName","o","String","name","Boolean","Number","constructor","isCreate","source","ProviderContext","createContext","undefined","ResourcesNotProvidedError","_Error","_this","call","this","Object","setPrototypeOf","_assertThisInitialized","prototype","_wrapNativeSuper","Error","useNamedProvider","configs","_parentData$has","key","parentDataContext","useContext","parentData","data","has","allowUndef","get","ProviderScope","_ref","children","value","Map","set","_jsx","jsx","Provider","props","ctor","renderNested","nestedElements","nestedChildren","currentElement","remainingElement","slice","React","cloneElement","providers","_createdData$current","cleanUp","createdData","useRef","valueData","useEffect","currentData","current","_configs$allowUndef"],"mappings":"+JAEgBA,EAAqBC,GACnC,MAAiB,iBAANA,EACFC,OAAOC,KAKC,kBAANF,EACFG,QAAQD,KAKA,iBAANF,EACFI,OAAOF,KAKC,iBAANF,EACFA,EAAEK,YAAYH,KAEhBF,EAAEE,IACX,UAEgBI,EAAYC,GAC1B,MAAyB,mBAAXA,CAChB,CCvBM,IAAAC,eAAkBC,EAAaA,mBAA0BC,yuCCNzD,IAAAC,wBAA0BC,WAC9B,SAAAD,EAAYT,GAAYW,IAAAA,EAE0C,OADhEA,EAAAD,EAAAE,KAAAC,KAAA,cAAoBb,EAAI,yCACxBc,KAAAA,OAAOC,wIAAcC,CAAAL,GAAOF,EAA0BQ,WAAUN,CAClE,CAAC,SAJ6BD,KAAAD,yEAI7BA,CAAA,eAAAS,EAJqCC,QCmBxC,SAASC,EAAoBpB,EAAcqB,GAAgCC,IAAA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