"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR = void 0;
exports.getExecuteSlowRelayLeafDiscriminatorBytes = getExecuteSlowRelayLeafDiscriminatorBytes;
exports.getExecuteSlowRelayLeafInstructionDataEncoder = getExecuteSlowRelayLeafInstructionDataEncoder;
exports.getExecuteSlowRelayLeafInstructionDataDecoder = getExecuteSlowRelayLeafInstructionDataDecoder;
exports.getExecuteSlowRelayLeafInstructionDataCodec = getExecuteSlowRelayLeafInstructionDataCodec;
exports.getExecuteSlowRelayLeafInstructionAsync = getExecuteSlowRelayLeafInstructionAsync;
exports.getExecuteSlowRelayLeafInstruction = getExecuteSlowRelayLeafInstruction;
exports.parseExecuteSlowRelayLeafInstruction = parseExecuteSlowRelayLeafInstruction;
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");
var types_1 = require("../types");
exports.EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR = new Uint8Array([
    26, 207, 3, 168, 193, 252, 59, 127,
]);
function getExecuteSlowRelayLeafDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR);
}
function getExecuteSlowRelayLeafInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["relayHash", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32)],
        ["slowFillLeaf", (0, kit_1.getOptionEncoder)((0, types_1.getSlowFillEncoder)())],
        ["rootBundleId", (0, kit_1.getOptionEncoder)((0, kit_1.getU32Encoder)())],
        [
            "proof",
            (0, kit_1.getOptionEncoder)((0, kit_1.getArrayEncoder)((0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 32))),
        ],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR })); });
}
function getExecuteSlowRelayLeafInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["relayHash", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32)],
        ["slowFillLeaf", (0, kit_1.getOptionDecoder)((0, types_1.getSlowFillDecoder)())],
        ["rootBundleId", (0, kit_1.getOptionDecoder)((0, kit_1.getU32Decoder)())],
        [
            "proof",
            (0, kit_1.getOptionDecoder)((0, kit_1.getArrayDecoder)((0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 32))),
        ],
    ]);
}
function getExecuteSlowRelayLeafInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getExecuteSlowRelayLeafInstructionDataEncoder(), getExecuteSlowRelayLeafInstructionDataDecoder());
}
function getExecuteSlowRelayLeafInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, _c, _d, _e, getAccountMeta;
        var _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
        return tslib_1.__generator(this, function (_u) {
            switch (_u.label) {
                case 0:
                    programAddress = (_f = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _f !== void 0 ? _f : programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_g = input.signer) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        instructionParams: {
                            value: (_h = input.instructionParams) !== null && _h !== void 0 ? _h : null,
                            isWritable: true,
                        },
                        state: { value: (_j = input.state) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        rootBundle: { value: (_k = input.rootBundle) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        fillStatus: { value: (_l = input.fillStatus) !== null && _l !== void 0 ? _l : null, isWritable: true },
                        mint: { value: (_m = input.mint) !== null && _m !== void 0 ? _m : null, isWritable: false },
                        recipientTokenAccount: {
                            value: (_o = input.recipientTokenAccount) !== null && _o !== void 0 ? _o : null,
                            isWritable: true,
                        },
                        vault: { value: (_p = input.vault) !== null && _p !== void 0 ? _p : null, isWritable: true },
                        tokenProgram: { value: (_q = input.tokenProgram) !== null && _q !== void 0 ? _q : null, isWritable: false },
                        systemProgram: { value: (_r = input.systemProgram) !== null && _r !== void 0 ? _r : null, isWritable: false },
                        eventAuthority: { value: (_s = input.eventAuthority) !== null && _s !== void 0 ? _s : null, isWritable: false },
                        program: { value: (_t = input.program) !== null && _t !== void 0 ? _t : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    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 = _u.sent();
                    _u.label = 2;
                case 2:
                    if (!!accounts.fillStatus.value) return [3, 4];
                    _b = accounts.fillStatus;
                    return [4, (0, pdas_1.findFillStatusPda)({
                            relayHash: (0, shared_1.expectSome)(args.relayHash),
                        })];
                case 3:
                    _b.value = _u.sent();
                    _u.label = 4;
                case 4:
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.recipientTokenAccount.value) return [3, 6];
                    _c = accounts.recipientTokenAccount;
                    return [4, (0, kit_1.getProgramDerivedAddress)({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                (0, kit_1.getOptionEncoder)((0, types_1.getSlowFillEncoder)()).encode((0, shared_1.expectSome)(args.slowFillLeaf)),
                                (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 5:
                    _c.value = _u.sent();
                    _u.label = 6;
                case 6:
                    if (!!accounts.vault.value) return [3, 8];
                    _d = 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 7:
                    _d.value = _u.sent();
                    _u.label = 8;
                case 8:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3, 10];
                    _e = accounts.eventAuthority;
                    return [4, (0, pdas_1.findEventAuthorityPda)()];
                case 9:
                    _e.value = _u.sent();
                    _u.label = 10;
                case 10:
                    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.fillStatus),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.recipientTokenAccount),
                                getAccountMeta(accounts.vault),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getExecuteSlowRelayLeafInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getExecuteSlowRelayLeafInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    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 },
        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: false },
        fillStatus: { value: (_f = input.fillStatus) !== null && _f !== void 0 ? _f : null, isWritable: true },
        mint: { value: (_g = input.mint) !== null && _g !== void 0 ? _g : null, isWritable: false },
        recipientTokenAccount: {
            value: (_h = input.recipientTokenAccount) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        vault: { value: (_j = input.vault) !== null && _j !== void 0 ? _j : null, isWritable: true },
        tokenProgram: { value: (_k = input.tokenProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        systemProgram: { value: (_l = input.systemProgram) !== null && _l !== void 0 ? _l : null, isWritable: false },
        eventAuthority: { value: (_m = input.eventAuthority) !== null && _m !== void 0 ? _m : null, isWritable: false },
        program: { value: (_o = input.program) !== null && _o !== void 0 ? _o : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    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.fillStatus),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getExecuteSlowRelayLeafInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseExecuteSlowRelayLeafInstruction(instruction) {
    if (instruction.accounts.length < 12) {
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    var getNextOptionalAccount = function () {
        var accountMeta = getNextAccount();
        return accountMeta.address === programs_1.SVM_SPOKE_PROGRAM_ADDRESS
            ? undefined
            : accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            signer: getNextAccount(),
            instructionParams: getNextOptionalAccount(),
            state: getNextAccount(),
            rootBundle: getNextAccount(),
            fillStatus: getNextAccount(),
            mint: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            vault: getNextAccount(),
            tokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getExecuteSlowRelayLeafInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=executeSlowRelayLeaf.js.map