--[[****************************************************************************
  * RealNibba.Overlay by Saiket                                                 *
  * RealNibba.Overlay.ZoneData.lua - Zone size data and localized zone names.   *
  ****************************************************************************]]


local Overlay = _RealNibbaOverlay; -- integrated into FarmPack
Overlay.ZoneMaps = {}; -- [ LocalizedZoneName ] = MapFile;

local Indexes = {};
local Widths = {};
local Heights = {};
local Names = {};

-- LibMapData-1.0 provides accurate zone dimensions for every outdoor zone.
-- Used to populate size data for all zones, not just ones with PathData.
local LMD = LibStub and LibStub("LibMapData-1.0", true);




--[[****************************************************************************
  * Function: RealNibba.Overlay.GetZoneSize                                     *
  * Description: Gets the width and height of a zone in yards.                 *
  ****************************************************************************]]
function Overlay.GetZoneSize ( Map )
	Map = Indexes[ Map ];
	return Widths[ Map ], Heights[ Map ];
end
--[[****************************************************************************
  * Function: RealNibba.Overlay.GetZoneName                                     *
  * Description: Gets a localized zone name for a map.                         *
  ****************************************************************************]]
function Overlay.GetZoneName ( Map )
	return Names[ Indexes[ Map ] ];
end




local function Add ( Map, Width, Height )
	local Index = #Widths + 1;
	Indexes[ Map ] = Index;
	Widths[ Index ] = Width;
	Heights[ Index ] = Height;
end

-- Verified accurate dimensions for zones that have PathData.
-- All other outdoor zones are populated via LibMapData below.
Add( 492, 6270.833313, 4181.250000 ); -- Icecrown
Add( 495, 8493.750000, 5662.500000 ); -- The Storm Peaks


-- Build a lookup table for localized zone names to map files.
-- Iterates every continent/zone; gets dimensions from LibMapData for any zone
-- not already in the hardcoded table above. This means the overlay framework
-- is ready for every outdoor zone — paths can be added to PathData at any time.
local function HandleZones ( ContinentIndex, ... )
	for ZoneIndex = 1, select( "#", ... ) do
		SetMapZoom( ContinentIndex, ZoneIndex );

		local Map  = GetCurrentMapAreaID() - 1;
		local Name = select( ZoneIndex, ... );

		-- Populate size data from LibMapData for zones not hardcoded above
		if not Indexes[ Map ] and LMD then
			local w, h = LMD:MapArea( GetMapInfo(), 0 );
			if w and w > 0 then
				Add( Map, w, h );
			end
		end

		if Indexes[ Map ] then
			Names[ Indexes[ Map ] ] = Name;
			Overlay.ZoneMaps[ Name ] = Map;
		end
	end
end
-- Carbonite compatibility: skip zone enumeration at load time —
-- SetMapZoom calls corrupt Carbonite's internal map state.
-- The overlay still works via hardcoded zones above + Carbonite module.
local _carboniteLoaded = IsAddOnLoaded("Carbonite") or IsAddOnLoaded("Carbonite_Core") or (Nx ~= nil)
if not _carboniteLoaded then
	for ContinentIndex = 1, select( "#", GetMapContinents() ) do
		HandleZones( ContinentIndex, GetMapZones( ContinentIndex ) );
	end
end
