--[[
    Commander 3.0 - TUI Cell

    Represents a single cell in the character grid.
    Each cell has a character and optional foreground color.

    Uses object pooling to reduce GC pressure during rendering.
]]

local ADDON_NAME, C3 = ...

-------------------------------------------------------------------------------
-- Cell Class
-------------------------------------------------------------------------------

local Cell = {}
Cell.__index = Cell

C3.TUI = C3.TUI or {}
C3.TUI.Cell = Cell

-- Cell pool for reduced GC pressure
local CellPool = {}
local CELL_POOL_MAX = 500

---Acquire a Cell from the pool or create new
---@param char? string Single character (default " ")
---@param fg? string Foreground hex color (e.g., "88C0D0")
---@return table
function Cell.Acquire(char, fg)
    local cell = table.remove(CellPool)
    if cell then
        cell.char = char or " "
        cell.fg = fg
        return cell
    end
    return Cell.new(char, fg)
end

---Release a Cell back to the pool
---@param cell table
function Cell.Release(cell)
    if #CellPool < CELL_POOL_MAX then
        cell.char = " "
        cell.fg = nil
        table.insert(CellPool, cell)
    end
end

---Create a new Cell
---@param char? string Single character (default " ")
---@param fg? string Foreground hex color
---@return table
function Cell.new(char, fg)
    return setmetatable({
        char = char or " ",
        fg = fg,
    }, Cell)
end

---Reset cell to empty state
function Cell:Reset()
    self.char = " "
    self.fg = nil
end

---Clone this cell
---@return table
function Cell:Clone()
    return Cell.Acquire(self.char, self.fg)
end

---Check if cell equals another
---@param other table
---@return boolean
function Cell:Equals(other)
    return self.char == other.char and self.fg == other.fg
end

---Get pool statistics
---@return number poolSize, number maxSize
function Cell.GetPoolStats()
    return #CellPool, CELL_POOL_MAX
end

return Cell
