-- HDG_HT_Registry.lua
-- Maps widget id (LAYOUT_CONFIG.widgets key) → widget factory function.
-- Each widget file calls Registry.Register(id, factory) at module load.
-- TestWindow / HouseTab call Registry.Get(id) to look up the factory.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
local Registry = {}
privateDB.HDG_HT.Registry = Registry

local _factories = {}

function Registry.Register(id, factory)
    _factories[id] = factory
end

function Registry.Get(id)
    return _factories[id]
end

function Registry.HasWidget(id)
    return _factories[id] ~= nil
end

function Registry.AllIDs()
    local ids = {}
    for id in pairs(_factories) do ids[#ids + 1] = id end
    return ids
end

-- ----------------------------------------------------------------------------
-- Widget registrations (each widget file gets one line here).
-- We do registrations centrally so the Registry has authoritative knowledge
-- of which widgets are available without scanning files.
-- ----------------------------------------------------------------------------
local function registerAll()
    local W = privateDB.HDG_HT.Widgets or {}
    if W.Capacity then Registry.Register("capacity", W.Capacity.Create) end
    if W.CraftableNow then Registry.Register("craftableNow", W.CraftableNow.Create) end
    if W.HotPicks then Registry.Register("hotPicks", W.HotPicks.Create) end
    if W.RecentActivity then Registry.Register("recentActivity", W.RecentActivity.Create) end
    if W.NextRewards then Registry.Register("nextRewards", W.NextRewards.Create) end
    if W.TrophyShelf    then Registry.Register("trophyShelf",    W.TrophyShelf.Create)    end
    if W.SourceDonut    then Registry.Register("sourceDonut",    W.SourceDonut.Create)    end
    if W.ExpansionDonut then Registry.Register("expansionDonut", W.ExpansionDonut.Create) end
    if W.DecoratorProfile then Registry.Register("decoratorProfile", W.DecoratorProfile.Create) end
    if W.CloseCards    then Registry.Register("closeCards",    W.CloseCards.Create)    end
    if W.Featured      then Registry.Register("featured",      W.Featured.Create)      end
    if W.StyleAffinity then Registry.Register("styleAffinity", W.StyleAffinity.Create) end
    if W.MultiHouse    then Registry.Register("multiHouse",    W.MultiHouse.Create)    end
    if W.Records       then Registry.Register("records",       W.Records.Create)       end
    if W.ThemedSets    then Registry.Register("themedSets",    W.ThemedSets.Create)    end
    if W.LumberWallet  then Registry.Register("lumberWallet",  W.LumberWallet.Create)  end
    if W.DecorCurrency then Registry.Register("decorCurrency", W.DecorCurrency.Create) end
    if W.GoblinTopLumber then Registry.Register("goblinTopLumber", W.GoblinTopLumber.Create) end
    if W.Favorites     then Registry.Register("favorites",     W.Favorites.Create)     end
    if W.TopVendors    then Registry.Register("topVendors",    W.TopVendors.Create)    end
    if W.RitualSites   then Registry.Register("ritualSites",   W.RitualSites.Create)   end
    if W.AbyssAnglers  then Registry.Register("abyssAnglers",  W.AbyssAnglers.Create)  end
    if W.DecorDuels    then Registry.Register("decorDuels",    W.DecorDuels.Create)    end
    if W.Velocity      then Registry.Register("velocity",      W.Velocity.Create)      end
end

-- Defer registration until after all widget files are loaded.
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function(self)
    registerAll()
    self:UnregisterAllEvents()
end)
