"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferStream = void 0;
const stream_1 = require("stream");
class BufferStream {
    constructor(buffer, isBigEndian = false) {
        this.currentIndex = 0;
        this.isBigEndian = isBigEndian;
        this.readStream = new stream_1.Readable();
        this.readStream.push(buffer);
        this.readStream.push(null);
        this.bufferLength = buffer.length;
    }
    readString(size, trimNulls = true) {
        const str = this.read(size).toString();
        if (trimNulls) {
            return str.replace(/\0/g, "");
        }
        return str;
    }
    readInt(size = 4, unsigned = false) {
        if (unsigned) {
            return this.isBigEndian ? this.read(size).readUIntBE(0, size) : this.read(size).readUIntLE(0, size);
        }
        else {
            return this.isBigEndian ? this.read(size).readIntBE(0, size) : this.read(size).readIntLE(0, size);
        }
    }
    readInts(amount, size = 4, unsigned = false) {
        const nums = [];
        for (let i = 0; i < amount; i++) {
            nums.push(this.readInt(size, unsigned));
        }
        return nums;
    }
    readBigInt(unsigned = false) {
        if (unsigned) {
            return this.isBigEndian ? this.read(8).readBigUInt64BE() : this.read(8).readBigUInt64LE();
        }
        else {
            return this.isBigEndian ? this.read(8).readBigInt64BE() : this.read(8).readBigInt64LE();
        }
    }
    readFloat() {
        return this.isBigEndian ? this.read(4).readFloatBE() : this.read(4).readFloatLE();
    }
    readFloats(amount) {
        const nums = [];
        for (let i = 0; i < amount; i++) {
            nums.push(this.readFloat());
        }
        return nums;
    }
    readUntilNull(writeBuffer = []) {
        const byte = this.read(1)[0];
        if (byte === 0x00) {
            return Buffer.from(writeBuffer);
        }
        else {
            writeBuffer.push(byte);
            return this.readUntilNull(writeBuffer);
        }
    }
    readNulls() {
        let currentByte = this.read(1)[0];
        let count = 0;
        while (currentByte === 0x00) {
            currentByte = this.read(1)[0];
            count++;
        }
        return count;
    }
    readIntFloatPairs() {
        const options = [];
        const size = this.readStream.readableLength / 8;
        for (let i = 0; i < size; i++) {
            const key = this.readInt();
            const val = this.readFloat();
            options.push([key, val]);
        }
        return options;
    }
    readBool() {
        const int = this.readInt(1, true);
        return Boolean(int);
    }
    read(size) {
        this.currentIndex = size === undefined ? this.bufferLength : this.currentIndex + size;
        return this.readStream.read(size);
    }
    destroy() {
        this.readStream.destroy();
    }
    getPosition() {
        return this.currentIndex;
    }
}
exports.BufferStream = BufferStream;
//# sourceMappingURL=buffer-stream.js.map