using MongoDB.Bson;

namespace adas_core.Domain.Utils;

public static class BsonUtils
{
    public static object? ToObject(this BsonValue val)
    {
        if (val.IsInt32) return val.AsInt32;

        if (val.IsInt64) return val.AsInt64;

        if (val.IsDouble) return val.AsDouble;

        if (val.IsValidDateTime) return val.ToUniversalTime();

        if (val.IsString) return val.AsString;

        return null;
    }

    public static BsonValue? Get(this BsonDocument doc, string key)
    {
        return doc.TryGetValue(key, out var value) ? value : null;
    }
}