import { listen, type UnlistenFn, type EventCallback } from "@tauri-apps/api/event";

/**
 * Backend event names emitted from src-tauri/src/sidecar.rs.
 * Keep this list in sync with `app.emit(...)` calls there.
 */
export const Events = {
  SidecarReady: "sidecar://ready",
  SidecarExited: "sidecar://exited",
  PeersUpdated: "peers://updated",
  StatsUpdated: "stats://updated",
  RoutesUpdated: "routes://updated",
  MessageIncoming: "messages://incoming",
} as const;

export type EventName = (typeof Events)[keyof typeof Events];

export async function on<T>(
  event: EventName,
  handler: EventCallback<T>,
): Promise<UnlistenFn> {
  return listen<T>(event, handler);
}
