"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.WITHDRAW_RENT_FUND_DISCRIMINATOR = void 0;
exports.getWithdrawRentFundDiscriminatorBytes = getWithdrawRentFundDiscriminatorBytes;
exports.getWithdrawRentFundInstructionDataEncoder = getWithdrawRentFundInstructionDataEncoder;
exports.getWithdrawRentFundInstructionDataDecoder = getWithdrawRentFundInstructionDataDecoder;
exports.getWithdrawRentFundInstructionDataCodec = getWithdrawRentFundInstructionDataCodec;
exports.getWithdrawRentFundInstructionAsync = getWithdrawRentFundInstructionAsync;
exports.getWithdrawRentFundInstruction = getWithdrawRentFundInstruction;
exports.parseWithdrawRentFundInstruction = parseWithdrawRentFundInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.WITHDRAW_RENT_FUND_DISCRIMINATOR = new Uint8Array([
    153, 28, 108, 116, 132, 70, 161, 125,
]);
function getWithdrawRentFundDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.WITHDRAW_RENT_FUND_DISCRIMINATOR);
}
function getWithdrawRentFundInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['amount', (0, kit_1.getU64Encoder)()],
    ]), (value) => ({ ...value, discriminator: exports.WITHDRAW_RENT_FUND_DISCRIMINATOR }));
}
function getWithdrawRentFundInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['amount', (0, kit_1.getU64Decoder)()],
    ]);
}
function getWithdrawRentFundInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getWithdrawRentFundInstructionDataEncoder(), getWithdrawRentFundInstructionDataDecoder());
}
async function getWithdrawRentFundInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        recipient: { value: input.recipient ?? null, isWritable: true },
        programData: { value: input.programData ?? null, isWritable: false },
        thisProgram: { value: input.thisProgram ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.rentFund.value) {
        accounts.rentFund.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([114, 101, 110, 116, 95, 102, 117, 110, 100])),
            ],
        });
    }
    if (!accounts.thisProgram.value) {
        accounts.thisProgram.value =
            'CPr4bRvkVKcSCLyrQpkZrRrwGzQeVAXutFU8WupuBLXq';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.rentFund),
            getAccountMeta(accounts.recipient),
            getAccountMeta(accounts.programData),
            getAccountMeta(accounts.thisProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getWithdrawRentFundInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getWithdrawRentFundInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        recipient: { value: input.recipient ?? null, isWritable: true },
        programData: { value: input.programData ?? null, isWritable: false },
        thisProgram: { value: input.thisProgram ?? null, isWritable: false },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.thisProgram.value) {
        accounts.thisProgram.value =
            'CPr4bRvkVKcSCLyrQpkZrRrwGzQeVAXutFU8WupuBLXq';
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.rentFund),
            getAccountMeta(accounts.recipient),
            getAccountMeta(accounts.programData),
            getAccountMeta(accounts.thisProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getWithdrawRentFundInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseWithdrawRentFundInstruction(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: {
            signer: getNextAccount(),
            rentFund: getNextAccount(),
            recipient: getNextAccount(),
            programData: getNextAccount(),
            thisProgram: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getWithdrawRentFundInstructionDataDecoder().decode(instruction.data),
    };
}
