local Position = require("lib.position")

---@class Stack
---@field elements Position[]
local Stack = {}
Stack.__index = Stack


---@return Stack
function Stack:Create()
  -- stack table
  local t = {}
  setmetatable(t, Stack)

  -- entry table
  t.elements = {}

  return t
end


---@param element Position
function Stack:Push(element)
    if element == nil or element == {} then
        printError("Failed to push empty element to the stack!")
        return
    end

    table.insert(self.elements, element)
end


---@return Position | nil
function Stack:Pop()
    if self:Count() == 0 then
        printError("Failed to pop element from the empty stack!")
        return nil
    end

    return table.remove(self.elements)
end


---@return Position | nil
function Stack:Peek()
    if self:Count() == 0 then
        printError("Failed to peek element from the empty stack!")
        return nil
    end

    return self.elements[#self.elements]
end


---@return number
function Stack:Count()
    return #self.elements
end


return Stack