"use strict";
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;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var pdas_1 = require("../pdas");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.CLAIM_RELAYER_REFUND_DISCRIMINATOR = new Uint8Array([
    205, 34, 34, 224, 204, 103, 81, 176,
]);
function getClaimRelayerRefundDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.CLAIM_RELAYER_REFUND_DISCRIMINATOR);
}
function getClaimRelayerRefundInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)]]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.CLAIM_RELAYER_REFUND_DISCRIMINATOR })); });
}
function getClaimRelayerRefundInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
    ]);
}
function getClaimRelayerRefundInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getClaimRelayerRefundInstructionDataEncoder(), getClaimRelayerRefundInstructionDataDecoder());
}
function getClaimRelayerRefundInstructionAsync(input, config) {
    return tslib_1.__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 tslib_1.__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 : programs_1.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;
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.vault.value) return [3, 2];
                    _a = accounts.vault;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.state.value)),
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                                (0, kit_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
                            ],
                        })];
                case 1:
                    _a.value = _r.sent();
                    _r.label = 2;
                case 2:
                    if (!!accounts.claimAccount.value) return [3, 4];
                    _b = accounts.claimAccount;
                    return [4, (0, pdas_1.findClaimAccountPda)({
                            mint: (0, shared_1.expectAddress)(accounts.mint.value),
                            refundAddress: (0, shared_1.expectAddress)(accounts.refundAddress.value),
                        })];
                case 3:
                    _b.value = _r.sent();
                    _r.label = 4;
                case 4:
                    if (!!accounts.eventAuthority.value) return [3, 6];
                    _c = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 5:
                    _c.value = _r.sent();
                    _r.label = 6;
                case 6:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, 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,
                        })];
            }
        });
    });
}
function getClaimRelayerRefundInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    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;
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    var getAccountMeta = (0, shared_1.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,
    });
}
function parseClaimRelayerRefundInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        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