"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRequestTxAndStateResp = exports.serializeRequestTxAndStateResp = exports.cRequestTxAndStateRespVersion = void 0;
const lib_types_1 = require("@shardeum-foundation/lib-types");
const WrappedDataResponse_1 = require("./WrappedDataResponse");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Context_1 = require("../p2p/Context");
const AppObjEnum_1 = require("./enum/AppObjEnum");
exports.cRequestTxAndStateRespVersion = 1;
function serializeRequestTxAndStateResp(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRequestTxAndStateResp);
    }
    stream.writeUInt8(exports.cRequestTxAndStateRespVersion);
    stream.writeUInt16(obj.stateList.length);
    obj.stateList.forEach((state) => (0, WrappedDataResponse_1.serializeWrappedDataResponse)(stream, state));
    const beforeHashKeys = Object.keys(obj.account_state_hash_before);
    stream.writeUInt16(beforeHashKeys.length);
    beforeHashKeys.forEach((key) => {
        stream.writeString(key);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(obj.account_state_hash_before[key]);
    });
    const afterHashKeys = Object.keys(obj.account_state_hash_after);
    stream.writeUInt16(afterHashKeys.length);
    afterHashKeys.forEach((key) => {
        stream.writeString(key);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(obj.account_state_hash_after[key]);
    });
    stream.writeString(obj.note);
    stream.writeUInt8(obj.success ? 1 : 0);
    stream.writeUInt8(obj.acceptedTX !== undefined ? 1 : 0);
    if (obj.acceptedTX !== undefined) {
        stream.writeString(lib_types_1.Utils.safeStringify(obj.acceptedTX));
    }
    stream.writeUInt8(obj.originalData !== undefined ? 1 : 0);
    if (obj.originalData !== undefined) {
        stream.writeString(lib_types_1.Utils.safeStringify(obj.originalData));
    }
    if (obj.appReceiptData !== undefined) {
        stream.writeBuffer(Context_1.stateManager.app.binarySerializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, obj.appReceiptData));
    }
}
exports.serializeRequestTxAndStateResp = serializeRequestTxAndStateResp;
function deserializeRequestTxAndStateResp(stream) {
    const version = stream.readUInt8();
    if (version > exports.cRequestTxAndStateRespVersion) {
        throw new Error('cRequestTxAndStateRespVersion version mismatch');
    }
    const result = {
        stateList: [],
        account_state_hash_before: {},
        account_state_hash_after: {},
        note: '',
        success: false,
    };
    const stateListLength = stream.readUInt16();
    for (let i = 0; i < stateListLength; i++) {
        result.stateList.push((0, WrappedDataResponse_1.deserializeWrappedDataResponse)(stream));
    }
    const beforeHashLength = stream.readUInt16();
    for (let i = 0; i < beforeHashLength; i++) {
        const key = stream.readString();
        const value = stream.readString();
        // eslint-disable-next-line security/detect-object-injection
        result.account_state_hash_before[key] = value;
    }
    const afterHashLength = stream.readUInt16();
    for (let i = 0; i < afterHashLength; i++) {
        const key = stream.readString();
        const value = stream.readString();
        // eslint-disable-next-line security/detect-object-injection
        result.account_state_hash_after[key] = value;
    }
    result.note = stream.readString();
    result.success = stream.readUInt8() === 1;
    if (stream.readUInt8() === 1) {
        // Check if acceptedTX is present
        result.acceptedTX = lib_types_1.Utils.safeJsonParse(stream.readString());
    }
    if (stream.readUInt8() === 1) {
        // Check if originalData is present
        result.originalData = lib_types_1.Utils.safeJsonParse(stream.readString());
    }
    if (stream.readUInt8() === 1) {
        // Check if appReceiptData is present
        result.appReceiptData = Context_1.stateManager.app.binaryDeserializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, stream.readBuffer());
    }
    return result;
}
exports.deserializeRequestTxAndStateResp = deserializeRequestTxAndStateResp;
//# sourceMappingURL=RequestTxAndStateResp.js.map