"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.RECEIVE_MESSAGE_DISCRIMINATOR = void 0;
exports.getReceiveMessageDiscriminatorBytes = getReceiveMessageDiscriminatorBytes;
exports.getReceiveMessageInstructionDataEncoder = getReceiveMessageInstructionDataEncoder;
exports.getReceiveMessageInstructionDataDecoder = getReceiveMessageInstructionDataDecoder;
exports.getReceiveMessageInstructionDataCodec = getReceiveMessageInstructionDataCodec;
exports.getReceiveMessageInstructionAsync = getReceiveMessageInstructionAsync;
exports.getReceiveMessageInstruction = getReceiveMessageInstruction;
exports.parseReceiveMessageInstruction = parseReceiveMessageInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const 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)())],
    ]), (value) => ({ ...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());
}
async function getReceiveMessageInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        payer: { value: input.payer ?? null, isWritable: true },
        caller: { value: input.caller ?? null, isWritable: false },
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        messageTransmitter: {
            value: input.messageTransmitter ?? null,
            isWritable: false,
        },
        usedNonce: { value: input.usedNonce ?? null, isWritable: true },
        receiver: { value: input.receiver ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.authorityPda.value) {
        accounts.authorityPda.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    109, 101, 115, 115, 97, 103, 101, 95, 116, 114, 97, 110, 115, 109,
                    105, 116, 116, 101, 114, 95, 97, 117, 116, 104, 111, 114, 105, 116,
                    121,
                ])),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.receiver.value)),
            ],
        });
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    if (!accounts.eventAuthority.value) {
        accounts.eventAuthority.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    95, 95, 101, 118, 101, 110, 116, 95, 97, 117, 116, 104, 111, 114,
                    105, 116, 121,
                ])),
            ],
        });
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.caller),
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.usedNonce),
            getAccountMeta(accounts.receiver),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getReceiveMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getReceiveMessageInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.MESSAGE_TRANSMITTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        payer: { value: input.payer ?? null, isWritable: true },
        caller: { value: input.caller ?? null, isWritable: false },
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        messageTransmitter: {
            value: input.messageTransmitter ?? null,
            isWritable: false,
        },
        usedNonce: { value: input.usedNonce ?? null, isWritable: true },
        receiver: { value: input.receiver ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? 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.payer),
            getAccountMeta(accounts.caller),
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.messageTransmitter),
            getAccountMeta(accounts.usedNonce),
            getAccountMeta(accounts.receiver),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getReceiveMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 9) {
        // 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: {
            payer: getNextAccount(),
            caller: getNextAccount(),
            authorityPda: getNextAccount(),
            messageTransmitter: getNextAccount(),
            usedNonce: getNextAccount(),
            receiver: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
