-- Portal Pro :: Core/Compat.lua
-- Classic Era 1.15.9's addon API tracks TBC Classic (2.5.6), not retail. Some C_* namespaces
-- exist, some don't, and which is which has shifted across 1.15.x point releases. Everything
-- version-sensitive gets resolved once, here, so no other file has to care.

local ADDON_NAME, PP = ...

local C = {}
PP.C = C

--------------------------------------------------------------------------------
-- Addon metadata
--------------------------------------------------------------------------------

local getMeta = (C_AddOns and C_AddOns.GetAddOnMetadata) or _G.GetAddOnMetadata
local rawVersion = (getMeta and getMeta(ADDON_NAME, "Version")) or ""

-- The TOC carries @project-version@, which the BigWigs packager substitutes at release
-- time. Running straight from a working copy leaves the token unreplaced, so show
-- something honest rather than a literal keyword.
if rawVersion == "" or rawVersion:find("@project%-version@") then
	PP.version = "dev"
	PP.isDev = true
else
	PP.version = rawVersion
end

--------------------------------------------------------------------------------
-- Spells
--------------------------------------------------------------------------------

--- @return string|nil name
function C.SpellName(spellID)
	if C_Spell and C_Spell.GetSpellInfo then
		local info = C_Spell.GetSpellInfo(spellID)
		return info and info.name
	end
	return (GetSpellInfo(spellID))
end

--- @return number|nil fileID
function C.SpellIcon(spellID)
	if C_Spell and C_Spell.GetSpellTexture then
		return C_Spell.GetSpellTexture(spellID)
	end
	if _G.GetSpellTexture then
		return GetSpellTexture(spellID)
	end
	return select(3, GetSpellInfo(spellID))
end

--- True if the spell is in the player's spellbook.
function C.IsSpellKnown(spellID)
	if _G.IsSpellKnown then
		local ok, known = pcall(IsSpellKnown, spellID)
		if ok then return known end
	end
	return C.SpellName(spellID) ~= nil
end

--------------------------------------------------------------------------------
-- Items
--------------------------------------------------------------------------------

function C.ItemCount(itemID)
	if C_Item and C_Item.GetItemCount then
		return C_Item.GetItemCount(itemID) or 0
	end
	return GetItemCount(itemID) or 0
end

function C.ItemName(itemID)
	if C_Item and C_Item.GetItemInfo then
		return (C_Item.GetItemInfo(itemID))
	end
	return (GetItemInfo(itemID))
end

function C.ItemIcon(itemID)
	if C_Item and C_Item.GetItemIconByID then
		return C_Item.GetItemIconByID(itemID)
	end
	if _G.GetItemIcon then
		return GetItemIcon(itemID)
	end
	return select(10, GetItemInfo(itemID))
end

--------------------------------------------------------------------------------
-- Grouping
--------------------------------------------------------------------------------

function C.InviteUnit(name)
	if C_PartyInfo and C_PartyInfo.InviteUnit then
		return C_PartyInfo.InviteUnit(name)
	end
	return InviteUnit(name)
end

function C.UninviteUnit(name)
	if C_PartyInfo and C_PartyInfo.UninviteUnit then
		return C_PartyInfo.UninviteUnit(name)
	end
	return UninviteUnit(name)
end

function C.LeaveParty()
	if C_PartyInfo and C_PartyInfo.LeaveParty then
		return C_PartyInfo.LeaveParty()
	end
	return LeaveParty()
end

--------------------------------------------------------------------------------
-- Misc
--------------------------------------------------------------------------------

--- Strip the realm suffix from a chat sender.
function C.ShortName(name)
	if not name then return nil end
	if _G.Ambiguate then
		name = Ambiguate(name, "short")
	end
	return (name:gsub("%-.*$", ""))
end

--- The name of the person we're currently trading with, if any.
function C.TradePartner()
	local frameText = _G.TradeFrameRecipientNameText
	if frameText then
		local n = frameText:GetText()
		if n and n ~= "" then return C.ShortName(n) end
	end
	local n = UnitName("npc")
	if n and n ~= "" then return C.ShortName(n) end
	return nil
end
