"use strict";
var _PacketBuilder_instances, _PacketBuilder_maxPacketSize, _PacketBuilder_protocolVersion, _PacketBuilder_completedBuffers, _PacketBuilder_finished, _PacketBuilder_currentPacketBuffer, _PacketBuilder_currentPacketFilled, _PacketBuilder_finishBuffer;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PacketBuilder = void 0;
const tslib_1 = require("tslib");
class PacketBuilder {
    constructor(maxPacketSize, protocolVersion) {
        _PacketBuilder_instances.add(this);
        _PacketBuilder_maxPacketSize.set(this, void 0);
        _PacketBuilder_protocolVersion.set(this, void 0);
        _PacketBuilder_completedBuffers.set(this, []);
        _PacketBuilder_finished.set(this, false);
        _PacketBuilder_currentPacketBuffer.set(this, void 0);
        _PacketBuilder_currentPacketFilled.set(this, void 0);
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_maxPacketSize, maxPacketSize, "f");
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_protocolVersion, protocolVersion, "f");
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_currentPacketBuffer, Buffer.alloc(maxPacketSize), "f");
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_currentPacketFilled, 0, "f");
    }
    addCommand(cmd) {
        if (tslib_1.__classPrivateFieldGet(this, _PacketBuilder_finished, "f"))
            throw new Error('Packets have been finished');
        if (typeof cmd.serialize !== 'function') {
            throw new Error(`Command ${cmd.constructor.name} is not serializable`);
        }
        const rawName = cmd.constructor.rawName;
        const payload = cmd.serialize(tslib_1.__classPrivateFieldGet(this, _PacketBuilder_protocolVersion, "f"));
        const totalLength = payload.length + 8;
        if (totalLength > tslib_1.__classPrivateFieldGet(this, _PacketBuilder_maxPacketSize, "f")) {
            // Command is too large for a normal packet, try sending it on its own anyway
            tslib_1.__classPrivateFieldGet(this, _PacketBuilder_instances, "m", _PacketBuilder_finishBuffer).call(this, totalLength);
        }
        // Ensure the packet will fit into the current buffer
        if (totalLength + tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") > tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketBuffer, "f").length) {
            tslib_1.__classPrivateFieldGet(this, _PacketBuilder_instances, "m", _PacketBuilder_finishBuffer).call(this);
        }
        // Add to packet
        tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketBuffer, "f").writeUInt16BE(payload.length + 8, tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") + 0);
        tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketBuffer, "f").write(rawName, tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") + 4, 4);
        payload.copy(tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketBuffer, "f"), tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") + 8);
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_currentPacketFilled, tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") + totalLength, "f");
    }
    getPackets() {
        tslib_1.__classPrivateFieldGet(this, _PacketBuilder_instances, "m", _PacketBuilder_finishBuffer).call(this, 0);
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_finished, true, "f");
        return tslib_1.__classPrivateFieldGet(this, _PacketBuilder_completedBuffers, "f");
    }
}
exports.PacketBuilder = PacketBuilder;
_PacketBuilder_maxPacketSize = new WeakMap(), _PacketBuilder_protocolVersion = new WeakMap(), _PacketBuilder_completedBuffers = new WeakMap(), _PacketBuilder_finished = new WeakMap(), _PacketBuilder_currentPacketBuffer = new WeakMap(), _PacketBuilder_currentPacketFilled = new WeakMap(), _PacketBuilder_instances = new WeakSet(), _PacketBuilder_finishBuffer = function _PacketBuilder_finishBuffer(newBufferLength = tslib_1.__classPrivateFieldGet(this, _PacketBuilder_maxPacketSize, "f")) {
    if (tslib_1.__classPrivateFieldGet(this, _PacketBuilder_finished, "f"))
        return;
    if (tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f") > 0) {
        tslib_1.__classPrivateFieldGet(this, _PacketBuilder_completedBuffers, "f").push(tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketBuffer, "f").subarray(0, tslib_1.__classPrivateFieldGet(this, _PacketBuilder_currentPacketFilled, "f")));
    }
    if (newBufferLength > 0) {
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_currentPacketBuffer, Buffer.alloc(newBufferLength), "f");
        tslib_1.__classPrivateFieldSet(this, _PacketBuilder_currentPacketFilled, 0, "f");
    }
};
//# sourceMappingURL=packetBuilder.js.map