"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRequestStateForTxPostResp = exports.serializeRequestStateForTxPostResp = exports.cRequestStateForTxPostRespVersion = void 0;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const WrappedDataResponse_1 = require("./WrappedDataResponse");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
exports.cRequestStateForTxPostRespVersion = 1;
function serializeRequestStateForTxPostResp(stream, resp, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRequestStateForTxPostResp);
    }
    stream.writeUInt8(exports.cRequestStateForTxPostRespVersion);
    stream.writeUInt8(resp.success ? 1 : 0);
    stream.writeString(resp.note);
    stream.writeUInt16(Object.keys(resp.beforeHashes).length);
    Object.entries(resp.beforeHashes).forEach(([key, value]) => {
        stream.writeString(key);
        stream.writeString(value);
    }); // Serialize each item
    stream.writeUInt16(resp.stateList.length);
    resp.stateList.forEach((item) => (0, WrappedDataResponse_1.serializeWrappedDataResponse)(stream, item)); // Serialize each item
}
exports.serializeRequestStateForTxPostResp = serializeRequestStateForTxPostResp;
function deserializeRequestStateForTxPostResp(stream) {
    const version = stream.readUInt8();
    if (version > exports.cRequestStateForTxPostRespVersion) {
        throw new Error('RequestStateForTxPostResp version mismatch');
    }
    const success = stream.readUInt8() === 1;
    const note = stream.readString();
    const beforeHashesLength = stream.readUInt16();
    const beforeHashes = {};
    for (let i = 0; i < beforeHashesLength; i++) {
        const key = stream.readString();
        const value = stream.readString();
        // eslint-disable-next-line security/detect-object-injection
        beforeHashes[key] = value;
    }
    const stateListLength = stream.readUInt16();
    const stateList = Array.from({ length: stateListLength }, () => (0, WrappedDataResponse_1.deserializeWrappedDataResponse)(stream));
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.RequestStateForTxPostResp, {
        success,
        note,
        beforeHashes,
        stateList,
    });
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return { success, note, beforeHashes, stateList };
}
exports.deserializeRequestStateForTxPostResp = deserializeRequestStateForTxPostResp;
//# sourceMappingURL=RequestStateForTxPostResp.js.map