"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EXECUTE_RELAYER_REFUND_LEAF_DEFERRED_DISCRIMINATOR = void 0;
exports.getExecuteRelayerRefundLeafDeferredDiscriminatorBytes = getExecuteRelayerRefundLeafDeferredDiscriminatorBytes;
exports.getExecuteRelayerRefundLeafDeferredInstructionDataEncoder = getExecuteRelayerRefundLeafDeferredInstructionDataEncoder;
exports.getExecuteRelayerRefundLeafDeferredInstructionDataDecoder = getExecuteRelayerRefundLeafDeferredInstructionDataDecoder;
exports.getExecuteRelayerRefundLeafDeferredInstructionDataCodec = getExecuteRelayerRefundLeafDeferredInstructionDataCodec;
exports.getExecuteRelayerRefundLeafDeferredInstructionAsync = getExecuteRelayerRefundLeafDeferredInstructionAsync;
exports.getExecuteRelayerRefundLeafDeferredInstruction = getExecuteRelayerRefundLeafDeferredInstruction;
exports.parseExecuteRelayerRefundLeafDeferredInstruction = parseExecuteRelayerRefundLeafDeferredInstruction;
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.EXECUTE_RELAYER_REFUND_LEAF_DEFERRED_DISCRIMINATOR = new Uint8Array([110, 112, 89, 208, 38, 116, 93, 10]);
function getExecuteRelayerRefundLeafDeferredDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.EXECUTE_RELAYER_REFUND_LEAF_DEFERRED_DISCRIMINATOR);
}
function getExecuteRelayerRefundLeafDeferredInstructionDataEncoder() {
    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.EXECUTE_RELAYER_REFUND_LEAF_DEFERRED_DISCRIMINATOR })); });
}
function getExecuteRelayerRefundLeafDeferredInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
    ]);
}
function getExecuteRelayerRefundLeafDeferredInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getExecuteRelayerRefundLeafDeferredInstructionDataEncoder(), getExecuteRelayerRefundLeafDeferredInstructionDataDecoder());
}
function getExecuteRelayerRefundLeafDeferredInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, _a, _b, _c, _d, getAccountMeta;
        var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
        return tslib_1.__generator(this, function (_s) {
            switch (_s.label) {
                case 0:
                    programAddress = (_e = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _e !== void 0 ? _e : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_f = input.signer) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        instructionParams: {
                            value: (_g = input.instructionParams) !== null && _g !== void 0 ? _g : null,
                            isWritable: true,
                        },
                        state: { value: (_h = input.state) !== null && _h !== void 0 ? _h : null, isWritable: false },
                        rootBundle: { value: (_j = input.rootBundle) !== null && _j !== void 0 ? _j : null, isWritable: true },
                        vault: { value: (_k = input.vault) !== null && _k !== void 0 ? _k : null, isWritable: true },
                        mint: { value: (_l = input.mint) !== null && _l !== void 0 ? _l : null, isWritable: false },
                        transferLiability: {
                            value: (_m = input.transferLiability) !== null && _m !== void 0 ? _m : null,
                            isWritable: true,
                        },
                        tokenProgram: { value: (_o = input.tokenProgram) !== null && _o !== void 0 ? _o : null, isWritable: false },
                        systemProgram: { value: (_p = input.systemProgram) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        eventAuthority: { value: (_q = input.eventAuthority) !== null && _q !== void 0 ? _q : null, isWritable: false },
                        program: { value: (_r = input.program) !== null && _r !== void 0 ? _r : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    if (!!accounts.instructionParams.value) return [3, 2];
                    _a = accounts.instructionParams;
                    return [4, (0, pdas_1.findInstructionParamsPda)({
                            signer: (0, shared_1.expectAddress)(accounts.signer.value),
                        })];
                case 1:
                    _a.value = _s.sent();
                    _s.label = 2;
                case 2:
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.vault.value) return [3, 4];
                    _b = 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 3:
                    _b.value = _s.sent();
                    _s.label = 4;
                case 4:
                    if (!!accounts.transferLiability.value) return [3, 6];
                    _c = accounts.transferLiability;
                    return [4, (0, pdas_1.findTransferLiabilityPda)({
                            mint: (0, shared_1.expectAddress)(accounts.mint.value),
                        })];
                case 5:
                    _c.value = _s.sent();
                    _s.label = 6;
                case 6:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 8];
                    _d = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 7:
                    _d.value = _s.sent();
                    _s.label = 8;
                case 8:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.instructionParams),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.rootBundle),
                                getAccountMeta(accounts.vault),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.transferLiability),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getExecuteRelayerRefundLeafDeferredInstructionDataEncoder().encode({}),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getExecuteRelayerRefundLeafDeferredInstruction(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: true },
        instructionParams: {
            value: (_c = input.instructionParams) !== null && _c !== void 0 ? _c : null,
            isWritable: true,
        },
        state: { value: (_d = input.state) !== null && _d !== void 0 ? _d : null, isWritable: false },
        rootBundle: { value: (_e = input.rootBundle) !== null && _e !== void 0 ? _e : null, isWritable: true },
        vault: { value: (_f = input.vault) !== null && _f !== void 0 ? _f : null, isWritable: true },
        mint: { value: (_g = input.mint) !== null && _g !== void 0 ? _g : null, isWritable: false },
        transferLiability: {
            value: (_h = input.transferLiability) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        tokenProgram: { value: (_j = input.tokenProgram) !== null && _j !== void 0 ? _j : null, isWritable: false },
        systemProgram: { value: (_k = input.systemProgram) !== 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";
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.instructionParams),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.rootBundle),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.transferLiability),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getExecuteRelayerRefundLeafDeferredInstructionDataEncoder().encode({}),
        programAddress: programAddress,
    });
}
function parseExecuteRelayerRefundLeafDeferredInstruction(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(),
            instructionParams: getNextAccount(),
            state: getNextAccount(),
            rootBundle: getNextAccount(),
            vault: getNextAccount(),
            mint: getNextAccount(),
            transferLiability: getNextAccount(),
            tokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getExecuteRelayerRefundLeafDeferredInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=executeRelayerRefundLeafDeferred.js.map