using Microsoft.AspNetCore.Razor.Language.Intermediate;
using System.Threading.Channels;

namespace ManagementApp.AServices.ThreadManage
{
    public class ConcurrentQueueThread: IConcurrentTasks
    {
        private readonly Channel<Func<CancellationToken, ValueTask>> queue;
        public ConcurrentQueueThread(int capacity) {
            var options = new BoundedChannelOptions(capacity)
            {
                FullMode = BoundedChannelFullMode.Wait
            };
            queue = Channel.CreateBounded<Func<CancellationToken, ValueTask>>(options);
        }
    

        public async ValueTask Add(Func<CancellationToken, ValueTask> value)
        {
            await queue.Writer.WriteAsync(value); 
        }

        public async ValueTask<Func<CancellationToken, ValueTask>> Last(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public async ValueTask<Func<CancellationToken, ValueTask>> First(CancellationToken cancellationToken)
        {
            
            return await queue.Reader.ReadAsync(cancellationToken);       
        }

       

    }

}
