/**
 * Open-state registry for disclosure popovers (spec §07; arch 10).
 *
 * A single id names whichever popover is open — settings clusters, the sort
 * control, select controls, chart export — so at most one shows at a time, and
 * any can be opened imperatively (the Cmd/Ctrl+, shortcut targets the editor
 * settings cluster). Kept in its own module so component files export only
 * components (fast-refresh friendly). The shared open/place/dismiss behavior
 * built on top lives in `hooks/usePopover`.
 */

import { create } from 'zustand';

export interface PopoverState {
  /** Id of the single open popover, or null. */
  openId: string | null;
  toggle: (id: string) => void;
  show: (id: string) => void;
  close: () => void;
}

export const usePopoverStore = create<PopoverState>((set) => ({
  openId: null,
  toggle: (id) => set((s) => ({ openId: s.openId === id ? null : id })),
  show: (id) => set({ openId: id }),
  close: () => set({ openId: null }),
}));

/** Open a popover by id from outside React (e.g. the Cmd/Ctrl+, shortcut). */
export const openPopover = (id: string): void => usePopoverStore.getState().show(id);
