using System.Buffers;
using size_t = System.UInt32;

#nullable disable

namespace SharpCompress.Compressors.Rar.UnpackV2017;

internal partial class BitInput
{
    public BitInput(bool AllocBuffer)
    {
        ExternalBuffer = false;
        if (AllocBuffer)
        {
            // getbits32 attempts to read data from InAddr, ... InAddr+3 positions.
            // So let's allocate 3 additional bytes for situation, when we need to
            // read only 1 byte from the last position of buffer and avoid a crash
            // from access to next 3 bytes, which contents we do not need.
            size_t BufSize = MAX_SIZE + 3;
            InBuf = ArrayPool<byte>.Shared.Rent(checked((int)BufSize));

            // Ensure that we get predictable results when accessing bytes in area
            // not filled with read data.
            //memset(InBuf,0,BufSize);
        }
        else
        {
            InBuf = null;
        }
    }

    //BitInput::~BitInput()
    //{
    //    if (!ExternalBuffer)
    //    delete[] InBuf;
    //}
    //

    public void faddbits(uint Bits) =>
        // Function wrapped version of inline addbits to save code size.
        addbits(Bits);

    public uint fgetbits() =>
        // Function wrapped version of inline getbits to save code size.
        getbits();

    public virtual void Dispose()
    {
        if (!ExternalBuffer && InBuf != null)
        {
            ArrayPool<byte>.Shared.Return(InBuf);
            InBuf = null;
        }
    }
}
