-- [ ORBIT COMPONENT HELPERS ]-----------------------------------------------------------------------
-- Shared utility functions for component positioning and sizing.
-- Handles WoW 12.0+ secret values safely.

local _, Orbit = ...
local Engine = Orbit.Engine

Engine.ComponentHelpers = {}
local Helpers = Engine.ComponentHelpers

-- [ CONFIGURATION ] ---------------------------------------------------------------------------------

Helpers.PADDING = 25 -- Drag boundary padding
local DEFAULT_MIN_WIDTH = 40
local DEFAULT_MIN_HEIGHT = 16

-- [ SAFE SIZE ACCESSOR ] ----------------------------------------------------------------------------

-- For FontStrings, uses GetStringWidth/GetStringHeight which return actual text bounds
function Helpers.SafeGetSize(region)
    if not region then
        return DEFAULT_MIN_WIDTH, DEFAULT_MIN_HEIGHT
    end

    local width, height = DEFAULT_MIN_WIDTH, DEFAULT_MIN_HEIGHT

    -- For FontStrings, prefer GetStringWidth/GetStringHeight for actual text bounds
    local isFontString = region.GetStringWidth ~= nil

    -- Try to get width
    local ok, w = pcall(function()
        local val
        if isFontString then
            val = region:GetStringWidth()
            if (not val or val <= 0) and region.GetWidth then
                val = region:GetWidth()
            end
        else
            val = region:GetWidth()
        end
        if issecretvalue and issecretvalue(val) then
            return nil
        end
        return val
    end)
    if ok and w and type(w) == "number" and w > 0 then
        width = w
    end

    -- Try to get height
    local ok2, h = pcall(function()
        local val
        if isFontString then
            val = region:GetStringHeight()
            if (not val or val <= 0) and region.GetHeight then
                val = region:GetHeight()
            end
        else
            val = region:GetHeight()
        end
        if issecretvalue and issecretvalue(val) then
            return nil
        end
        return val
    end)
    if ok2 and h and type(h) == "number" and h > 0 then
        height = h
    end

    return width, height
end

-- [ SAFE NUMBER ACCESSOR ] --------------------------------------------------------------------------

function Helpers.SafeGetNumber(val, default)
    if val == nil then
        return default
    end
    if issecretvalue and issecretvalue(val) then
        return default
    end
    if type(val) ~= "number" then
        return default
    end
    return val
end

