"use strict";

import React, { useEffect, useState } from 'react';
import { AutoMountedDialog } from './Dialog';
import { buttonToAction, dismissAlert, subscribeAlerts } from "./alert-store.js";

/**
 * 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.
 */
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
export function BloomDialogProvider({
  children
}) {
  const [queue, setQueue] = useState([]);
  useEffect(() => 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__*/_jsxs(_Fragment, {
    children: [children, head ? /*#__PURE__*/_jsx(AlertHost, {
      entry: head
    }, head.id) : null]
  });
}
function AlertHost({
  entry
}) {
  const actions = entry.buttons.map(buttonToAction);
  return /*#__PURE__*/_jsx(AutoMountedDialog, {
    title: entry.title,
    description: entry.message,
    actions: actions,
    onResolve: () => dismissAlert(entry.id)
  });
}
//# sourceMappingURL=BloomDialogProvider.js.map