{"version":3,"file":"UdpAppProvider.mjs","names":["React","ReactNode","useEffect","useState","SnackbarProvider","SnackbarProviderProps","ThemeProvider","AuthProvider","initializeUnityI18n","InitializeI18nOptions","storeInitialPath","storeInitialTenant","BrowserRouter","QueryProvider","UdpAiAppSurface","UdpDevToolsOptions","query","UdpAppProviderProps","children","snackbarProps","authProps","doNotWaitForTenant","i18nOptions","devTools","ai","SNACKBAR_HOST_STYLE","createSnackbarHost","HTMLElement","document","undefined","prototype","showPopover","el","createElement","setAttribute","style","cssText","useSnackbarTopLayerHost","$","_c","host","t0","t1","body","appendChild","scheduled","promote","queueMicrotask","isConnected","matches","hidePopover","removeAttribute","observer","MutationObserver","records","some","_temp","observe","childList","subtree","disconnect","remove","UdpAppProvider","FC","t2","snackbarHost","t3","content","t4","t5","t6","t7","record","addedNodes","length"],"sources":["../../src/providers/UdpAppProvider.tsx"],"sourcesContent":["import React, { ReactNode, useEffect, useState } from 'react';\nimport { SnackbarProvider, SnackbarProviderProps } from 'notistack';\nimport { ThemeProvider } from '../utilities/theme';\nimport { AuthProvider } from '../utilities/auth/AuthProvider';\nimport { initializeUnityI18n } from '../utilities/i18n/initializeI18n';\nimport type { InitializeI18nOptions } from '../utilities/i18n/initializeI18n';\nimport { storeInitialPath } from '../utilities/redirect';\nimport { storeInitialTenant } from '../utilities/storage';\nimport { BrowserRouter } from 'react-router-dom';\nimport { QueryProvider } from './QueryProvider';\nimport { UdpAiAppSurface } from './UdpAiAppSurface';\n\nexport interface UdpDevToolsOptions {\n  /** Show the React Query DevTools panel. Defaults to false. */\n  query?: boolean;\n}\n\nexport interface UdpAppProviderProps {\n  children: ReactNode;\n  /**\n   * Props forwarded to the SnackbarProvider from notistack.\n   */\n  snackbarProps?: SnackbarProviderProps;\n  /**\n   * Props forwarded to the AuthProviderWrapper.\n   */\n  authProps?: {\n    doNotWaitForTenant?: boolean;\n  };\n  /**\n   * Optional configuration overrides for the shared i18n instance.\n   */\n  i18nOptions?: InitializeI18nOptions;\n  /**\n   * Enable developer tools for debugging. All options default to false.\n   */\n  devTools?: UdpDevToolsOptions;\n  /**\n   * Opt in to app-level AI: mounts the AI access provider. A page then builds\n   * an `AiCapability` (prompt, live context, tools, proposal, the models it\n   * runs on) and mounts `UdpAiAssistant` for it where the chat belongs.\n   * Nothing about models is decided here.\n   *\n   * No assistant UI is mounted for you; nothing appears until a page puts a\n   * surface on screen.\n   *\n   * Connection details are not props: the gateway URL comes from\n   * `ConfigService.UDP_AI_GATEWAY_BASE` and the product header from\n   * `UNITY_PRODUCT_ID`. With no gateway URL configured this is a no-op.\n   * Entitlement is not a prop either: the gateway is asked once per tenant\n   * (`GET /v1/models`) and AI surfaces render only when it allows this\n   * tenant/product — the app declares that it ships AI, the server decides\n   * who gets it.\n   *\n   * Omitted (the default) means no AI code runs at all.\n   */\n  ai?: boolean;\n}\n\n/**\n * Strip the popover UA box entirely: notistack's own container is\n * position: fixed and does all the positioning, so the host is a zero-size\n * anchor that must not paint, size, or clip anything.\n */\nconst SNACKBAR_HOST_STYLE =\n  'position:fixed;inset:auto;width:0;height:0;margin:0;padding:0;border:0;background:none;overflow:visible';\n\nconst createSnackbarHost = (): HTMLElement | undefined => {\n  if (typeof document === 'undefined') return undefined;\n  if (typeof HTMLElement.prototype.showPopover !== 'function') return undefined;\n\n  const el = document.createElement('div');\n  el.setAttribute('popover', 'manual');\n  el.style.cssText = SNACKBAR_HOST_STYLE;\n  return el;\n};\n\n/**\n * Creates the DOM element notistack portals its snackbar container into, and\n * keeps it in the browser's **top layer**.\n *\n * Why this exists: `UdpDrawer` and `UdpDialog` open a native `<dialog>` with\n * `showModal()`, which promotes them to the top layer. Top-layer elements paint\n * above every element in ordinary DOM regardless of `z-index`, so notistack's\n * container — a plain `position: fixed` element at `zIndex.snackbar` — is hidden\n * behind an open drawer or dialog. No z-index can fix that; the container has to\n * be in the top layer too, which the Popover API provides.\n *\n * Paint order inside the top layer is promotion order, so the host is\n * re-promoted (`hidePopover()` then `showPopover()`, synchronously, so nothing\n * paints in between) whenever a snackbar is added. That keeps it above whichever\n * drawer or dialog opened most recently.\n *\n * Two consequences worth knowing:\n * - While a modal drawer or dialog is open, everything outside it is inert, so a\n *   snackbar `action` button is visible but not clickable. Auto-dismiss is\n *   unaffected. Anything the user must act on belongs in a `UdpMessageBar`\n *   inside the drawer, not a toast.\n * - If a drawer opens while a snackbar is already showing, the drawer is\n *   promoted last and covers it until the next snackbar arrives.\n *\n * Returns `undefined` when the browser has no Popover API, which leaves\n * notistack rendering inline exactly as it did before.\n */\nconst useSnackbarTopLayerHost = (): HTMLElement | undefined => {\n  const [host] = useState(createSnackbarHost);\n\n  useEffect(() => {\n    if (!host) return undefined;\n\n    document.body.appendChild(host);\n\n    let scheduled = false;\n    const promote = () => {\n      if (scheduled) return;\n      scheduled = true;\n      // Coalesce to one promotion per React commit.\n      queueMicrotask(() => {\n        scheduled = false;\n        if (!host.isConnected) return;\n        try {\n          if (host.matches(':popover-open')) host.hidePopover();\n          host.showPopover();\n        } catch {\n          // Popover refused: fall back to a plain element so snackbars still\n          // render, just without top-layer promotion.\n          host.removeAttribute('popover');\n        }\n      });\n    };\n\n    // Only additions matter — a snackbar leaving cannot change what sits above it.\n    const observer = new MutationObserver((records) => {\n      if (records.some((record) => record.addedNodes.length > 0)) promote();\n    });\n    observer.observe(host, { childList: true, subtree: true });\n\n    promote();\n\n    return () => {\n      observer.disconnect();\n      host.remove();\n    };\n  }, [host]);\n\n  return host;\n};\n\n/**\n * Bundles the core UDP providers (theme, snackbar, auth) and ensures the shared\n * i18n instance is initialised exactly once. Intended to be rendered beneath a\n * react-router <Router /> so the auth provider has access to history.\n */\nexport const UdpAppProvider: React.FC<UdpAppProviderProps> = ({\n  children,\n  snackbarProps,\n  authProps,\n  i18nOptions,\n  devTools,\n  ai,\n}) => {\n  useEffect(() => {\n    initializeUnityI18n(i18nOptions);\n  }, [i18nOptions]);\n  // Snackbars have to live in the browser top layer or the modal <dialog>\n  // behind UdpDrawer / UdpDialog paints over them. See useSnackbarTopLayerHost.\n  const snackbarHost = useSnackbarTopLayerHost();\n  storeInitialPath();\n  storeInitialTenant();\n  // Inside the auth/query providers: the assistant's transport authenticates\n  // through the same apiMutate the rest of the library uses.\n  const content = ai ? <UdpAiAppSurface>{children}</UdpAiAppSurface> : children;\n  return (\n    <ThemeProvider>\n      <QueryProvider showDevTools={devTools?.query}>\n        <AuthProvider {...authProps}>\n          <BrowserRouter>\n            <SnackbarProvider maxSnack={3} domRoot={snackbarHost} {...snackbarProps}>\n              {content}\n            </SnackbarProvider>\n          </BrowserRouter>\n        </AuthProvider>\n      </QueryProvider>\n    </ThemeProvider>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAgEA,MAAMyB,sBACJ;AAEF,MAAMC,2BAAoD;AACxD,KAAI,OAAOE,aAAa,YAAa,QAAOC,KAAAA;AAC5C,KAAI,OAAOF,YAAYG,UAAUC,gBAAgB,WAAY,QAAOF,KAAAA;CAEpE,MAAMG,KAAKJ,SAASK,cAAc,MAAM;AACxCD,IAAGE,aAAa,WAAW,SAAS;AACpCF,IAAGG,MAAMC,UAAUX;AACnB,QAAOO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BT,MAAMK,gCAA0B;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAC9B,MAAA,CAAAC,QAAerC,SAASuB,mBAAmB;CAAC,IAAAe;CAAA,IAAAC;AAAA,KAAAJ,EAAA,OAAAE,MAAA;AAElCC,aAAA;AACR,OAAI,CAACD,KAAI;AAETZ,YAAQe,KAAKC,YAAaJ,KAAK;GAE/B,IAAAK,YAAgB;GAChB,MAAAC,gBAAgB;AACd,QAAID,UAAS;AACbA,gBAAYA;AAEZE,yBAAe;AACbF,iBAAYA;AACZ,SAAI,CAACL,KAAIQ,YAAY;AACrB,SAAA;AACE,UAAIR,KAAIS,QAAS,gBAAgB,CAAET,MAAIU,aAAc;AACrDV,WAAIT,aAAc;aAAA;AAIlBS,WAAIW,gBAAiB,UAAU;;MAEj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