// src/utils/EventBus.ts
type EventHandler = (detail?: any) => void

class EventBus {
    private listeners: { [event: string]: EventHandler[] } = {}

    // Methode zum Abonnieren eines Ereignisses
    public on(event: string, handler: EventHandler): () => void {
        if (!this.listeners[event]) {
            this.listeners[event] = []
        }
        this.listeners[event].push(handler)
        return () => this.off(event, handler)
    }

    // Methode zum Abmelden eines Ereignisses
    private off(event: string, handler: EventHandler): void {
        if (!this.listeners[event]) return
        this.listeners[event] = this.listeners[event].filter((h) => h !== handler)
    }

    // Methode zum Senden eines Ereignisses
    public emit(event: string, detail?: any): void {
        if (!this.listeners[event]) return
        this.listeners[event].forEach((handler) => handler(detail))
    }
}

// Exportiere eine Instanz der Eventbus-Klasse
export const eventBus = new EventBus()
