local addonName, addon = ...

function addon:Initialize()
    if self.UserData and self.UserData.Initialize then
        self.UserData:Initialize()
    end

    if self.DB and self.DB.Initialize then
        self.DB:Initialize()
    end

    if self.LootTracker
        and self.LootTracker.DB
        and self.LootTracker.DB.Initialize
    then
        self.LootTracker.DB:Initialize()
    end

    if self.LootTracker
        and self.LootTracker.Query
        and self.LootTracker.Query.Initialize
    then
        self.LootTracker.Query:Initialize()
    end

    if self.LootTracker
        and self.LootTracker.Crafting
        and self.LootTracker.Crafting.InitializeRecipeDatabase
    then
        self.LootTracker.Crafting:InitializeRecipeDatabase()
    end

    if self.GameData and self.GameData.Initialize then
        self.GameData:Initialize()
    end

    if self.UI and self.UI.Initialize then
        self.UI:Initialize()
    end

    if self.MinimapButton and self.MinimapButton.Initialize then
        self.MinimapButton:Initialize()
    end
end

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("ADDON_LOADED")

eventFrame:SetScript("OnEvent", function(_, event, name)
    if event ~= "ADDON_LOADED" or name ~= addonName then
        return
    end

    addon:Initialize()
end)

SLASH_WHATSNEXT1 = "/wn"
SLASH_WHATSNEXT2 = "/whatsnext"

SlashCmdList["WHATSNEXT"] = function(message)
    local command, rest = tostring(message or ""):match("^%s*(%S*)%s*(.-)%s*$")
    command = string.lower(command or "")

    if command == "endb" or command == "englishdb" then
        if addon.LootTracker
            and addon.LootTracker.EnglishDBBuilder
            and addon.LootTracker.EnglishDBBuilder.HandleSlash
        then
            addon.LootTracker.EnglishDBBuilder:HandleSlash(rest)
        end
        return
    end

    if addon.UI and addon.UI.Toggle then
        addon.UI:Toggle()
    end
end
