{"version":3,"file":"ensureShadowMount-BMj3k3q0.mjs","names":[],"sources":["../src/shadow/react/getOrCreateDiv.ts","../src/shadow/react/getOrCreateEmotionInsertionPoint.ts","../src/shadow/react/ensureShadowMount.ts"],"sourcesContent":["/**\n * Returns the shadow-root child `<div>` with the given id, creating it if absent.\n * @param {ShadowRoot} root - The shadow root to search/append within.\n * @param {string} id - The element id.\n * @returns {HTMLDivElement} The existing or newly created div.\n */\nexport const getOrCreateDiv = (\n  root: ShadowRoot,\n  id: string,\n): HTMLDivElement => {\n  const existing = root.getElementById(id)\n  if (existing instanceof HTMLDivElement) return existing\n\n  const el = document.createElement('div')\n  el.id = id\n  root.appendChild(el)\n  return el\n}\n","/**\n * Returns the shadow-root `<meta name=\"emotion-insertion-point\">` with the given\n * id, creating it if absent. Keeps Emotion's styles ordered before shadow content.\n * @param {ShadowRoot} root - The shadow root to search/append within.\n * @param {string} id - The element id.\n * @returns {HTMLMetaElement} The existing or newly created insertion-point meta.\n */\nexport const getOrCreateEmotionInsertionPoint = (\n  root: ShadowRoot,\n  id: string,\n): HTMLMetaElement => {\n  const existing = root.getElementById(id)\n  if (existing instanceof HTMLMetaElement) return existing\n\n  const el = document.createElement('meta')\n  el.id = id\n  el.setAttribute('name', 'emotion-insertion-point')\n  root.appendChild(el)\n  return el\n}\n","import createCache from '@emotion/cache'\n\nimport type { EnsureShadowMountOptions } from '../../types/shadow/EnsureShadowMountOptions'\nimport type { ShadowMount } from '../../types/shadow/ShadowMount'\nimport { injectShadowStyles } from '../injectShadowStyles'\nimport { getOrCreateDiv } from './getOrCreateDiv'\nimport { getOrCreateEmotionInsertionPoint } from './getOrCreateEmotionInsertionPoint'\n\nconst STYLE_ELEMENT_ID = 'sbu-shadow-styles'\nconst REACT_MOUNT_ID = 'sbu-react-root'\nconst PORTAL_CONTAINER_ID = 'sbu-portal-root'\nconst EMOTION_INSERTION_POINT_ID = 'sbu-emotion-insertion-point'\n\n/**\n * Ensures a ShadowRoot mount exists on the host element for a widget.\n *\n * Creates stable React-mount and portal-container nodes, injects the widget's CSS\n * into the shadow root only (never into the document), and configures an Emotion\n * cache scoped to the shadow root. All internal ids are namespaced by `cacheKey`\n * so editor and runtime mounts on the same host never collide.\n * @param {HTMLElement} host - The host element to attach the shadow root to.\n * @param {EnsureShadowMountOptions} options - The cache key and the widget's CSS text.\n * @returns {ShadowMount} The shadow root, mount nodes and the scoped Emotion cache.\n */\nexport const ensureShadowMount = (\n  host: HTMLElement,\n  { cacheKey, cssText }: EnsureShadowMountOptions,\n): ShadowMount => {\n  const shadowRoot = host.shadowRoot ?? host.attachShadow({ mode: 'open' })\n\n  const reactMountEl = getOrCreateDiv(\n    shadowRoot,\n    `${REACT_MOUNT_ID}-${cacheKey}`,\n  )\n  const portalContainerEl = getOrCreateDiv(\n    shadowRoot,\n    `${PORTAL_CONTAINER_ID}-${cacheKey}`,\n  )\n  const insertionPoint = getOrCreateEmotionInsertionPoint(\n    shadowRoot,\n    `${EMOTION_INSERTION_POINT_ID}-${cacheKey}`,\n  )\n\n  injectShadowStyles(shadowRoot, cssText, `${STYLE_ELEMENT_ID}-${cacheKey}`)\n\n  // Emotion's `container` is typed as HTMLElement but supports ShadowRoot targets\n  // at runtime; the localized cast keeps the rest of the call type-safe.\n  const emotionCache = createCache({\n    key: cacheKey,\n    container: shadowRoot as unknown as HTMLElement,\n    insertionPoint,\n  })\n\n  return { shadowRoot, reactMountEl, portalContainerEl, emotionCache }\n}\n"],"mappings":";;;AAMA,IAAa,KACX,GACA,MACmB;CACnB,IAAM,IAAW,EAAK,eAAe,CAAE;CACvC,IAAI,aAAoB,gBAAgB,OAAO;CAE/C,IAAM,IAAK,SAAS,cAAc,KAAK;CAGvC,OAFA,EAAG,KAAK,GACR,EAAK,YAAY,CAAE,GACZ;AACT,GCVa,KACX,GACA,MACoB;CACpB,IAAM,IAAW,EAAK,eAAe,CAAE;CACvC,IAAI,aAAoB,iBAAiB,OAAO;CAEhD,IAAM,IAAK,SAAS,cAAc,MAAM;CAIxC,OAHA,EAAG,KAAK,GACR,EAAG,aAAa,QAAQ,yBAAyB,GACjD,EAAK,YAAY,CAAE,GACZ;AACT,GCXM,IAAmB,qBACnB,IAAiB,kBACjB,IAAsB,mBACtB,IAA6B,+BAatB,KACX,GACA,EAAE,aAAU,iBACI;CAChB,IAAM,IAAa,EAAK,cAAc,EAAK,aAAa,EAAE,MAAM,OAAO,CAAC,GAElE,IAAe,EACnB,GACA,GAAG,EAAe,GAAG,GACvB,GACM,IAAoB,EACxB,GACA,GAAG,EAAoB,GAAG,GAC5B,GACM,IAAiB,EACrB,GACA,GAAG,EAA2B,GAAG,GACnC;CAYA,OAVA,EAAmB,GAAY,GAAS,GAAG,EAAiB,GAAG,GAAU,GAUlE;EAAE;EAAY;EAAc;EAAmB,cANjC,EAAY;GAC/B,KAAK;GACL,WAAW;GACX;EACF,CAEsD;CAAa;AACrE"}