"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cSignedReceiptVersion = void 0;
exports.serializeSignedReceipt = serializeSignedReceipt;
exports.deserializeSignedReceipt = deserializeSignedReceipt;
const Proposal_1 = require("./Proposal");
const Sign_1 = require("./Sign");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cSignedReceiptVersion = 1;
function serializeSignedReceipt(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cSignedReceipt);
    }
    stream.writeUInt8(exports.cSignedReceiptVersion);
    (0, Proposal_1.serializeProposal)(stream, obj.proposal);
    stream.writeString(obj.proposalHash);
    stream.writeUInt16(obj.voteOffsets.length);
    for (let i = 0; i < obj.voteOffsets.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        stream.writeUInt16(obj.voteOffsets[i]);
    }
    stream.writeUInt16(obj.signaturePack.length);
    for (let i = 0; i < obj.signaturePack.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        (0, Sign_1.serializeSign)(stream, obj.signaturePack[i]);
    }
    if (obj.sign) {
        stream.writeUInt8(1);
        (0, Sign_1.serializeSign)(stream, obj.sign);
    }
    else {
        stream.writeUInt8(0);
    }
}
function deserializeSignedReceipt(stream) {
    const version = stream.readUInt8();
    if (version > exports.cSignedReceiptVersion) {
        throw new Error(`SignedReceiptDeserializer expected version ${exports.cSignedReceiptVersion}, got ${version}`);
    }
    const proposal = (0, Proposal_1.deserializeProposal)(stream);
    const proposalHash = stream.readString();
    const voteOffsetsLength = stream.readUInt16();
    const voteOffsets = [];
    for (let i = 0; i < voteOffsetsLength; i++) {
        voteOffsets.push(stream.readUInt16());
    }
    const signaturesLength = stream.readUInt16();
    const signatures = [];
    for (let i = 0; i < signaturesLength; i++) {
        signatures.push((0, Sign_1.deserializeSign)(stream));
    }
    const hasSign = stream.readUInt8();
    if (hasSign) {
        return {
            proposal,
            proposalHash,
            voteOffsets,
            signaturePack: signatures,
            sign: (0, Sign_1.deserializeSign)(stream),
        };
    }
    else {
        return {
            proposal,
            proposalHash,
            voteOffsets,
            signaturePack: signatures,
        };
    }
}
//# sourceMappingURL=SignedReceipt.js.map