"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cRequestReceiptForTxRespVersion = void 0;
exports.serializeRequestReceiptForTxResp = serializeRequestReceiptForTxResp;
exports.deserializeRequestReceiptForTxResp = deserializeRequestReceiptForTxResp;
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const SignedReceipt_1 = require("./SignedReceipt");
exports.cRequestReceiptForTxRespVersion = 1;
function serializeRequestReceiptForTxResp(stream, inp, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRequestReceiptForTxResp);
    }
    stream.writeUInt8(exports.cRequestReceiptForTxRespVersion);
    if (inp.receipt === null) {
        stream.writeUInt8(0);
    }
    else {
        stream.writeUInt8(1);
        (0, SignedReceipt_1.serializeSignedReceipt)(stream, inp.receipt);
    }
    stream.writeString(inp.note);
    stream.writeUInt8(inp.success ? 1 : 0);
}
function deserializeRequestReceiptForTxResp(stream) {
    const version = stream.readUInt8();
    if (version !== exports.cRequestReceiptForTxRespVersion) {
        throw new Error('RequestReceiptForTxResp version mismatch');
    }
    if (stream.readUInt8() === 0) {
        const note = stream.readString();
        const success = stream.readUInt8() === 1;
        return { receipt: null, note, success };
    }
    const receipt = (0, SignedReceipt_1.deserializeSignedReceipt)(stream);
    const note = stream.readString();
    const success = stream.readUInt8() === 1;
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.RequestReceiptForTxResp, { receipt, note, success });
    if (errors && errors.length > 0) {
        throw new Error(`AJV: validation error -> ${errors.join(', ')}`);
    }
    return { receipt, note, success };
}
//# sourceMappingURL=RequestReceiptForTxResp.js.map