import { Checksum } from '@iotize/tap/client/impl';
/**
 * Created by IoTize on 19/04/2018.
 * <p>
 * Glue packet chunk to build the original ble packet
 * @param {number} bufferLength
 * @class
 */
var BLEPacketBuilder = /** @class */ (function () {
    function BLEPacketBuilder(bufferLength) {
        this.bufferOffset = 0;
        this.dataLength = 0;
        this.data = new Uint8Array(bufferLength);
        this.reset();
    }
    /**
     * Append new chunk of data into the builder
     * @param {Array} dataChunk the data chunk. The first byte must be the position offset of this chunk in the buffer (in bytes)
     */
    BLEPacketBuilder.prototype.append = function (dataChunk) {
        this.bufferOffset = (dataChunk[0] & 0xff) - 1;
        if (this.bufferOffset + dataChunk.length > this.data.length) {
            throw new Error("Buffer size exceeded. Maximum size is " + this.data.length + " byte(s).");
        }
        for (var i = 1; i < dataChunk.length; i++) {
            this.data[this.bufferOffset + i] = dataChunk[i];
        }
        this.dataLength += dataChunk.length - 1;
    };
    BLEPacketBuilder.prototype.reset = function () {
        this.dataLength = 0;
        this.bufferOffset = 0;
    };
    BLEPacketBuilder.prototype.hasAllChunks = function () {
        return this.bufferOffset === -1;
    };
    BLEPacketBuilder.prototype.isChecksumValid = function () {
        var computedChecksum = this.getComputedChecksum();
        var expectedChecksum = this.getExpectedChecksum();
        return expectedChecksum === computedChecksum;
    };
    BLEPacketBuilder.prototype.getComputedChecksum = function () {
        return (Checksum.compute(this.data.subarray(0, this.dataLength - 1)) & 0xff);
    };
    BLEPacketBuilder.prototype.getExpectedChecksum = function () {
        return this.data[this.dataLength - 1];
    };
    /**
     * @return {Array} the result data without the checksum
     */
    BLEPacketBuilder.prototype.getData = function () {
        return this.data.slice(0, this.dataLength - 1);
        // let result: number[] = (s => { let a = []; while (s-- > 0) a.push(0); return a; })(this.dataLength - 1);
        // /* arraycopy */((srcPts, srcOff, dstPts, dstOff, size) => { if (srcPts !== dstPts || dstOff >= srcOff + size) { while (--size >= 0) dstPts[dstOff++] = srcPts[srcOff++]; } else { let tmp = srcPts.slice(srcOff, srcOff + size); for (let i = 0; i < size; i++) dstPts[dstOff++] = tmp[i]; } })(this.data, 0, result, 0, result.length);
        // return result;
    };
    /**
     * @return {Array} the buffer (with the checksum)
     */
    BLEPacketBuilder.prototype.getBuffer = function () {
        return this.data;
    };
    return BLEPacketBuilder;
}());
export { BLEPacketBuilder };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmxlLXBhY2tldC1idWlsZGVyLmpzIiwic291cmNlUm9vdCI6Im5nOi8vQGlvdGl6ZS90YXAvcHJvdG9jb2wvYmxlL2NvbW1vbi8iLCJzb3VyY2VzIjpbImxpYi9ibGUtcGFja2V0LWJ1aWxkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ25EOzs7Ozs7R0FNRztBQUNIO0lBT0ksMEJBQW1CLFlBQW9CO1FBQ25DLElBQUksQ0FBQyxZQUFZLEdBQUcsQ0FBQyxDQUFDO1FBQ3RCLElBQUksQ0FBQyxVQUFVLEdBQUcsQ0FBQyxDQUFDO1FBQ3BCLElBQUksQ0FBQyxJQUFJLEdBQUcsSUFBSSxVQUFVLENBQUMsWUFBWSxDQUFDLENBQUM7UUFDekMsSUFBSSxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQ2pCLENBQUM7SUFFRDs7O09BR0c7SUFDSSxpQ0FBTSxHQUFiLFVBQWMsU0FBcUI7UUFDL0IsSUFBSSxDQUFDLFlBQVksR0FBRyxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUMsR0FBRyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUM7UUFDOUMsSUFBSSxJQUFJLENBQUMsWUFBWSxHQUFHLFNBQVMsQ0FBQyxNQUFNLEdBQUcsSUFBSSxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUU7WUFDekQsTUFBTSxJQUFJLEtBQUssQ0FDWCwyQ0FBeUMsSUFBSSxDQUF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