"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var number_decoder_1 = require("../../client/impl/converter/body/number-decoder");
var format_helper_1 = require("../format/format-helper");
/**
 * TODO clean usage and remove in favor of KaitaiStream
 * @deprecated
 */
var ByteBuffer = /** @class */ (function () {
    function ByteBuffer(data) {
        this._lsb = false;
        this._offset = 0;
        this._data = data;
    }
    Object.defineProperty(ByteBuffer.prototype, "lsb", {
        get: function () {
            return this._lsb;
        },
        set: function (isLsb) {
            this._lsb = isLsb;
        },
        enumerable: true,
        configurable: true
    });
    ByteBuffer.merge = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var totalSize = args.reduce(function (previouseValue, currentValue) { return previouseValue + currentValue.length; }, 0);
        var buffer = ByteBuffer.create(totalSize);
        args.forEach(function (value) {
            buffer.add(value);
        });
        return buffer;
    };
    ByteBuffer.from = function (value) {
        return new ByteBuffer(value);
    };
    ByteBuffer.prototype.readNumber = function (sizeOf, signed, lsb) {
        if (sizeOf === void 0) { sizeOf = 4; }
        if (signed === void 0) { signed = true; }
        if (lsb === void 0) { lsb = undefined; }
        var data = this._data.slice(this._offset, this._offset + sizeOf);
        lsb = lsb === undefined ? this._lsb : lsb;
        if (this._data.byteLength < this._offset + sizeOf) {
            throw new Error("Buffer overflow at index " + (this._offset + sizeOf) + ". Max is " + data.byteLength);
        }
        // logger.debug(`Read data ${this._offset} to ${this._offset+sizeOf} <=> ${Buffer.from(data).toString("hex")}`);
        var number = number_decoder_1.NumberConverter.fromBytes(data, sizeOf, signed, lsb);
        this._offset += sizeOf;
        return number;
    };
    ByteBuffer.prototype.get_number = function (length) {
        return this.readNumber(length);
    };
    ByteBuffer.prototype.get_string = function (length) {
        return format_helper_1.FormatHelper.toAsciiString(this.readArray(length));
    };
    ByteBuffer.prototype.get_bytes = function (length) {
        return this.readArray(length);
    };
    ByteBuffer.prototype.put_bytes = function (data, length) {
        this.add(data, length);
        return this;
    };
    ByteBuffer.prototype.readUnsignedNumber = function (sizeOf, lsb) {
        if (sizeOf === void 0) { sizeOf = 4; }
        return this.readNumber(sizeOf, false, lsb);
    };
    /**
     *
     * @param length
     */
    ByteBuffer.prototype.readArray = function (length) {
        if (!length) {
            length = this._data.length - this._offset;
        }
        if (length == 0) {
            return new Uint8Array(0);
        }
        var value = this._data.subarray(this._offset, this._offset + length);
        this._offset += length;
        return value;
    };
    Object.defineProperty(ByteBuffer.prototype, "data", {
        get: function () {
            return this._data;
        },
        enumerable: true,
        configurable: true
    });
    ByteBuffer.prototype.put_boolean = function (value, mask) {
        if (value) {
            this._data[this._offset] |= mask;
        }
        else {
            this._data[this._offset] &= ~mask;
        }
        return this;
    };
    ByteBuffer.prototype.put_string = function (value, length) {
        var data = format_helper_1.FormatHelper.toByteBuffer(value);
        this.add(data, length);
        return this;
    };
    /**
     * Build data from 0 to offset
     */
    ByteBuffer.prototype.build = function () {
        return this._data.slice(0, this._offset);
    };
    ByteBuffer.prototype.forward = function (size) {
        this._offset += size;
        return this;
    };
    ByteBuffer.prototype.add = function (data, length) {
        if (length) {
            data = data.slice(0, length);
        }
        else {
            length = data ? data.length : 0;
        }
        if (length == 0)
            return this;
        this._data.set(data, this._offset);
        this._offset += length;
        return this;
    };
    ByteBuffer.prototype.put_number = function (value, sizeOf) {
        return this.addNumber(value, sizeOf);
    };
    ByteBuffer.prototype.addNumber = function (value, sizeOf, lsb) {
        if (sizeOf === void 0) { sizeOf = 4; }
        if (lsb === void 0) { lsb = false; }
        lsb = lsb === undefined ? this._lsb : lsb;
        var data = number_decoder_1.NumberConverter.toOpaqueMsb(value, sizeOf * 8);
        if (lsb) {
            data = data.reverse();
        }
        this.add(data);
        return this;
    };
    ByteBuffer.create = function (size) {
        return new ByteBuffer(new Uint8Array(size));
    };
    Object.defineProperty(ByteBuffer.prototype, "offset", {
        get: function () {
            return this._offset;
        },
        set: function (offset) {
            this._offset = offset;
        },
        enumerable: true,
        configurable: true
    });
    return ByteBuffer;
}());
exports.ByteBuffer = ByteBuffer;
