using MessagePack;
using System;
using System.Buffers;

namespace Server
{
    public static class MessageHelper
    {
        public static T MessagePackDeserialize<T>(ReadOnlyMemory<byte> buffer)
        {
            return MessagePack.MessagePackSerializer.Deserialize<T>(buffer);
        }
        
        public static byte[] MessagePackSerialize<T>(T obj)
        {
            return MessagePack.MessagePackSerializer.Serialize(obj);
        }

        public static byte[] MessagePackSerialize<T>(byte byteType, T data)
        {
            ArrayBufferWriter<byte> arrayBufferWriter = new ArrayBufferWriter<byte>();
            arrayBufferWriter.GetSpan(1)[0] = byteType;
            arrayBufferWriter.Advance(1);
            MessagePackSerializer.Serialize(arrayBufferWriter, data);
            return arrayBufferWriter.WrittenSpan.ToArray();
        }
    }
}