/**
 * 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, getAddressDecoder, getAddressEncoder, getBytesDecoder, getBytesEncoder, getOptionDecoder, getOptionEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, transformEncoder, } from "@solana/kit";
import { findEventAuthorityPda, findFillStatusPda, findInstructionParamsPda, } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, expectSome, getAccountMetaFactory, } from "../shared";
import { getRelayDataDecoder, getRelayDataEncoder, } from "../types";
export var FILL_RELAY_DISCRIMINATOR = new Uint8Array([
    100, 84, 222, 90, 106, 209, 58, 222,
]);
export function getFillRelayDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(FILL_RELAY_DISCRIMINATOR);
}
export function getFillRelayInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["relayHash", fixEncoderSize(getBytesEncoder(), 32)],
        ["relayData", getOptionEncoder(getRelayDataEncoder())],
        ["repaymentChainId", getOptionEncoder(getU64Encoder())],
        ["repaymentAddress", getOptionEncoder(getAddressEncoder())],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: FILL_RELAY_DISCRIMINATOR })); });
}
export function getFillRelayInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["relayHash", fixDecoderSize(getBytesDecoder(), 32)],
        ["relayData", getOptionDecoder(getRelayDataDecoder())],
        ["repaymentChainId", getOptionDecoder(getU64Decoder())],
        ["repaymentAddress", getOptionDecoder(getAddressDecoder())],
    ]);
}
export function getFillRelayInstructionDataCodec() {
    return combineCodec(getFillRelayInstructionDataEncoder(), getFillRelayInstructionDataDecoder());
}
export function getFillRelayInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, _c, _d, getAccountMeta;
        var _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
        return __generator(this, function (_u) {
            switch (_u.label) {
                case 0:
                    programAddress = (_e = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _e !== void 0 ? _e : 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 },
                        delegate: { value: (_j = input.delegate) !== null && _j !== void 0 ? _j : null, isWritable: false },
                        mint: { value: (_k = input.mint) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        relayerTokenAccount: {
                            value: (_l = input.relayerTokenAccount) !== null && _l !== void 0 ? _l : null,
                            isWritable: true,
                        },
                        recipientTokenAccount: {
                            value: (_m = input.recipientTokenAccount) !== null && _m !== void 0 ? _m : null,
                            isWritable: true,
                        },
                        fillStatus: { value: (_o = input.fillStatus) !== null && _o !== void 0 ? _o : null, isWritable: true },
                        tokenProgram: { value: (_p = input.tokenProgram) !== null && _p !== void 0 ? _p : null, isWritable: false },
                        associatedTokenProgram: {
                            value: (_q = input.associatedTokenProgram) !== 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.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.recipientTokenAccount.value) return [3 /*break*/, 4];
                    _b = accounts.recipientTokenAccount;
                    return [4 /*yield*/, getProgramDerivedAddress({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                getOptionEncoder(getRelayDataEncoder()).encode(expectSome(args.relayData)),
                                getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
                                getAddressEncoder().encode(expectAddress(accounts.mint.value)),
                            ],
                        })];
                case 3:
                    _b.value = _u.sent();
                    _u.label = 4;
                case 4:
                    if (!!accounts.fillStatus.value) return [3 /*break*/, 6];
                    _c = accounts.fillStatus;
                    return [4 /*yield*/, findFillStatusPda({
                            relayHash: expectSome(args.relayHash),
                        })];
                case 5:
                    _c.value = _u.sent();
                    _u.label = 6;
                case 6:
                    if (!accounts.associatedTokenProgram.value) {
                        accounts.associatedTokenProgram.value =
                            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
                    }
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 8];
                    _d = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 7:
                    _d.value = _u.sent();
                    _u.label = 8;
                case 8:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.instructionParams),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.delegate),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.relayerTokenAccount),
                                getAccountMeta(accounts.recipientTokenAccount),
                                getAccountMeta(accounts.fillStatus),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.associatedTokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getFillRelayInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getFillRelayInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    // 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: 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 },
        delegate: { value: (_e = input.delegate) !== null && _e !== void 0 ? _e : null, isWritable: false },
        mint: { value: (_f = input.mint) !== null && _f !== void 0 ? _f : null, isWritable: false },
        relayerTokenAccount: {
            value: (_g = input.relayerTokenAccount) !== null && _g !== void 0 ? _g : null,
            isWritable: true,
        },
        recipientTokenAccount: {
            value: (_h = input.recipientTokenAccount) !== null && _h !== void 0 ? _h : null,
            isWritable: true,
        },
        fillStatus: { value: (_j = input.fillStatus) !== null && _j !== void 0 ? _j : null, isWritable: true },
        tokenProgram: { value: (_k = input.tokenProgram) !== null && _k !== void 0 ? _k : null, isWritable: false },
        associatedTokenProgram: {
            value: (_l = input.associatedTokenProgram) !== null && _l !== void 0 ? _l : null,
            isWritable: false,
        },
        systemProgram: { value: (_m = input.systemProgram) !== null && _m !== void 0 ? _m : null, isWritable: false },
        eventAuthority: { value: (_o = input.eventAuthority) !== null && _o !== void 0 ? _o : null, isWritable: false },
        program: { value: (_p = input.program) !== null && _p !== void 0 ? _p : 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.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
    }
    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.delegate),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.relayerTokenAccount),
            getAccountMeta(accounts.recipientTokenAccount),
            getAccountMeta(accounts.fillStatus),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getFillRelayInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
export function parseFillRelayInstruction(instruction) {
    if (instruction.accounts.length < 13) {
        // 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(),
            delegate: getNextAccount(),
            mint: getNextAccount(),
            relayerTokenAccount: getNextAccount(),
            recipientTokenAccount: getNextAccount(),
            fillStatus: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getFillRelayInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=fillRelay.js.map