{"version":3,"file":"GlobalContext.cjs","names":["useRef","GLOBAL_CONTEXT"],"sources":["../../src/GlobalContext/GlobalContext.tsx"],"sourcesContent":["import { useContext } from 'react';\nimport { reactive, setAsGlobalContext } from '@pastweb/tools';\nimport { useRef } from '../useRef';\nimport { useBeforeMount } from '../useBeforeMount';\nimport { GLOBAL_CONTEXT } from './constants';\nimport type { ContextProviderProps, GlobalContext } from './types';\n\n/**\n * Provides a reactive Pastweb global context to a React subtree.\n *\n * `GlobalContext` starts from the nearest parent context, applies `update`\n * values, then runs optional installer functions from `use`. Descendants can\n * subscribe to individual values with {@link getContext}.\n *\n * Installer-returned keys are guarded against accidental collisions with values\n * already present in the local scope.\n *\n * @param props - Provider options.\n * @param props.children - React subtree that receives the context.\n * @param props.use - Installer or installers used to add shared values.\n * @param props.update - Values merged into the local context for this provider.\n *\n * @example\n * ```tsx\n * const installUser = () => ({ user: { name: 'Ada' } });\n *\n * <GlobalContext use={installUser}>\n *   <Profile />\n * </GlobalContext>\n * ```\n */\nexport function GlobalContext(props: ContextProviderProps) {\n  const { update = {}, use, children } = props;\n  const parent = useContext(GLOBAL_CONTEXT);\n  const local = useRef<Record<string, any>>(reactive({ ...parent, ...update }));\n\n  function updateLocal(obj: Record<string | symbol, any>, name?: string) {\n    const entries = Object.entries(obj);\n\n    if (!entries.length) return;\n\n    for (const [key, value] of entries) {\n      if (local.value[key] && name) {\n        throw Error(`GlobalContext ERROR: the install function \"${name}\" returns an object with the key \"${key}\" which it is already present into the global context.`);\n      }\n\n      local.value[key] = value;\n    }\n  }\n\n  useBeforeMount(() => {\n    setAsGlobalContext(local.value);\n\n    if (!use) return;\n\n    const installers = Array.isArray(use) ? use : [use];\n\n    installers.forEach(fn => {\n      const keys = Object.keys(local.value);\n      const obj = fn(keys);\n\n      updateLocal(obj, fn.name);\n    });\n  });\n\n  updateLocal(update);\n\n  return (\n    <GLOBAL_CONTEXT.Provider value={local.value}>\n      {children}\n    </GLOBAL_CONTEXT.Provider>\n  )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,cAAc,OAA6B;CACzD,MAAM,EAAE,SAAS,CAAC,GAAG,KAAK,aAAa;CAEvC,MAAM,QAAQA,sBAAAA,QAAAA,GAAAA,eAAAA,SAAAA,CAAqC;EAAE,IAAA,GAAA,MAAA,WAAA,CAD3BC,gCAAAA,cACmC;EAAG,GAAG;CAAO,CAAC,CAAC;CAE5E,SAAS,YAAY,KAAmC,MAAe;EACrE,MAAM,UAAU,OAAO,QAAQ,GAAG;EAElC,IAAI,CAAC,QAAQ,QAAQ;EAErB,KAAK,MAAM,CAAC,KAAK,UAAU,SAAS;GAClC,IAAI,MAAM,MAAM,QAAQ,MACtB,MAAM,MAAM,8CAA8C,KAAK,oCAAoC,IAAI,uDAAuD;GAGhK,MAAM,MAAM,OAAO;EACrB;CACF;CAEA,sCAAA,qBAAqB;EACnB,CAAA,GAAA,eAAA,mBAAA,CAAmB,MAAM,KAAK;EAE9B,IAAI,CAAC,KAAK;EAIV,CAFmB,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,GAAG,EAAA,CAEvC,SAAQ,OAAM;GAIvB,YAFY,GADC,OAAO,KAAK,MAAM,KACb,CAEJ,GAAG,GAAG,IAAI;EAC1B,CAAC;CACH,CAAC;CAED,YAAY,MAAM;CAElB,OACE,iBAAA,GAAA,kBAAA,IAAA,CAACA,gCAAAA,eAAe,UAAhB;EAAyB,OAAO,MAAM;EACnC;CACsB,CAAA;AAE7B"}