"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeBroadcastFinalStateReq = exports.serializeBroadcastFinalStateReq = exports.cBroadcastFinalStateReqVersion = void 0;
const WrappedDataResponse_1 = require("./WrappedDataResponse");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
exports.cBroadcastFinalStateReqVersion = 1;
function serializeBroadcastFinalStateReq(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cBroadcastFinalStateReq);
    }
    stream.writeUInt8(exports.cBroadcastFinalStateReqVersion);
    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
}
exports.serializeBroadcastFinalStateReq = serializeBroadcastFinalStateReq;
function deserializeBroadcastFinalStateReq(stream) {
    const version = stream.readUInt8();
    if (version > exports.cBroadcastFinalStateReqVersion) {
        throw new Error('BroadcastFinalStateReq 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 };
}
exports.deserializeBroadcastFinalStateReq = deserializeBroadcastFinalStateReq;
//# sourceMappingURL=BroadcastFinalStateReq.js.map