"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRepairOOSAccountsReq = exports.serializeRepairOOSAccountsReq = exports.cRepairOOSAccountsReqVersion = void 0;
const SignedReceipt_1 = require("./SignedReceipt");
const WrappedData_1 = require("./WrappedData");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cRepairOOSAccountsReqVersion = 1;
//TODO: add file in /ajv folder
// Two enums
const serializeRepairOOSAccountsReq = (stream, inp, root = false) => {
    // const errors = verifyPayload(AJVSchemaEnum.RepairOOSAccountsReq', inp)
    // if (errors && errors.length > 0) {
    //   throw new Error('Data validation error')
    // }
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRepairOOSAccountsReq);
    }
    stream.writeUInt8(exports.cRepairOOSAccountsReqVersion);
    stream.writeUInt32(inp.repairInstructions.length || 0);
    for (let i = 0; i < inp.repairInstructions.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.repairInstructions[i].accountID);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.repairInstructions[i].hash);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.repairInstructions[i].txId);
        // eslint-disable-next-line security/detect-object-injection
        (0, WrappedData_1.serializeWrappedData)(stream, inp.repairInstructions[i].accountData);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.repairInstructions[i].targetNodeId);
        // eslint-disable-next-line security/detect-object-injection
        (0, SignedReceipt_1.serializeSignedReceipt)(stream, inp.repairInstructions[i].signedReceipt);
    }
};
exports.serializeRepairOOSAccountsReq = serializeRepairOOSAccountsReq;
const deserializeRepairOOSAccountsReq = (stream) => {
    const version = stream.readUInt8();
    if (version !== exports.cRepairOOSAccountsReqVersion) {
        throw new Error(`RepairOOSAccountsReqDeserializer expected version ${exports.cRepairOOSAccountsReqVersion}, got ${version}`);
    }
    const repairInstructionsLength = stream.readUInt32();
    const result = {
        repairInstructions: [],
    };
    for (let i = 0; i < repairInstructionsLength; i++) {
        result.repairInstructions.push({
            accountID: stream.readString(),
            hash: stream.readString(),
            txId: stream.readString(),
            accountData: (0, WrappedData_1.deserializeWrappedData)(stream),
            targetNodeId: stream.readString(),
            signedReceipt: (0, SignedReceipt_1.deserializeSignedReceipt)(stream),
        });
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.RepairOOSAccountsReq, result);
    if (errors && errors.length > 0) {
        throw new Error('AJV: Data validation error ->' + errors.join(', '));
    }
    return result;
};
exports.deserializeRepairOOSAccountsReq = deserializeRepairOOSAccountsReq;
//# sourceMappingURL=RepairOOSAccountsReq.js.map