using audit_logs.Services.Interfaces;


using Newtonsoft.Json;
using System.Threading.Tasks;

namespace audit_logs.Services;

public class DeepCopyService : IDeepCopyService
{
    public async Task<T> DeepCopyAsync<T>(T source)
    {
        var serialized = JsonConvert.SerializeObject(source);
        var deserialized = JsonConvert.DeserializeObject<T>(serialized);
        
        if (deserialized == null)
            throw new InvalidOperationException("Deserialization resulted in null");

        return deserialized;
    }
}