/**
 * Preview render status — the bridge between the Live Preview (which owns
 * rendering) and the two panes that surface its outcome.
 *
 * Both the editor and the preview must show the same render problem: spec §03E
 * puts a readable error in the **editor** pane, and spec §04 puts one in the
 * **preview** pane, for the very same failure (invalid JSON, or valid JSON that
 * fails to render as Vega-Lite — including an unresolved dataset reference). The
 * Live Preview is the single producer; it writes the current error here and both
 * panes subscribe. `null` means the current spec rendered cleanly (or is blank).
 *
 * `busy` tracks whether a render is in flight long enough to warrant a visible
 * indicator (arch §10.2: >~1s owes a non-blocking busy overlay). LivePreview arms
 * a 1 s timer when a render starts and sets `busy = true` only if the render has
 * not settled by then; it clears `busy` on settle or error regardless.
 *
 * Kept as its own tiny store rather than folded into the SnippetStore: this is
 * transient render state, not durable domain data, and it must not be persisted.
 */

import { create } from 'zustand';

export interface PreviewState {
  /** The current render error message, or null when the spec renders cleanly. */
  error: string | null;
  /** Set (or clear) the current render error. */
  setError: (error: string | null) => void;
  /**
   * True while a render has been in flight for longer than the ~1s NN/g threshold
   * (arch §10.2). The LivePreview overlay reads this to show a non-blocking busy
   * indication; aria-busy on the preview region mirrors it.
   */
  busy: boolean;
  /** Set or clear the busy flag. */
  setBusy: (busy: boolean) => void;
}

export const usePreviewStore = create<PreviewState>((set) => ({
  error: null,
  setError: (error) => set({ error }),
  busy: false,
  setBusy: (busy) => set({ busy }),
}));
