{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n  createContext as createReactContext,\n  useContext as useReactContext,\n} from \"react\"\n\nexport interface CreateContextOptions<T> {\n  strict?: boolean\n  hookName?: string\n  providerName?: string\n  errorMessage?: string\n  name?: string\n  defaultValue?: T\n}\n\nexport type CreateContextReturn<T> = [\n  React.Provider<T>,\n  () => T,\n  React.Context<T>,\n]\n\nfunction getErrorMessage(hook: string, provider: string) {\n  return `${hook} returned \\`undefined\\`. Seems you forgot to wrap component within ${provider}`\n}\n\nexport function createContext<T>(options: CreateContextOptions<T> = {}) {\n  const {\n    name,\n    strict = true,\n    hookName = \"useContext\",\n    providerName = \"Provider\",\n    errorMessage,\n    defaultValue,\n  } = options\n\n  const Context = createReactContext<T | undefined>(defaultValue)\n\n  Context.displayName = name\n\n  function useContext() {\n    const context = useReactContext(Context)\n\n    if (!context && strict) {\n      const error = new Error(\n        errorMessage ?? getErrorMessage(hookName, providerName),\n      )\n      error.name = \"ContextError\"\n      Error.captureStackTrace?.(error, useContext)\n      throw error\n    }\n\n    return context\n  }\n\n  return [Context.Provider, useContext, Context] as CreateContextReturn<T>\n}\n"],"mappings":";;;AAAA;AAAA,EACE,iBAAiB;AAAA,EACjB,cAAc;AAAA,OACT;AAiBP,SAAS,gBAAgB,MAAc,UAAkB;AACvD,SAAO,GAAG,0EAA0E;AACtF;AAEO,SAAS,cAAiB,UAAmC,CAAC,GAAG;AACtE,QAAM;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,IACT,WAAW;AAAA,IACX,eAAe;AAAA,IACf;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,UAAU,mBAAkC,YAAY;AAE9D,UAAQ,cAAc;AAEtB,WAAS,aAAa;AAtCxB;AAuCI,UAAM,UAAU,gBAAgB,OAAO;AAEvC,QAAI,CAAC,WAAW,QAAQ;AACtB,YAAM,QAAQ,IAAI;AAAA,QAChB,sCAAgB,gBAAgB,UAAU,YAAY;AAAA,MACxD;AACA,YAAM,OAAO;AACb,kBAAM,sBAAN,+BAA0B,OAAO;AACjC,YAAM;AAAA,IACR;AAEA,WAAO;AAAA,EACT;AAEA,SAAO,CAAC,QAAQ,UAAU,YAAY,OAAO;AAC/C;","names":[]}