"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.CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR = void 0;
exports.getCloseClaimAccountDiscriminatorBytes = getCloseClaimAccountDiscriminatorBytes;
exports.getCloseClaimAccountInstructionDataEncoder = getCloseClaimAccountInstructionDataEncoder;
exports.getCloseClaimAccountInstructionDataDecoder = getCloseClaimAccountInstructionDataDecoder;
exports.getCloseClaimAccountInstructionDataCodec = getCloseClaimAccountInstructionDataCodec;
exports.getCloseClaimAccountInstructionAsync = getCloseClaimAccountInstructionAsync;
exports.getCloseClaimAccountInstruction = getCloseClaimAccountInstruction;
exports.parseCloseClaimAccountInstruction = parseCloseClaimAccountInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR = new Uint8Array([
    241, 146, 203, 216, 58, 222, 91, 118,
]);
function getCloseClaimAccountDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR);
}
function getCloseClaimAccountInstructionDataEncoder() {
    return (0, web3_v2_js_1.transformEncoder)((0, web3_v2_js_1.getStructEncoder)([['discriminator', (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8)]]), (value) => ({ ...value, discriminator: exports.CLOSE_CLAIM_ACCOUNT_DISCRIMINATOR }));
}
function getCloseClaimAccountInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
    ]);
}
function getCloseClaimAccountInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getCloseClaimAccountInstructionDataEncoder(), getCloseClaimAccountInstructionDataDecoder());
}
async function getCloseClaimAccountInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        refundAddress: { value: input.refundAddress ?? null, isWritable: false },
        claimAccount: { value: input.claimAccount ?? null, isWritable: true },
    };
    const accounts = originalAccounts;
    // Resolve default values.
    if (!accounts.claimAccount.value) {
        accounts.claimAccount.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getBytesEncoder)().encode(new Uint8Array([
                    99, 108, 97, 105, 109, 95, 97, 99, 99, 111, 117, 110, 116,
                ])),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.refundAddress.value)),
            ],
        });
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.claimAccount),
        ],
        programAddress,
        data: getCloseClaimAccountInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function getCloseClaimAccountInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        refundAddress: { value: input.refundAddress ?? null, isWritable: false },
        claimAccount: { value: input.claimAccount ?? null, isWritable: true },
    };
    const accounts = originalAccounts;
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.claimAccount),
        ],
        programAddress,
        data: getCloseClaimAccountInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function parseCloseClaimAccountInstruction(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: {
            signer: getNextAccount(),
            mint: getNextAccount(),
            refundAddress: getNextAccount(),
            claimAccount: getNextAccount(),
        },
        data: getCloseClaimAccountInstructionDataDecoder().decode(instruction.data),
    };
}
