"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.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR = void 0;
exports.getHandleReceiveMessageDiscriminatorBytes = getHandleReceiveMessageDiscriminatorBytes;
exports.getHandleReceiveMessageInstructionDataEncoder = getHandleReceiveMessageInstructionDataEncoder;
exports.getHandleReceiveMessageInstructionDataDecoder = getHandleReceiveMessageInstructionDataDecoder;
exports.getHandleReceiveMessageInstructionDataCodec = getHandleReceiveMessageInstructionDataCodec;
exports.getHandleReceiveMessageInstructionAsync = getHandleReceiveMessageInstructionAsync;
exports.getHandleReceiveMessageInstruction = getHandleReceiveMessageInstruction;
exports.parseHandleReceiveMessageInstruction = parseHandleReceiveMessageInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR = new Uint8Array([
    133, 102, 1, 180, 145, 11, 138, 180,
]);
function getHandleReceiveMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR);
}
function getHandleReceiveMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['remoteDomain', (0, kit_1.getU32Encoder)()],
        ['sender', (0, kit_1.getAddressEncoder)()],
        ['messageBody', (0, kit_1.addEncoderSizePrefix)((0, kit_1.getBytesEncoder)(), (0, kit_1.getU32Encoder)())],
        ['authorityBump', (0, kit_1.getU8Encoder)()],
    ]), (value) => ({
        ...value,
        discriminator: exports.HANDLE_RECEIVE_MESSAGE_DISCRIMINATOR,
    }));
}
function getHandleReceiveMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['remoteDomain', (0, kit_1.getU32Decoder)()],
        ['sender', (0, kit_1.getAddressDecoder)()],
        ['messageBody', (0, kit_1.addDecoderSizePrefix)((0, kit_1.getBytesDecoder)(), (0, kit_1.getU32Decoder)())],
        ['authorityBump', (0, kit_1.getU8Decoder)()],
    ]);
}
function getHandleReceiveMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getHandleReceiveMessageInstructionDataEncoder(), getHandleReceiveMessageInstructionDataDecoder());
}
async function getHandleReceiveMessageInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        state: { value: input.state ?? null, isWritable: false },
        selfAuthority: { value: input.selfAuthority ?? 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.getBytesEncoder)().encode(new Uint8Array([
                    183, 102, 64, 134, 222, 55, 238, 112, 130, 28, 16, 68, 91, 22, 47,
                    44, 126, 200, 121, 91, 208, 128, 12, 20, 98, 148, 158, 35, 40, 209,
                    221, 90,
                ])),
            ],
        });
    }
    if (!accounts.selfAuthority.value) {
        accounts.selfAuthority.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([
                    115, 101, 108, 102, 95, 97, 117, 116, 104, 111, 114, 105, 116, 121,
                ])),
            ],
        });
    }
    if (!accounts.program.value) {
        accounts.program.value =
            'DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.selfAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getHandleReceiveMessageInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        state: { value: input.state ?? null, isWritable: false },
        selfAuthority: { value: input.selfAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.program.value) {
        accounts.program.value =
            'DLv3NggMiSaef97YCkew5xKUHDh13tVGZ7tydt3ZeAru';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.selfAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getHandleReceiveMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseHandleReceiveMessageInstruction(instruction) {
    if (instruction.accounts.length < 4) {
        // 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: {
            authorityPda: getNextAccount(),
            state: getNextAccount(),
            selfAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getHandleReceiveMessageInstructionDataDecoder().decode(instruction.data),
    };
}
