/**
 * 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
 */
import { __assign, __awaiter, __generator } from "tslib";
import { combineCodec, fixDecoderSize, fixEncoderSize, getAddressEncoder, getBytesDecoder, getBytesEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, transformEncoder, } from "@solana/kit";
import { findClaimAccountPda, findEventAuthorityPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, getAccountMetaFactory, } from "../shared";
export var CLAIM_RELAYER_REFUND_DISCRIMINATOR = new Uint8Array([
    205, 34, 34, 224, 204, 103, 81, 176,
]);
export function getClaimRelayerRefundDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(CLAIM_RELAYER_REFUND_DISCRIMINATOR);
}
export function getClaimRelayerRefundInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]), function (value) { return (__assign(__assign({}, value), { discriminator: CLAIM_RELAYER_REFUND_DISCRIMINATOR })); });
}
export function getClaimRelayerRefundInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ]);
}
export function getClaimRelayerRefundInstructionDataCodec() {
    return combineCodec(getClaimRelayerRefundInstructionDataEncoder(), getClaimRelayerRefundInstructionDataDecoder());
}
export function getClaimRelayerRefundInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, _a, _b, _c, getAccountMeta;
        var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
        return __generator(this, function (_r) {
            switch (_r.label) {
                case 0:
                    programAddress = (_d = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _d !== void 0 ? _d : SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_e = input.signer) !== null && _e !== void 0 ? _e : null, isWritable: false },
                        initializer: { value: (_f = input.initializer) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        state: { value: (_g = input.state) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        vault: { value: (_h = input.vault) !== null && _h !== void 0 ? _h : null, isWritable: true },
                        mint: { value: (_j = input.mint) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        refundAddress: { value: (_k = input.refundAddress) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        tokenAccount: { value: (_l = input.tokenAccount) !== null && _l !== void 0 ? _l : null, isWritable: true },
                        claimAccount: { value: (_m = input.claimAccount) !== null && _m !== void 0 ? _m : null, isWritable: true },
                        tokenProgram: { value: (_o = input.tokenProgram) !== null && _o !== void 0 ? _o : null, isWritable: false },
                        eventAuthority: { value: (_p = input.eventAuthority) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        program: { value: (_q = input.program) !== null && _q !== void 0 ? _q : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    // Resolve default values.
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.vault.value) return [3 /*break*/, 2];
                    _a = accounts.vault;
                    return [4 /*yield*/, getProgramDerivedAddress({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                getAddressEncoder().encode(expectAddress(accounts.state.value)),
                                getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
                                getAddressEncoder().encode(expectAddress(accounts.mint.value)),
                            ],
                        })];
                case 1:
                    _a.value = _r.sent();
                    _r.label = 2;
                case 2:
                    if (!!accounts.claimAccount.value) return [3 /*break*/, 4];
                    _b = accounts.claimAccount;
                    return [4 /*yield*/, findClaimAccountPda({
                            mint: expectAddress(accounts.mint.value),
                            refundAddress: expectAddress(accounts.refundAddress.value),
                        })];
                case 3:
                    _b.value = _r.sent();
                    _r.label = 4;
                case 4:
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 6];
                    _c = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 5:
                    _c.value = _r.sent();
                    _r.label = 6;
                case 6:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            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),
                            ],
                            data: getClaimRelayerRefundInstructionDataEncoder().encode({}),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getClaimRelayerRefundInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    // Program address.
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    var originalAccounts = {
        signer: { value: (_b = input.signer) !== null && _b !== void 0 ? _b : null, isWritable: false },
        initializer: { value: (_c = input.initializer) !== null && _c !== void 0 ? _c : null, isWritable: true },
        state: { value: (_d = input.state) !== null && _d !== void 0 ? _d : null, isWritable: false },
        vault: { value: (_e = input.vault) !== null && _e !== void 0 ? _e : null, isWritable: true },
        mint: { value: (_f = input.mint) !== null && _f !== void 0 ? _f : null, isWritable: false },
        refundAddress: { value: (_g = input.refundAddress) !== null && _g !== void 0 ? _g : null, isWritable: false },
        tokenAccount: { value: (_h = input.tokenAccount) !== null && _h !== void 0 ? _h : null, isWritable: true },
        claimAccount: { value: (_j = input.claimAccount) !== null && _j !== void 0 ? _j : null, isWritable: true },
        tokenProgram: { value: (_k = input.tokenProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        eventAuthority: { value: (_l = input.eventAuthority) !== null && _l !== void 0 ? _l : null, isWritable: false },
        program: { value: (_m = input.program) !== null && _m !== void 0 ? _m : null, isWritable: false },
    };
    var accounts = originalAccounts;
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    var getAccountMeta = getAccountMetaFactory(programAddress, "programId");
    return Object.freeze({
        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),
        ],
        data: getClaimRelayerRefundInstructionDataEncoder().encode({}),
        programAddress: programAddress,
    });
}
export function parseClaimRelayerRefundInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        // TODO: Coded error.
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var 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),
    };
}
//# sourceMappingURL=claimRelayerRefund.js.map