-- ============================================================================
-- Housing Decor Guide - EventBus
-- Simple pub/sub event system for decoupled module communication
-- ============================================================================

local addonName, privateDB = ...

-- Use the addon's global namespace (created in HDG_Core.lua)
local HDG = _G.HousingDecorGuide

HDG.EventBus = {
    listeners = {}
}

-- Register a callback for an event
function HDG.EventBus:Register(event, callback)
    if not self.listeners[event] then
        self.listeners[event] = {}
    end
    table.insert(self.listeners[event], callback)
end

-- Trigger an event with optional payload
function HDG.EventBus:Trigger(event, payload)
    if self.listeners[event] then
        for _, callback in ipairs(self.listeners[event]) do
            -- Safely call callback to prevent one error breaking the bus
            local status, err = pcall(callback, payload)
            if not status then
                print("|cffff0000[HDG EventBus Error]|r", event, err)
            end
        end
    end
end

-- Unregister a specific callback (optional utility)
function HDG.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
