import { writable } from './store';

// A dialog request pushed by a server-side control-logic action.dialog node and
// delivered over the WebSocket. Unlike panel logic dialogs (lib/logic.ts) these
// originate on the server and are addressed to the connected user by identity.
export interface ControlDialog {
  id: string;
  kind: 'info' | 'error' | 'input';
  title?: string;
  message?: string;
}

// Active control-logic dialogs awaiting the user's acknowledgement / input.
export const controlDialogs = writable<ControlDialog[]>([]);

// pushControlDialog is invoked by the WS client on a "dialog" message.
export function pushControlDialog(d: ControlDialog): void {
  controlDialogs.update(list => (list.some(x => x.id === d.id) ? list : [...list, d]));
}

// dismissControlDialog removes a dialog once answered or cancelled.
export function dismissControlDialog(id: string): void {
  controlDialogs.update(list => list.filter(d => d.id !== id));
}
