{"version":3,"file":"createContext.cjs","sources":["../../src/shared/createContext.ts"],"sourcesContent":["import { type InjectionKey, inject, provide } from 'vue'\n\n/**\n * @param providerComponentName - The name(s) of the component(s) providing the context.\n *\n * There are situations where context can come from multiple components. In such cases, you might need to give an array of component names to provide your context, instead of just a single string.\n *\n * @param contextName The description for injection key symbol.\n */\nexport function createContext<ContextValue>(\n  providerComponentName: string | string[],\n  contextName?: string,\n) {\n  const symbolDescription\n    = typeof providerComponentName === 'string' && !contextName\n      ? `${providerComponentName}Context`\n      : contextName\n\n  const injectionKey: InjectionKey<ContextValue | null> = Symbol(symbolDescription)\n\n  /**\n   * @param fallback The context value to return if the injection fails.\n   *\n   * @throws When context injection failed and no fallback is specified.\n   * This happens when the component injecting the context is not a child of the root component providing the context.\n   */\n  const injectContext = <\n    T extends ContextValue | null | undefined = ContextValue,\n  >(\n    fallback?: T,\n  ): T extends null ? ContextValue | null : ContextValue => {\n    const context = inject(injectionKey, fallback)\n    if (context)\n      return context\n\n    if (context === null)\n      return context as any\n\n    throw new Error(\n      `Injection \\`${injectionKey.toString()}\\` not found. Component must be used within ${\n        Array.isArray(providerComponentName)\n          ? `one of the following components: ${providerComponentName.join(\n            ', ',\n          )}`\n          : `\\`${providerComponentName}\\``\n      }`,\n    )\n  }\n\n  const provideContext = (contextValue: ContextValue) => {\n    provide(injectionKey, contextValue)\n    return contextValue\n  }\n\n  return [injectContext, provideContext] as const\n}\n"],"names":["inject","provide"],"mappings":";;;;AASgB,SAAA,aAAA,CACd,uBACA,WACA,EAAA;AACA,EAAM,MAAA,iBAAA,GACF,OAAO,qBAA0B,KAAA,QAAA,IAAY,CAAC,WAC5C,GAAA,CAAA,EAAG,qBAAqB,CACxB,OAAA,CAAA,GAAA,WAAA;AAEN,EAAM,MAAA,YAAA,GAAkD,OAAO,iBAAiB,CAAA;AAQhF,EAAM,MAAA,aAAA,GAAgB,CAGpB,QACwD,KAAA;AACxD,IAAM,MAAA,OAAA,GAAUA,UAAO,CAAA,YAAA,EAAc,QAAQ,CAAA;AAC7C,IAAI,IAAA,OAAA;AACF,MAAO,OAAA,OAAA;AAET,IAAA,IAAI,OAAY,KAAA,IAAA;AACd,MAAO,OAAA,OAAA;AAET,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,CAAA,YAAA,EAAe,YAAa,CAAA,QAAA,EAAU,CAAA,4CAAA,EACpC,MAAM,OAAQ,CAAA,qBAAqB,CAC/B,GAAA,CAAA,iCAAA,EAAoC,qBAAsB,CAAA,IAAA;AAAA,QAC1D;AAAA,OACD,CAAA,CAAA,GACC,CAAK,EAAA,EAAA,qBAAqB,CAChC,EAAA,CAAA,CAAA;AAAA,KACF;AAAA,GACF;AAEA,EAAM,MAAA,cAAA,GAAiB,CAAC,YAA+B,KAAA;AACrD,IAAAC,WAAA,CAAQ,cAAc,YAAY,CAAA;AAClC,IAAO,OAAA,YAAA;AAAA,GACT;AAEA,EAAO,OAAA,CAAC,eAAe,cAAc,CAAA;AACvC;;;;"}