-- ============================================================================
-- Vamoose's Companion - EventBus
-- Simple pub/sub event system for decoupled module communication
-- ============================================================================

VC = VC or {}
VC.EventBus = {
    listeners = {}
}

function VC.EventBus:Register(event, callback)
    if not self.listeners[event] then
        self.listeners[event] = {}
    end
    table.insert(self.listeners[event], callback)
end

function VC.EventBus:Trigger(event, payload)
    if self.listeners[event] then
        for _, callback in ipairs(self.listeners[event]) do
            local status, err = pcall(callback, payload)
            if not status then
                print("|cffff0000[VC EventBus Error]|r", event, err)
            end
        end
    end
end

function VC.EventBus:Unregister(event, callback)
    if self.listeners[event] then
        for i, cb in ipairs(self.listeners[event]) do
            if cb == callback then
                table.remove(self.listeners[event], i)
                return true
            end
        end
    end
    return false
end
