"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.CLAIM_RELAYER_REFUND_DISCRIMINATOR = void 0;
exports.getClaimRelayerRefundDiscriminatorBytes = getClaimRelayerRefundDiscriminatorBytes;
exports.getClaimRelayerRefundInstructionDataEncoder = getClaimRelayerRefundInstructionDataEncoder;
exports.getClaimRelayerRefundInstructionDataDecoder = getClaimRelayerRefundInstructionDataDecoder;
exports.getClaimRelayerRefundInstructionDataCodec = getClaimRelayerRefundInstructionDataCodec;
exports.getClaimRelayerRefundInstructionAsync = getClaimRelayerRefundInstructionAsync;
exports.getClaimRelayerRefundInstruction = getClaimRelayerRefundInstruction;
exports.parseClaimRelayerRefundInstruction = parseClaimRelayerRefundInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.CLAIM_RELAYER_REFUND_DISCRIMINATOR = new Uint8Array([
    205, 34, 34, 224, 204, 103, 81, 176,
]);
function getClaimRelayerRefundDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.CLAIM_RELAYER_REFUND_DISCRIMINATOR);
}
function getClaimRelayerRefundInstructionDataEncoder() {
    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.CLAIM_RELAYER_REFUND_DISCRIMINATOR }));
}
function getClaimRelayerRefundInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
    ]);
}
function getClaimRelayerRefundInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getClaimRelayerRefundInstructionDataEncoder(), getClaimRelayerRefundInstructionDataDecoder());
}
async function getClaimRelayerRefundInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        initializer: { value: input.initializer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: false },
        vault: { value: input.vault ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        refundAddress: { value: input.refundAddress ?? null, isWritable: false },
        tokenAccount: { value: input.tokenAccount ?? null, isWritable: true },
        claimAccount: { value: input.claimAccount ?? null, isWritable: true },
        tokenProgram: { value: input.tokenProgram ?? 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.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.vault.value) {
        accounts.vault.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.state.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
            ],
        });
    }
    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.eventAuthority.value) {
        accounts.eventAuthority.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_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.signer),
            getAccountMeta(accounts.initializer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.tokenAccount),
            getAccountMeta(accounts.claimAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getClaimRelayerRefundInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function getClaimRelayerRefundInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        initializer: { value: input.initializer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: false },
        vault: { value: input.vault ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        refundAddress: { value: input.refundAddress ?? null, isWritable: false },
        tokenAccount: { value: input.tokenAccount ?? null, isWritable: true },
        claimAccount: { value: input.claimAccount ?? null, isWritable: true },
        tokenProgram: { value: input.tokenProgram ?? 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.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.initializer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.refundAddress),
            getAccountMeta(accounts.tokenAccount),
            getAccountMeta(accounts.claimAccount),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getClaimRelayerRefundInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function parseClaimRelayerRefundInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        // 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(),
            initializer: getNextAccount(),
            state: getNextAccount(),
            vault: getNextAccount(),
            mint: getNextAccount(),
            refundAddress: getNextAccount(),
            tokenAccount: getNextAccount(),
            claimAccount: getNextAccount(),
            tokenProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getClaimRelayerRefundInstructionDataDecoder().decode(instruction.data),
    };
}
