"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.INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR = void 0;
exports.getInitializeClaimAccountDiscriminatorBytes = getInitializeClaimAccountDiscriminatorBytes;
exports.getInitializeClaimAccountInstructionDataEncoder = getInitializeClaimAccountInstructionDataEncoder;
exports.getInitializeClaimAccountInstructionDataDecoder = getInitializeClaimAccountInstructionDataDecoder;
exports.getInitializeClaimAccountInstructionDataCodec = getInitializeClaimAccountInstructionDataCodec;
exports.getInitializeClaimAccountInstructionAsync = getInitializeClaimAccountInstructionAsync;
exports.getInitializeClaimAccountInstruction = getInitializeClaimAccountInstruction;
exports.parseInitializeClaimAccountInstruction = parseInitializeClaimAccountInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR = new Uint8Array([
    22, 247, 214, 191, 90, 74, 87, 216,
]);
function getInitializeClaimAccountDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR);
}
function getInitializeClaimAccountInstructionDataEncoder() {
    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.INITIALIZE_CLAIM_ACCOUNT_DISCRIMINATOR,
    }));
}
function getInitializeClaimAccountInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
    ]);
}
function getInitializeClaimAccountInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getInitializeClaimAccountInstructionDataEncoder(), getInitializeClaimAccountInstructionDataDecoder());
}
async function getInitializeClaimAccountInstructionAsync(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 },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    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)),
            ],
        });
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.claimAccount),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getInitializeClaimAccountInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function getInitializeClaimAccountInstruction(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 },
        systemProgram: { value: input.systemProgram ?? 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.signer),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.claimAccount),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getInitializeClaimAccountInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function parseInitializeClaimAccountInstruction(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: {
            signer: getNextAccount(),
            mint: getNextAccount(),
            refundAddress: getNextAccount(),
            claimAccount: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getInitializeClaimAccountInstructionDataDecoder().decode(instruction.data),
    };
}
