"use strict";
/**
 * This code was AUTOGENERATED using the codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SEND_MESSAGE_DISCRIMINATOR = void 0;
exports.getSendMessageDiscriminatorBytes = getSendMessageDiscriminatorBytes;
exports.getSendMessageInstructionDataEncoder = getSendMessageInstructionDataEncoder;
exports.getSendMessageInstructionDataDecoder = getSendMessageInstructionDataDecoder;
exports.getSendMessageInstructionDataCodec = getSendMessageInstructionDataCodec;
exports.getSendMessageInstruction = getSendMessageInstruction;
exports.parseSendMessageInstruction = parseSendMessageInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.SEND_MESSAGE_DISCRIMINATOR = new Uint8Array([
    57, 40, 34, 178, 189, 10, 65, 26,
]);
function getSendMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.SEND_MESSAGE_DISCRIMINATOR);
}
function getSendMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['destinationDomain', (0, kit_1.getU32Encoder)()],
        ['recipient', (0, kit_1.getAddressEncoder)()],
        ['destinationCaller', (0, kit_1.getAddressEncoder)()],
        ['minFinalityThreshold', (0, kit_1.getU32Encoder)()],
        ['messageBody', (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
    ]), (value) => ({ ...value, discriminator: exports.SEND_MESSAGE_DISCRIMINATOR }));
}
function getSendMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['destinationDomain', (0, kit_1.getU32Decoder)()],
        ['recipient', (0, kit_1.getAddressDecoder)()],
        ['destinationCaller', (0, kit_1.getAddressDecoder)()],
        ['minFinalityThreshold', (0, kit_1.getU32Decoder)()],
        ['messageBody', (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
    ]);
}
function getSendMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getSendMessageInstructionDataEncoder(), getSendMessageInstructionDataDecoder());
}
function getSendMessageInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        eventRentPayer: { value: input.eventRentPayer ?? null, isWritable: true },
        senderAuthorityPda: {
            value: input.senderAuthorityPda ?? null,
            isWritable: false,
        },
        messageTransmitter: {
            value: input.messageTransmitter ?? null,
            isWritable: true,
        },
        messageSentEventData: {
            value: input.messageSentEventData ?? null,
            isWritable: true,
        },
        senderProgram: { value: input.senderProgram ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.eventRentPayer),
            getAccountMeta(accounts.senderAuthorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.messageSentEventData),
            getAccountMeta(accounts.senderProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getSendMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseSendMessageInstruction(instruction) {
    if (instruction.accounts.length < 6) {
        // TODO: Coded error.
        throw new Error('Not enough accounts');
    }
    let accountIndex = 0;
    const getNextAccount = () => {
        const accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            eventRentPayer: getNextAccount(),
            senderAuthorityPda: getNextAccount(),
            messageTransmitter: getNextAccount(),
            messageSentEventData: getNextAccount(),
            senderProgram: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getSendMessageInstructionDataDecoder().decode(instruction.data),
    };
}
