/**
 * Confirmation dialogs — the in-app replacement for `window.confirm`
 * (docs/architecture/03 → "Confirmation & alert dialogs").
 *
 * This is a deliberately separate, lighter layer from the named feature-modal
 * system (registry/coordinator/shell). Feature modals are a fixed, registered,
 * URL-navigable set with "at most one open at a time". A confirmation is the
 * opposite: ephemeral, content-on-call, and allowed to sit *above* an open
 * feature modal (the discard-changes prompt appears over the Datasets modal).
 * So it gets its own store and its own z-layer rather than a `ModalName` entry.
 *
 * The API is promise-based and callable from anywhere — React components and
 * non-React code alike (the modal coordinator's `setConfirm` seam will resolve
 * to `confirm()` here once the feature-modal system lands). `confirm(opts)`
 * returns a Promise<boolean>: `true` if the user accepted, `false` if they
 * cancelled / dismissed.
 *
 * The store holds only request state; rendering lives in ConfirmDialog.tsx.
 * Keeping the resolve/replace logic here makes it unit-testable without a DOM.
 */

import { create } from 'zustand';

export interface ConfirmOptions {
  /** Short dialog title, e.g. "Delete snippet". */
  title: string;
  /** Body text; states the consequence ("This cannot be undone."). */
  message: string;
  /** Primary (accept) button label. Defaults to "Confirm". */
  confirmLabel?: string;
  /** Secondary (cancel) button label. Defaults to "Cancel". */
  cancelLabel?: string;
  /**
   * Destructive intent → Carbon "danger" styling (red primary) and a
   * cancel-defaulted focus so an accidental Enter doesn't destroy anything.
   */
  danger?: boolean;
}

interface ConfirmRequest extends ConfirmOptions {
  /** Settles the promise returned by `confirm()` for this request. */
  resolve: (accepted: boolean) => void;
}

export interface ConfirmState {
  /** The open request, or null when no dialog is showing. */
  request: ConfirmRequest | null;
  /** Open a confirmation and resolve when the user chooses. */
  confirm: (options: ConfirmOptions) => Promise<boolean>;
  /** Settle the open request with the user's choice and close the dialog. */
  resolve: (accepted: boolean) => void;
}

export const useConfirmStore = create<ConfirmState>((set, get) => ({
  request: null,

  confirm: (options) =>
    new Promise<boolean>((resolve) => {
      // Only one confirmation at a time. If one is somehow already open,
      // treat it as cancelled so its awaiter never hangs, then replace it.
      const prev = get().request;
      if (prev) prev.resolve(false);
      set({ request: { ...options, resolve } });
    }),

  resolve: (accepted) => {
    const req = get().request;
    if (!req) return; // nothing open — no-op
    set({ request: null });
    req.resolve(accepted);
  },
}));

/**
 * Imperative entry point for non-React callers (e.g. the future modal
 * coordinator's `setConfirm`). Components may use this too, but typically
 * subscribe to `useConfirmStore` for rendering.
 */
export const confirm = (options: ConfirmOptions): Promise<boolean> =>
  useConfirmStore.getState().confirm(options);
