"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.confirm = confirm;
exports.getConfirmQueue = getConfirmQueue;
exports.resolveConfirm = resolveConfirm;
exports.subscribeConfirms = subscribeConfirms;
/**
 * Imperative confirm store.
 *
 * Mirrors the `dialog/alert-store` pattern: a module-scope queue + a single
 * subscriber (the `<AlertDialogHost />` mounted in the app tree). `confirm()`
 * enqueues an entry and resolves a promise with the user's choice. Entries
 * enqueued before the host mounts drain on subscribe — no lost confirms.
 */

let idCounter = 0;
function genId() {
  idCounter += 1;
  return `bloom-confirm-${idCounter}`;
}
let queue = [];
let listener = null;
function emit() {
  if (listener) listener(queue);
}

/**
 * Show a Bloom confirm dialog and resolve with the user's choice.
 *
 * ```ts
 * const ok = await confirm({
 *   title: 'Delete app?',
 *   description: 'This cannot be undone.',
 *   confirmLabel: 'Delete',
 *   destructive: true,
 * });
 * if (ok) await doDelete();
 * ```
 */
function confirm(options) {
  return new Promise(resolve => {
    const id = genId();
    queue = [...queue, {
      ...options,
      id,
      resolve
    }];
    emit();
  });
}

/** Resolve and remove an entry. Called by the host on confirm/cancel/dismiss. */
function resolveConfirm(id, confirmed) {
  const entry = queue.find(e => e.id === id);
  if (!entry) return;
  queue = queue.filter(e => e.id !== id);
  entry.resolve(confirmed);
  emit();
}

/**
 * Subscribe to queue changes (single-subscriber model; latest wins). Delivers
 * the current queue synchronously so a freshly mounted host catches up.
 */
function subscribeConfirms(fn) {
  listener = fn;
  fn(queue);
  return () => {
    if (listener === fn) listener = null;
  };
}

/** Inspect the current queue. Used by tests. */
function getConfirmQueue() {
  return queue;
}
//# sourceMappingURL=confirm-store.js.map