{"version":3,"file":"internal-Cad15PZF.cjs","names":[],"sources":["../src/defineComponent/internal.ts"],"sourcesContent":["import type { AtomStore } from \"@dathra/store\";\nimport { getCurrentStore } from \"@dathra/store/internal\";\n\nconst STORE_BINDING = Symbol(\"dathra.component.store\");\n\ntype StoreBoundHost = HTMLElement & {\n  [STORE_BINDING]?: AtomStore;\n};\n\nfunction canUseComponentDOMRuntime(): boolean {\n  return (\n    typeof globalThis.document !== \"undefined\" &&\n    typeof globalThis.HTMLElement !== \"undefined\" &&\n    typeof globalThis.customElements !== \"undefined\"\n  );\n}\n\n/**\n * Bind an atom store to a component host before hydration or connection.\n */\nfunction bindStoreToHost(host: HTMLElement, store: AtomStore): void {\n  const boundHost = host as StoreBoundHost;\n  if (boundHost[STORE_BINDING] === undefined) {\n    boundHost[STORE_BINDING] = store;\n  }\n}\n\nfunction captureCurrentStore(host: HTMLElement): void {\n  const currentStore = getCurrentStore();\n  if (currentStore !== undefined) {\n    bindStoreToHost(host, currentStore);\n  }\n}\n\nfunction getStoreFromHost(host: HTMLElement): AtomStore {\n  const store = (host as StoreBoundHost)[STORE_BINDING];\n  if (store === undefined) {\n    throw new Error(\"[dathra] No store bound to component host\");\n  }\n  return store;\n}\n\nfunction peekStoreFromHost(host: HTMLElement): AtomStore | undefined {\n  return (host as StoreBoundHost)[STORE_BINDING];\n}\n\nfunction bindCurrentStoreToSubtree(root: Node): void {\n  const currentStore = getCurrentStore();\n  if (currentStore === undefined) return;\n\n  if (root instanceof HTMLElement) {\n    if (root.tagName.includes(\"-\")) {\n      bindStoreToHost(root, currentStore);\n    }\n\n    const nestedHosts = root.querySelectorAll<HTMLElement>(\"*\");\n    for (const element of nestedHosts) {\n      if (element.tagName.includes(\"-\")) {\n        bindStoreToHost(element, currentStore);\n      }\n    }\n  }\n\n  if (root instanceof DocumentFragment) {\n    const nestedHosts = root.querySelectorAll<HTMLElement>(\"*\");\n    for (const element of nestedHosts) {\n      if (element.tagName.includes(\"-\")) {\n        bindStoreToHost(element, currentStore);\n      }\n    }\n  }\n}\n\nexport {\n  bindCurrentStoreToSubtree,\n  bindStoreToHost,\n  canUseComponentDOMRuntime,\n  captureCurrentStore,\n  getStoreFromHost,\n  peekStoreFromHost,\n};\n"],"mappings":"wCAGA,MAAM,EAAgB,OAAO,yBAAyB,CAMtD,SAAS,GAAqC,CAC5C,OACS,WAAW,WAAa,QACxB,WAAW,cAAgB,QAC3B,WAAW,iBAAmB,OAOzC,SAAS,EAAgB,EAAmB,EAAwB,CAClE,IAAM,EAAY,EACd,EAAU,KAAmB,IAAA,KAC/B,EAAU,GAAiB,GAI/B,SAAS,EAAoB,EAAyB,CACpD,IAAM,GAAA,EAAA,EAAA,kBAAgC,CAClC,IAAiB,IAAA,IACnB,EAAgB,EAAM,EAAa,CAIvC,SAAS,EAAiB,EAA8B,CACtD,IAAM,EAAS,EAAwB,GACvC,GAAI,IAAU,IAAA,GACZ,MAAU,MAAM,4CAA4C,CAE9D,OAAO,EAGT,SAAS,EAAkB,EAA0C,CACnE,OAAQ,EAAwB,GAGlC,SAAS,EAA0B,EAAkB,CACnD,IAAM,GAAA,EAAA,EAAA,kBAAgC,CAClC,OAAiB,IAAA,GAErB,IAAI,aAAgB,YAAa,CAC3B,EAAK,QAAQ,SAAS,IAAI,EAC5B,EAAgB,EAAM,EAAa,CAGrC,IAAM,EAAc,EAAK,iBAA8B,IAAI,CAC3D,IAAK,IAAM,KAAW,EAChB,EAAQ,QAAQ,SAAS,IAAI,EAC/B,EAAgB,EAAS,EAAa,CAK5C,GAAI,aAAgB,iBAAkB,CACpC,IAAM,EAAc,EAAK,iBAA8B,IAAI,CAC3D,IAAK,IAAM,KAAW,EAChB,EAAQ,QAAQ,SAAS,IAAI,EAC/B,EAAgB,EAAS,EAAa"}