"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRequestStateForTxResp = exports.serializeRequestStateForTxResp = exports.cRequestStateForTxRespVersion = void 0;
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const WrappedData_1 = require("./WrappedData");
exports.cRequestStateForTxRespVersion = 1;
const serializeRequestStateForTxResp = (stream, inp, bool = false) => {
    if (bool) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRequestStateForTxResp);
    }
    stream.writeUInt8(exports.cRequestStateForTxRespVersion);
    stream.writeUInt16(inp.stateList.length);
    for (let i = 0; i < inp.stateList.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        (0, WrappedData_1.serializeWrappedData)(stream, inp.stateList[i]);
    }
    stream.writeUInt16(Object.keys(inp.beforeHashes).length);
    for (const key in inp.beforeHashes) {
        stream.writeString(key);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.beforeHashes[key]);
    }
    stream.writeString(inp.note);
    stream.writeUInt8(inp.success ? 1 : 0);
};
exports.serializeRequestStateForTxResp = serializeRequestStateForTxResp;
const deserializeRequestStateForTxResp = (stream) => {
    const ver = stream.readUInt8();
    if (ver !== exports.cRequestStateForTxRespVersion) {
        throw new Error('Unsupported version');
    }
    const stateListLen = stream.readUInt16();
    const ret = {
        stateList: new Array(stateListLen),
        beforeHashes: {},
        note: '',
        success: false,
    };
    for (let i = 0; i < stateListLen; i++) {
        // eslint-disable-next-line security/detect-object-injection
        ret.stateList[i] = (0, WrappedData_1.deserializeWrappedData)(stream);
    }
    const beforeHashesLen = stream.readUInt16();
    for (let i = 0; i < beforeHashesLen; i++) {
        const key = stream.readString();
        const value = stream.readString();
        // eslint-disable-next-line security/detect-object-injection
        ret.beforeHashes[key] = value;
    }
    ret.note = stream.readString();
    ret.success = stream.readUInt8() !== 0;
    console.log('ret', ret);
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.RequestStateForTxResp, ret);
    if (errors && errors.length > 0) {
        throw new Error(`AJV: validation error -> ${errors.join(', ')}`);
    }
    return ret;
};
exports.deserializeRequestStateForTxResp = deserializeRequestStateForTxResp;
//# sourceMappingURL=RequestStateForTxResp.js.map