using System;
using System.Threading;
using System.Threading.Tasks;
namespace Timeline.Services.Data
{
    public static class DataManagerExtensions
    {
        /// 
        /// Try to get an entry and throw  if not exist.
        /// 
        public static async Task GetEntryAndCheck(this IDataManager dataManager, string tag, string notExistMessage, CancellationToken cancellationToken = default)
        {
            if (dataManager is null)
                throw new ArgumentNullException(nameof(dataManager));
            if (tag is null)
                throw new ArgumentNullException(nameof(tag));
            if (notExistMessage is null)
                throw new ArgumentNullException(nameof(notExistMessage));
            var data = await dataManager.GetEntryAsync(tag, cancellationToken);
            if (data is null)
                throw new DatabaseCorruptedException(string.Format(Resource.GetEntryAndCheckNotExist, tag, notExistMessage));
            return data;
        }
    }
}