local PT = AhUI
if not PT then return end

PT.InfoPanel = PT.InfoPanel or {}
local IP = PT.InfoPanel

IP.elementTypeInfo   = IP.elementTypeInfo   or {} 
IP.availableTypeInfo = IP.availableTypeInfo or {} 

function IP:RegisterElementInfo(elemType, def)
    if not elemType or elemType == "" then return end
    if type(def) ~= "table" then return end

    local typeId = elemType:lower()

    def.type  = typeId
    def.name  = def.name or elemType
    def.order = def.order or 100

    self.elementTypeInfo[typeId] = def
end

function IP:ScanElementTypes()
    wipe(self.availableTypeInfo)

    for typ, def in pairs(self.elementTypeInfo) do
        local ok = true
        if def.isAvailable then
            ok = def.isAvailable() and true or false
        end
        if ok then
            self.availableTypeInfo[typ] = def
        end
    end
end

function IP:GetAvailableElementTypes()
    return self.availableTypeInfo or {}
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
f:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")

f:SetScript("OnEvent", function()
    if IP.ScanElementTypes then
        IP:ScanElementTypes()
    end
end)

C_Timer.After(0, function()
    if IP.ScanElementTypes then
        IP:ScanElementTypes()
    end
end)
