/**
 * 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, getArrayDecoder, getArrayEncoder, getBytesDecoder, getBytesEncoder, getOptionDecoder, getOptionEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, transformEncoder, } from "@solana/kit";
import { findEventAuthorityPda, findFillStatusPda, findInstructionParamsPda, } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, expectSome, getAccountMetaFactory, } from "../shared";
import { getSlowFillDecoder, getSlowFillEncoder, } from "../types";
export var EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR = new Uint8Array([
    26, 207, 3, 168, 193, 252, 59, 127,
]);
export function getExecuteSlowRelayLeafDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR);
}
export function getExecuteSlowRelayLeafInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["relayHash", fixEncoderSize(getBytesEncoder(), 32)],
        ["slowFillLeaf", getOptionEncoder(getSlowFillEncoder())],
        ["rootBundleId", getOptionEncoder(getU32Encoder())],
        [
            "proof",
            getOptionEncoder(getArrayEncoder(fixEncoderSize(getBytesEncoder(), 32))),
        ],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: EXECUTE_SLOW_RELAY_LEAF_DISCRIMINATOR })); });
}
export function getExecuteSlowRelayLeafInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["relayHash", fixDecoderSize(getBytesDecoder(), 32)],
        ["slowFillLeaf", getOptionDecoder(getSlowFillDecoder())],
        ["rootBundleId", getOptionDecoder(getU32Decoder())],
        [
            "proof",
            getOptionDecoder(getArrayDecoder(fixDecoderSize(getBytesDecoder(), 32))),
        ],
    ]);
}
export function getExecuteSlowRelayLeafInstructionDataCodec() {
    return combineCodec(getExecuteSlowRelayLeafInstructionDataEncoder(), getExecuteSlowRelayLeafInstructionDataDecoder());
}
export function getExecuteSlowRelayLeafInstructionAsync(input, config) {
    return __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 __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 : 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 = __assign({}, input);
                    if (!!accounts.instructionParams.value) return [3 /*break*/, 2];
                    _a = accounts.instructionParams;
                    return [4 /*yield*/, findInstructionParamsPda({
                            signer: expectAddress(accounts.signer.value),
                        })];
                case 1:
                    _a.value = _u.sent();
                    _u.label = 2;
                case 2:
                    if (!!accounts.fillStatus.value) return [3 /*break*/, 4];
                    _b = accounts.fillStatus;
                    return [4 /*yield*/, findFillStatusPda({
                            relayHash: 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 /*break*/, 6];
                    _c = accounts.recipientTokenAccount;
                    return [4 /*yield*/, getProgramDerivedAddress({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                getOptionEncoder(getSlowFillEncoder()).encode(expectSome(args.slowFillLeaf)),
                                getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
                                getAddressEncoder().encode(expectAddress(accounts.mint.value)),
                            ],
                        })];
                case 5:
                    _c.value = _u.sent();
                    _u.label = 6;
                case 6:
                    if (!!accounts.vault.value) return [3 /*break*/, 8];
                    _d = 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 7:
                    _d.value = _u.sent();
                    _u.label = 8;
                case 8:
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 10];
                    _e = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 9:
                    _e.value = _u.sent();
                    _u.label = 10;
                case 10:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*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,
                        })];
            }
        });
    });
}
export function getExecuteSlowRelayLeafInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    // 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 },
        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;
    // Original args.
    var args = __assign({}, input);
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
    }
    if (!accounts.systemProgram.value) {
        accounts.systemProgram.value =
            "11111111111111111111111111111111";
    }
    var getAccountMeta = 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,
    });
}
export function parseExecuteSlowRelayLeafInstruction(instruction) {
    if (instruction.accounts.length < 12) {
        // TODO: Coded error.
        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 === 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