local IC = InvenCraftInfo2
IC.output = {}

local _G = _G
local min = _G.math.min
local reagent
local reagentParser = "^"..SPELL_REAGENTS.."(.+)"
local tooltip = CreateFrame("GameTooltip")
local dummy = tooltip:CreateFontString(nil, "OVERLAY", "GameFontNormal")
for i = 1, 4 do
	tooltip[i] = tooltip:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	tooltip:AddFontStrings(tooltip[i], dummy)
end

function IC:GetReagent(id)
	tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
	tooltip:ClearLines()
	tooltip:SetHyperlink("enchant:"..id)
	for i = 1, min(tooltip:NumLines(), #tooltip) do
		reagent = tooltip[i]:IsShown() and tooltip[i]:GetText()
		if reagent then
			reagent = reagent:match(reagentParser)
			if reagent then
				return reagent:gsub("|cff%x%x%x%x%x%x", ""):gsub("|r", ""):gsub("([^)]),", "%1 (1),"):gsub("([^)])$", "%1 (1)"):trim()
			end
		end
	end
	return nil
end

