"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.REPAY_RENT_FUND_DEBT_DISCRIMINATOR = void 0;
exports.getRepayRentFundDebtDiscriminatorBytes = getRepayRentFundDebtDiscriminatorBytes;
exports.getRepayRentFundDebtInstructionDataEncoder = getRepayRentFundDebtInstructionDataEncoder;
exports.getRepayRentFundDebtInstructionDataDecoder = getRepayRentFundDebtInstructionDataDecoder;
exports.getRepayRentFundDebtInstructionDataCodec = getRepayRentFundDebtInstructionDataCodec;
exports.getRepayRentFundDebtInstructionAsync = getRepayRentFundDebtInstructionAsync;
exports.getRepayRentFundDebtInstruction = getRepayRentFundDebtInstruction;
exports.parseRepayRentFundDebtInstruction = parseRepayRentFundDebtInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.REPAY_RENT_FUND_DEBT_DISCRIMINATOR = new Uint8Array([
    111, 95, 222, 174, 241, 41, 61, 78,
]);
function getRepayRentFundDebtDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.REPAY_RENT_FUND_DEBT_DISCRIMINATOR);
}
function getRepayRentFundDebtInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)]]), (value) => ({ ...value, discriminator: exports.REPAY_RENT_FUND_DEBT_DISCRIMINATOR }));
}
function getRepayRentFundDebtInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
    ]);
}
function getRepayRentFundDebtInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getRepayRentFundDebtInstructionDataEncoder(), getRepayRentFundDebtInstructionDataDecoder());
}
async function getRepayRentFundDebtInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        recipient: { value: input.recipient ?? null, isWritable: true },
        rentClaim: { value: input.rentClaim ?? null, isWritable: true },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // 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.rentClaim.value) {
        accounts.rentClaim.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([114, 101, 110, 116, 95, 99, 108, 97, 105, 109])),
                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.recipient.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.rentFund),
            getAccountMeta(accounts.recipient),
            getAccountMeta(accounts.rentClaim),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getRepayRentFundDebtInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function getRepayRentFundDebtInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        recipient: { value: input.recipient ?? null, isWritable: true },
        rentClaim: { value: input.rentClaim ?? null, isWritable: true },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Resolve default values.
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.rentFund),
            getAccountMeta(accounts.recipient),
            getAccountMeta(accounts.rentClaim),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getRepayRentFundDebtInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function parseRepayRentFundDebtInstruction(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: {
            rentFund: getNextAccount(),
            recipient: getNextAccount(),
            rentClaim: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getRepayRentFundDebtInstructionDataDecoder().decode(instruction.data),
    };
}
