"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BloomDialogProvider = BloomDialogProvider;
var _react = _interopRequireWildcard(require("react"));
var _Dialog = require("./Dialog");
var _alertStore = require("./alert-store.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * Mounts the imperative `alert()` host inside your app.
 *
 * Wrap your app root once (after `BloomThemeProvider`, so the dialog's
 * colours come from the active theme) and you can call `alert()` from
 * anywhere — including from code that runs before the provider mounts
 * (alerts are queued and drained on subscribe).
 *
 * ```tsx
 * import { BloomDialogProvider } from '@oxyhq/bloom';
 *
 * <BloomThemeProvider mode="system" colorPreset="oxy">
 *   <BloomDialogProvider>
 *     <App />
 *   </BloomDialogProvider>
 * </BloomThemeProvider>
 * ```
 *
 * Alerts are presented one at a time. While one is on screen, subsequent
 * `alert()` calls enqueue and present in FIFO order once their predecessor
 * has finished closing.
 */
function BloomDialogProvider({
  children
}) {
  const [queue, setQueue] = (0, _react.useState)([]);
  (0, _react.useEffect)(() => (0, _alertStore.subscribeAlerts)(setQueue), []);

  // Only the head of the queue is presented; this keeps the bottom-sheet
  // exit/enter animations clean and matches the platform `Alert.alert`
  // behaviour our consumers expect.
  const head = queue[0];
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [children, head ? /*#__PURE__*/(0, _jsxRuntime.jsx)(AlertHost, {
      entry: head
    }, head.id) : null]
  });
}
function AlertHost({
  entry
}) {
  const actions = entry.buttons.map(_alertStore.buttonToAction);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dialog.AutoMountedDialog, {
    title: entry.title,
    description: entry.message,
    actions: actions,
    onResolve: () => (0, _alertStore.dismissAlert)(entry.id)
  });
}
//# sourceMappingURL=BloomDialogProvider.js.map