{"version":3,"file":"create-context.js","names":[],"sources":["esm/create-context.js"],"sourcesContent":["import * as React from \"react\";\nimport { jsx } from \"react/jsx-runtime\";\nfunction createContext(rootComponentName, defaultContext) {\n  const Context = React.createContext(defaultContext);\n  function Provider(props) {\n    const { children, ...context } = props;\n    const value = React.useMemo(() => context, Object.values(context));\n    return /* @__PURE__ */ jsx(Context.Provider, { value, children });\n  }\n  function useContext(consumerName) {\n    const context = React.useContext(Context);\n    if (context) return context;\n    if (defaultContext !== void 0) return defaultContext;\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``);\n  }\n  return [Provider, useContext];\n}\nfunction createContextScope(scopeName, createContextScopeDeps = []) {\n  let defaultContexts = [];\n  function createContext2(rootComponentName, defaultContext) {\n    const BaseContext = React.createContext(defaultContext);\n    const index = defaultContexts.length;\n    defaultContexts = [...defaultContexts, defaultContext];\n    function Provider(props) {\n      const { scope, children, ...context } = props;\n      const Context = scope?.[scopeName]?.[index] || BaseContext;\n      const value = React.useMemo(\n        () => context,\n        Object.values(context)\n      );\n      return /* @__PURE__ */ jsx(Context.Provider, { value, children });\n    }\n    function useContext(consumerName, scope, options) {\n      const Context = scope?.[scopeName]?.[index] || BaseContext;\n      const context = React.useContext(Context);\n      if (context) return context;\n      if (defaultContext !== void 0) return defaultContext;\n      const missingContextMessage = `\\`${consumerName}\\` must be used within \\`${rootComponentName}\\``;\n      if (options?.fallback) {\n        if (options?.warn !== false) {\n          console.warn(missingContextMessage);\n        }\n        return options.fallback;\n      }\n      throw new Error(missingContextMessage);\n    }\n    return [Provider, useContext];\n  }\n  const createScope = () => {\n    const scopeContexts = defaultContexts.map((defaultContext) => {\n      return React.createContext(defaultContext);\n    });\n    return function useScope(scope) {\n      const contexts = scope?.[scopeName] || scopeContexts;\n      return React.useMemo(\n        () => ({ [`__scope${scopeName}`]: { ...scope, [scopeName]: contexts } }),\n        [scope, contexts]\n      );\n    };\n  };\n  createScope.scopeName = scopeName;\n  return [\n    createContext2,\n    composeContextScopes(createScope, ...createContextScopeDeps)\n  ];\n}\nfunction composeContextScopes(...scopes) {\n  const baseScope = scopes[0];\n  if (scopes.length === 1) return baseScope;\n  const createScope = () => {\n    const scopeHooks = scopes.map((createScope2) => ({\n      useScope: createScope2(),\n      scopeName: createScope2.scopeName\n    }));\n    return function useComposedScopes(overrideScopes) {\n      const nextScopes = scopeHooks.reduce((nextScopes2, { useScope, scopeName }) => {\n        const scopeProps = useScope(overrideScopes);\n        const currentScope = scopeProps[`__scope${scopeName}`];\n        return { ...nextScopes2, ...currentScope };\n      }, {});\n      return React.useMemo(\n        () => ({ [`__scope${baseScope.scopeName}`]: nextScopes }),\n        [nextScopes]\n      );\n    };\n  };\n  createScope.scopeName = baseScope.scopeName;\n  return createScope;\n}\nexport {\n  createContext,\n  createContextScope\n};\n//# sourceMappingURL=create-context.js.map\n"],"mappings":";;;;AAEA,SAAS,cAAc,mBAAmB,gBAAgB;CACxD,MAAM,UAAU,MAAM,cAAc,cAAc;CAClD,SAAS,SAAS,OAAO;EACvB,MAAM,EAAE,UAAU,GAAG,YAAY;EACjC,MAAM,QAAQ,MAAM,cAAc,SAAS,OAAO,OAAO,OAAO,CAAC;EACjE,OAAuB,oBAAI,QAAQ,UAAU;GAAE;GAAO;EAAS,CAAC;CAClE;CACA,SAAS,WAAW,cAAc;EAChC,MAAM,UAAU,MAAM,WAAW,OAAO;EACxC,IAAI,SAAS,OAAO;EACpB,IAAI,mBAAmB,KAAK,GAAG,OAAO;EACtC,MAAM,IAAI,MAAM,KAAK,aAAa,2BAA2B,kBAAkB,GAAG;CACpF;CACA,OAAO,CAAC,UAAU,UAAU;AAC9B;AACA,SAAS,mBAAmB,WAAW,yBAAyB,CAAC,GAAG;CAClE,IAAI,kBAAkB,CAAC;CACvB,SAAS,eAAe,mBAAmB,gBAAgB;EACzD,MAAM,cAAc,MAAM,cAAc,cAAc;EACtD,MAAM,QAAQ,gBAAgB;EAC9B,kBAAkB,CAAC,GAAG,iBAAiB,cA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