GuildHelper = GuildHelper or {}

local addon = GuildHelper

addon.SyncProtocol = addon.SyncProtocol or {}

local separator = "\31"

function addon.SyncProtocol:Encode(envelope)
    return table.concat({
        envelope.op or "",
        envelope.sessionId or "",
        envelope.tableName or "",
        tostring(envelope.seq or 0),
        tostring(envelope.total or 0),
        envelope.payload or "",
    }, separator)
end

function addon.SyncProtocol:Decode(message)
    local op, sessionId, tableName, seq, total, payload = strsplit(separator, message, 6)

    if not op or op == "" then
        return nil
    end

    return {
        op = op,
        sessionId = sessionId,
        tableName = tableName,
        seq = tonumber(seq) or 0,
        total = tonumber(total) or 0,
        payload = payload or "",
    }
end
