"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RECEIVE_MESSAGE_DISCRIMINATOR = void 0;
exports.getReceiveMessageDiscriminatorBytes = getReceiveMessageDiscriminatorBytes;
exports.getReceiveMessageInstructionDataEncoder = getReceiveMessageInstructionDataEncoder;
exports.getReceiveMessageInstructionDataDecoder = getReceiveMessageInstructionDataDecoder;
exports.getReceiveMessageInstructionDataCodec = getReceiveMessageInstructionDataCodec;
exports.getReceiveMessageInstruction = getReceiveMessageInstruction;
exports.parseReceiveMessageInstruction = parseReceiveMessageInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.RECEIVE_MESSAGE_DISCRIMINATOR = new Uint8Array([
    38, 144, 127, 225, 31, 225, 238, 25,
]);
function getReceiveMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.RECEIVE_MESSAGE_DISCRIMINATOR);
}
function getReceiveMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["message", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
        ["attestation", (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.RECEIVE_MESSAGE_DISCRIMINATOR })); });
}
function getReceiveMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["message", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
        ["attestation", (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
    ]);
}
function getReceiveMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getReceiveMessageInstructionDataEncoder(), getReceiveMessageInstructionDataDecoder());
}
function getReceiveMessageInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.MESSAGE_TRANSMITTER_PROGRAM_ADDRESS;
    var originalAccounts = {
        payer: { value: (_b = input.payer) !== null && _b !== void 0 ? _b : null, isWritable: true },
        caller: { value: (_c = input.caller) !== null && _c !== void 0 ? _c : null, isWritable: false },
        authorityPda: { value: (_d = input.authorityPda) !== null && _d !== void 0 ? _d : null, isWritable: false },
        messageTransmitter: {
            value: (_e = input.messageTransmitter) !== null && _e !== void 0 ? _e : null,
            isWritable: false,
        },
        usedNonces: { value: (_f = input.usedNonces) !== null && _f !== void 0 ? _f : null, isWritable: true },
        receiver: { value: (_g = input.receiver) !== null && _g !== void 0 ? _g : null, isWritable: false },
        systemProgram: { value: (_h = input.systemProgram) !== null && _h !== void 0 ? _h : null, isWritable: false },
        eventAuthority: { value: (_j = input.eventAuthority) !== null && _j !== void 0 ? _j : null, isWritable: false },
        program: { value: (_k = input.program) !== null && _k !== void 0 ? _k : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.caller),
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.usedNonces),
            getAccountMeta(accounts.receiver),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getReceiveMessageInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 9) {
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            payer: getNextAccount(),
            caller: getNextAccount(),
            authorityPda: getNextAccount(),
            messageTransmitter: getNextAccount(),
            usedNonces: getNextAccount(),
            receiver: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=receiveMessage.js.map