"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.RECLAIM_USED_NONCE_ACCOUNT_DISCRIMINATOR = void 0;
exports.getReclaimUsedNonceAccountDiscriminatorBytes = getReclaimUsedNonceAccountDiscriminatorBytes;
exports.getReclaimUsedNonceAccountInstructionDataEncoder = getReclaimUsedNonceAccountInstructionDataEncoder;
exports.getReclaimUsedNonceAccountInstructionDataDecoder = getReclaimUsedNonceAccountInstructionDataDecoder;
exports.getReclaimUsedNonceAccountInstructionDataCodec = getReclaimUsedNonceAccountInstructionDataCodec;
exports.getReclaimUsedNonceAccountInstructionAsync = getReclaimUsedNonceAccountInstructionAsync;
exports.getReclaimUsedNonceAccountInstruction = getReclaimUsedNonceAccountInstruction;
exports.parseReclaimUsedNonceAccountInstruction = parseReclaimUsedNonceAccountInstruction;
const kit_1 = require("@solana/kit");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
const types_1 = require("../types");
exports.RECLAIM_USED_NONCE_ACCOUNT_DISCRIMINATOR = new Uint8Array([
    153, 152, 111, 172, 156, 104, 116, 3,
]);
function getReclaimUsedNonceAccountDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.RECLAIM_USED_NONCE_ACCOUNT_DISCRIMINATOR);
}
function getReclaimUsedNonceAccountInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ['discriminator', (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ['params', (0, types_1.getUsedNonceAccountParamsEncoder)()],
    ]), (value) => ({
        ...value,
        discriminator: exports.RECLAIM_USED_NONCE_ACCOUNT_DISCRIMINATOR,
    }));
}
function getReclaimUsedNonceAccountInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ['discriminator', (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ['params', (0, types_1.getUsedNonceAccountParamsDecoder)()],
    ]);
}
function getReclaimUsedNonceAccountInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getReclaimUsedNonceAccountInstructionDataEncoder(), getReclaimUsedNonceAccountInstructionDataDecoder());
}
async function getReclaimUsedNonceAccountInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        state: { value: input.state ?? null, isWritable: false },
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        usedNonce: { value: input.usedNonce ?? null, isWritable: true },
        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.state.value) {
        accounts.state.value = await (0, kit_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, kit_1.getBytesEncoder)().encode(new Uint8Array([115, 116, 97, 116, 101])),
            ],
        });
    }
    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.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.state),
            getAccountMeta(accounts.rentFund),
            getAccountMeta(accounts.usedNonce),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getReclaimUsedNonceAccountInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getReclaimUsedNonceAccountInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        state: { value: input.state ?? null, isWritable: false },
        rentFund: { value: input.rentFund ?? null, isWritable: true },
        usedNonce: { value: input.usedNonce ?? null, isWritable: true },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.rentFund),
            getAccountMeta(accounts.usedNonce),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getReclaimUsedNonceAccountInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseReclaimUsedNonceAccountInstruction(instruction) {
    if (instruction.accounts.length < 5) {
        // 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: {
            state: getNextAccount(),
            rentFund: getNextAccount(),
            usedNonce: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getReclaimUsedNonceAccountInstructionDataDecoder().decode(instruction.data),
    };
}
