import { createContext } from "react";
import { Node, Edge } from "@xyflow/react";
import mitt, { Emitter } from "mitt";

export type Events = {
  // ---- editing request events (compo -> ?) ----
  reqRemoveNode: string;
  reqModifyNode: {id: string, data: any};

  // ---- editing completion events (view -> ?) ----
  onNodeAdded: Node;
  onNodeRemoved: string;
  onNodeModified: Node;

  onEdgeAdded: Edge;
  onEdgeRemoved: string;
  onEdgeModified: Edge;

  // ---- running events (runner -> ?) ----
  reqNodeReset: {id: string},
  onNodePending: {id: string},
  onNodeRunning: {id: string, handle: string|null, symbols: string[], remaining: number | undefined},
  onNodeAborted: {id: string, msg: string},
  onNodeDone: {id: string},
};

export type Bus = Emitter<Events>;
export default Bus;
export const createBus = ()=> mitt<Events>();

export const BusContext = createContext<Bus|undefined>(undefined);
