"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cBroadcastStateReqVersion = void 0;
exports.serializeBroadcastStateReq = serializeBroadcastStateReq;
exports.deserializeBroadcastStateReq = deserializeBroadcastStateReq;
const WrappedDataResponse_1 = require("./WrappedDataResponse");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cBroadcastStateReqVersion = 1;
function serializeBroadcastStateReq(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cBroadcastStateReq);
    }
    stream.writeUInt8(exports.cBroadcastStateReqVersion);
    stream.writeString(obj.txid);
    stream.writeUInt16(obj.stateList.length); // Serialize array length
    obj.stateList.forEach((item) => (0, WrappedDataResponse_1.serializeWrappedDataResponse)(stream, item)); // Serialize each item
}
function deserializeBroadcastStateReq(stream) {
    const version = stream.readUInt8();
    if (version > exports.cBroadcastStateReqVersion) {
        throw new Error('BroadcastStateReq version mismatch');
    }
    const txid = stream.readString();
    const stateListLength = stream.readUInt16();
    const stateList = Array.from({ length: stateListLength }, () => (0, WrappedDataResponse_1.deserializeWrappedDataResponse)(stream));
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.BroadcastStateReq, { txid, stateList });
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return { txid, stateList };
}
//# sourceMappingURL=BroadcastStateReq.js.map