using System.IO.Pipelines;
using Microsoft.AspNetCore.Http.Features;

namespace CashLineERP.Api.Infrastructure;

internal sealed class CompatibleHttpResponseBodyFeature : IHttpResponseBodyFeature
{
    private readonly IHttpResponseBodyFeature _innerFeature;
    private PipeWriter? _writer;

    public CompatibleHttpResponseBodyFeature(IHttpResponseBodyFeature innerFeature)
    {
        _innerFeature = innerFeature;
    }

    public Stream Stream => _innerFeature.Stream;

    public PipeWriter Writer => _writer ??= PipeWriter.Create(Stream, new StreamPipeWriterOptions(leaveOpen: true));

    public void DisableBuffering()
    {
        _innerFeature.DisableBuffering();
    }

    public Task StartAsync(CancellationToken cancellationToken = default)
    {
        return _innerFeature.StartAsync(cancellationToken);
    }

    public Task SendFileAsync(string path, long offset, long? count, CancellationToken cancellationToken = default)
    {
        return _innerFeature.SendFileAsync(path, offset, count, cancellationToken);
    }

    public Task CompleteAsync()
    {
        return _innerFeature.CompleteAsync();
    }
}
