"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_UNFINALIZED_MESSAGE_DISCRIMINATOR = void 0;
exports.getHandleReceiveUnfinalizedMessageDiscriminatorBytes = getHandleReceiveUnfinalizedMessageDiscriminatorBytes;
exports.getHandleReceiveUnfinalizedMessageInstructionDataEncoder = getHandleReceiveUnfinalizedMessageInstructionDataEncoder;
exports.getHandleReceiveUnfinalizedMessageInstructionDataDecoder = getHandleReceiveUnfinalizedMessageInstructionDataDecoder;
exports.getHandleReceiveUnfinalizedMessageInstructionDataCodec = getHandleReceiveUnfinalizedMessageInstructionDataCodec;
exports.getHandleReceiveUnfinalizedMessageInstructionAsync = getHandleReceiveUnfinalizedMessageInstructionAsync;
exports.getHandleReceiveUnfinalizedMessageInstruction = getHandleReceiveUnfinalizedMessageInstruction;
exports.parseHandleReceiveUnfinalizedMessageInstruction = parseHandleReceiveUnfinalizedMessageInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
const types_1 = require("../types");
exports.HANDLE_RECEIVE_UNFINALIZED_MESSAGE_DISCRIMINATOR = new Uint8Array([
    200, 169, 175, 20, 200, 58, 182, 61,
]);
function getHandleReceiveUnfinalizedMessageDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.HANDLE_RECEIVE_UNFINALIZED_MESSAGE_DISCRIMINATOR);
}
function getHandleReceiveUnfinalizedMessageInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['params', (0, types_1.getHandleReceiveMessageParamsEncoder)()],
    ]), (value) => ({
        ...value,
        discriminator: exports.HANDLE_RECEIVE_UNFINALIZED_MESSAGE_DISCRIMINATOR,
    }));
}
function getHandleReceiveUnfinalizedMessageInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['params', (0, types_1.getHandleReceiveMessageParamsDecoder)()],
    ]);
}
function getHandleReceiveUnfinalizedMessageInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getHandleReceiveUnfinalizedMessageInstructionDataEncoder(), getHandleReceiveUnfinalizedMessageInstructionDataDecoder());
}
async function getHandleReceiveUnfinalizedMessageInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
        remoteTokenMessenger: {
            value: input.remoteTokenMessenger ?? null,
            isWritable: false,
        },
        tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
        localToken: { value: input.localToken ?? null, isWritable: true },
        tokenPair: { value: input.tokenPair ?? null, isWritable: false },
        feeRecipientTokenAccount: {
            value: input.feeRecipientTokenAccount ?? null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: input.recipientTokenAccount ?? null,
            isWritable: true,
        },
        custodyTokenAccount: {
            value: input.custodyTokenAccount ?? null,
            isWritable: true,
        },
        tokenProgram: { value: input.tokenProgram ?? 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: 'CCTPV2Sm4AdWt5296sk4P66VBZ7bEhcARwFaaS9YPbeC',
            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([
                    166, 95, 200, 29, 15, 239, 168, 134, 12, 179, 184, 63, 8, 155, 2,
                    36, 190, 138, 102, 135, 183, 174, 73, 245, 148, 192, 185, 180, 215,
                    233, 56, 147,
                ])),
            ],
        });
    }
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    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.authorityPda),
            getAccountMeta(accounts.tokenMessenger),
            getAccountMeta(accounts.remoteTokenMessenger),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.tokenPair),
            getAccountMeta(accounts.feeRecipientTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.custodyTokenAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getHandleReceiveUnfinalizedMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getHandleReceiveUnfinalizedMessageInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.TOKEN_MESSENGER_MINTER_V2_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        authorityPda: { value: input.authorityPda ?? null, isWritable: false },
        tokenMessenger: { value: input.tokenMessenger ?? null, isWritable: false },
        remoteTokenMessenger: {
            value: input.remoteTokenMessenger ?? null,
            isWritable: false,
        },
        tokenMinter: { value: input.tokenMinter ?? null, isWritable: false },
        localToken: { value: input.localToken ?? null, isWritable: true },
        tokenPair: { value: input.tokenPair ?? null, isWritable: false },
        feeRecipientTokenAccount: {
            value: input.feeRecipientTokenAccount ?? null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: input.recipientTokenAccount ?? null,
            isWritable: true,
        },
        custodyTokenAccount: {
            value: input.custodyTokenAccount ?? null,
            isWritable: true,
        },
        tokenProgram: { value: input.tokenProgram ?? 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.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.authorityPda),
            getAccountMeta(accounts.tokenMessenger),
            getAccountMeta(accounts.remoteTokenMessenger),
            getAccountMeta(accounts.tokenMinter),
            getAccountMeta(accounts.localToken),
            getAccountMeta(accounts.tokenPair),
            getAccountMeta(accounts.feeRecipientTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.custodyTokenAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getHandleReceiveUnfinalizedMessageInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseHandleReceiveUnfinalizedMessageInstruction(instruction) {
    if (instruction.accounts.length < 12) {
        // 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(),
            tokenMessenger: getNextAccount(),
            remoteTokenMessenger: getNextAccount(),
            tokenMinter: getNextAccount(),
            localToken: getNextAccount(),
            tokenPair: getNextAccount(),
            feeRecipientTokenAccount: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            custodyTokenAccount: getNextAccount(),
            tokenProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getHandleReceiveUnfinalizedMessageInstructionDataDecoder().decode(instruction.data),
    };
}
