local _AddonName, _EchoRaidTools = ...;

local module = _EchoRaidTools:GetModule("todo-list")

local LibDeflate = LibStub:GetLibrary("LibDeflate")
local libSerializer = LibStub:GetLibrary("AceSerializer-3.0")
local configForDeflate = { level = 1 }

---@param t table
---@return string
function module:Compress(t)
    local serialData = libSerializer:Serialize(t)
    local compressedData = LibDeflate:CompressDeflate(serialData, configForDeflate)
    local encodedData = LibDeflate:EncodeForPrint(compressedData);
    encodedData = "ETODO:" .. encodedData
    return encodedData;
end

---@param encodedData string
---@return table|nil
function module:Decompress(encodedData)
    if not encodedData then return nil end

    local prefix = encodedData:sub(1, 6)
    if prefix ~= "ETODO:" then return nil end
    encodedData = encodedData:sub(7)

    local compressedData = LibDeflate:DecodeForPrint(encodedData);
    if not compressedData then return nil end

    local serialData = LibDeflate:DecompressDeflate(compressedData);
    if not serialData then return nil; end

    local status, data = libSerializer:Deserialize(serialData);
    if not status then return nil; end

    return data;
end
