local LibClientBuild = Wheel:Set("LibClientBuild", 46)
if (not LibClientBuild) then
	return
end

-- Lua API
local pairs = pairs
local select = select
local string_match = string.match
local string_split = string.split
local tonumber = tonumber
local tostring = tostring

LibClientBuild.embeds = LibClientBuild.embeds or {}

local currentClientPatch, currentClientBuild = GetBuildInfo()
currentClientBuild = tonumber(currentClientBuild)

-- Let's create some constants for faster lookups
local MAJOR,MINOR,PATCH = string_split(".", currentClientPatch)

local IsClassic, IsTBC
local IsRetail, IsRetailBFA, IsRetailShadowlands

IsClassic = tonumber(MAJOR) == 1
IsTBC = tonumber(MAJOR) == 2
IsWotLK = tonumber(MAJOR) == 3
IsRetail = tonumber(MAJOR) >= 9
IsRetailBFA = tonumber(MAJOR) == 8
IsRetailShadowlands = tonumber(MAJOR) == 9
IsRetailDragonflight = tonumber(MAJOR) == 10

local builds = {}

-- Patch to build
builds["1.13.2"] 	= 31446
builds["1.13.3"] 	= 33526
builds["1.13.4"] 	= 34219
builds["2.5.1"] 	= 38707
builds["8.0.1"] 	= 27101
builds["8.1.0"] 	= 29600
builds["8.1.5"] 	= 29704
builds["8.2.0"] 	= 30920
builds["8.2.0"] 	= 31229
builds["8.2.5"] 	= 31960
builds["8.3.0"] 	= 34220
builds["8.3.7"] 	= 35662
builds["9.0.1"] 	= 36577
builds["9.0.2"] 	= 37474
builds["9.0.5"] 	= 37988

-- Metas
builds["Classic"] 	= builds["1.13.7"]
builds["TBC"] 		= builds["2.5.1"]
builds["Retail"] 	= builds["8.3.0"]

-- Returns true if we're on a classic patch
LibClientBuild.IsClassic = function(self)
	return IsClassic
end

-- Returns true if we're on a classic TBC patch
LibClientBuild.IsTBC = function(self)
	return IsTBC
end

-- Returns true if we're on a classic WotLK patch
LibClientBuild.IsWotLK = function(self)
	return IsWotLK
end

-- Returns true if we're on a retail patch
LibClientBuild.IsRetail = function(self)
	return IsRetail
end

-- Returns true if we're on a retail BFA patch
LibClientBuild.IsRetailBFA = function(self)
	return IsRetailBFA
end

-- Returns true if we're on a retail Shadowlands patch
LibClientBuild.IsRetailShadowlands = function(self)
	return IsRetailShadowlands
end

-- Returns true if we're on a retail Dragonflight patch
LibClientBuild.IsRetailDragonflight = function(self)
	return IsRetailDragonflight
end

-- Return the build number for a given patch.
-- Return current build if the given patch is the current. EXPERIMENTAL!
LibClientBuild.GetClientBuildByPatch = function(self, patch)
	return (currentClientPatch == patch) and currentClientBuild or builds[patch]
end

LibClientBuild.GetCurrentClientPatch = function(self)
	return currentClientPatch
end

-- Return the current WoW client build
LibClientBuild.GetCurrentClientBuild = function(self)
	return currentClientBuild
end

-- Module embedding
local embedMethods = {
	IsClassic = true,
	IsTBC = true,
	IsWotLK = true,
	IsRetail = true,
	IsRetailBFA = true,
	IsRetailShadowlands = true,
	IsRetailDragonflight = true,
	GetClientBuildByPatch = true,
	GetCurrentClientBuild = true,
	GetCurrentClientPatch = true
}

LibClientBuild.Embed = function(self, target)
	for method in pairs(embedMethods) do
		target[method] = self[method]
	end
	self.embeds[target] = true
	return target
end

-- Upgrade existing embeds, if any
for target in pairs(LibClientBuild.embeds) do
	LibClientBuild:Embed(target)
end
