/**
 * 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 { addDecoderSizePrefix, addEncoderSizePrefix, combineCodec, fixDecoderSize, fixEncoderSize, getAddressDecoder, getAddressEncoder, getBytesDecoder, getBytesEncoder, getProgramDerivedAddress, getStructDecoder, getStructEncoder, getU32Decoder, getU32Encoder, getU64Decoder, getU64Encoder, transformEncoder, } from "@solana/kit";
import { findEventAuthorityPda } from "../pdas";
import { SVM_SPOKE_PROGRAM_ADDRESS } from "../programs";
import { expectAddress, expectSome, getAccountMetaFactory, } from "../shared";
export var DEPOSIT_NOW_DISCRIMINATOR = new Uint8Array([
    75, 228, 135, 221, 200, 25, 148, 26,
]);
export function getDepositNowDiscriminatorBytes() {
    return fixEncoderSize(getBytesEncoder(), 8).encode(DEPOSIT_NOW_DISCRIMINATOR);
}
export function getDepositNowInstructionDataEncoder() {
    return transformEncoder(getStructEncoder([
        ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
        ["depositor", getAddressEncoder()],
        ["recipient", getAddressEncoder()],
        ["inputToken", getAddressEncoder()],
        ["outputToken", getAddressEncoder()],
        ["inputAmount", getU64Encoder()],
        ["outputAmount", fixEncoderSize(getBytesEncoder(), 32)],
        ["destinationChainId", getU64Encoder()],
        ["exclusiveRelayer", getAddressEncoder()],
        ["fillDeadlineOffset", getU32Encoder()],
        ["exclusivityParameter", getU32Encoder()],
        ["message", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
    ]), function (value) { return (__assign(__assign({}, value), { discriminator: DEPOSIT_NOW_DISCRIMINATOR })); });
}
export function getDepositNowInstructionDataDecoder() {
    return getStructDecoder([
        ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
        ["depositor", getAddressDecoder()],
        ["recipient", getAddressDecoder()],
        ["inputToken", getAddressDecoder()],
        ["outputToken", getAddressDecoder()],
        ["inputAmount", getU64Decoder()],
        ["outputAmount", fixDecoderSize(getBytesDecoder(), 32)],
        ["destinationChainId", getU64Decoder()],
        ["exclusiveRelayer", getAddressDecoder()],
        ["fillDeadlineOffset", getU32Decoder()],
        ["exclusivityParameter", getU32Decoder()],
        ["message", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
    ]);
}
export function getDepositNowInstructionDataCodec() {
    return combineCodec(getDepositNowInstructionDataEncoder(), getDepositNowInstructionDataDecoder());
}
export function getDepositNowInstructionAsync(input, config) {
    return __awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, _b, _c, getAccountMeta;
        var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
        return __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 : SVM_SPOKE_PROGRAM_ADDRESS;
                    originalAccounts = {
                        signer: { value: (_e = input.signer) !== null && _e !== void 0 ? _e : null, isWritable: true },
                        state: { value: (_f = input.state) !== null && _f !== void 0 ? _f : null, isWritable: true },
                        delegate: { value: (_g = input.delegate) !== null && _g !== void 0 ? _g : null, isWritable: false },
                        depositorTokenAccount: {
                            value: (_h = input.depositorTokenAccount) !== null && _h !== void 0 ? _h : null,
                            isWritable: true,
                        },
                        vault: { value: (_j = input.vault) !== null && _j !== void 0 ? _j : null, isWritable: true },
                        mint: { value: (_k = input.mint) !== null && _k !== void 0 ? _k : null, isWritable: false },
                        tokenProgram: { value: (_l = input.tokenProgram) !== null && _l !== void 0 ? _l : null, isWritable: false },
                        associatedTokenProgram: {
                            value: (_m = input.associatedTokenProgram) !== null && _m !== void 0 ? _m : null,
                            isWritable: false,
                        },
                        systemProgram: { value: (_o = input.systemProgram) !== 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;
                    args = __assign({}, input);
                    // Resolve default values.
                    if (!accounts.tokenProgram.value) {
                        accounts.tokenProgram.value =
                            "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
                    }
                    if (!!accounts.depositorTokenAccount.value) return [3 /*break*/, 2];
                    _a = accounts.depositorTokenAccount;
                    return [4 /*yield*/, getProgramDerivedAddress({
                            programAddress: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
                            seeds: [
                                getAddressEncoder().encode(expectSome(args.depositor)),
                                getAddressEncoder().encode(expectAddress(accounts.tokenProgram.value)),
                                getAddressEncoder().encode(expectAddress(accounts.mint.value)),
                            ],
                        })];
                case 1:
                    _a.value = _r.sent();
                    _r.label = 2;
                case 2:
                    if (!!accounts.vault.value) return [3 /*break*/, 4];
                    _b = 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 3:
                    _b.value = _r.sent();
                    _r.label = 4;
                case 4:
                    if (!accounts.associatedTokenProgram.value) {
                        accounts.associatedTokenProgram.value =
                            "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
                    }
                    if (!accounts.systemProgram.value) {
                        accounts.systemProgram.value =
                            "11111111111111111111111111111111";
                    }
                    if (!!accounts.eventAuthority.value) return [3 /*break*/, 6];
                    _c = accounts.eventAuthority;
                    return [4 /*yield*/, findEventAuthorityPda()];
                case 5:
                    _c.value = _r.sent();
                    _r.label = 6;
                case 6:
                    getAccountMeta = getAccountMetaFactory(programAddress, "programId");
                    return [2 /*return*/, Object.freeze({
                            accounts: [
                                getAccountMeta(accounts.signer),
                                getAccountMeta(accounts.state),
                                getAccountMeta(accounts.delegate),
                                getAccountMeta(accounts.depositorTokenAccount),
                                getAccountMeta(accounts.vault),
                                getAccountMeta(accounts.mint),
                                getAccountMeta(accounts.tokenProgram),
                                getAccountMeta(accounts.associatedTokenProgram),
                                getAccountMeta(accounts.systemProgram),
                                getAccountMeta(accounts.eventAuthority),
                                getAccountMeta(accounts.program),
                            ],
                            data: getDepositNowInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
export function getDepositNowInstruction(input, config) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
    // 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 },
        state: { value: (_c = input.state) !== null && _c !== void 0 ? _c : null, isWritable: true },
        delegate: { value: (_d = input.delegate) !== null && _d !== void 0 ? _d : null, isWritable: false },
        depositorTokenAccount: {
            value: (_e = input.depositorTokenAccount) !== 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 },
        tokenProgram: { value: (_h = input.tokenProgram) !== null && _h !== void 0 ? _h : null, isWritable: false },
        associatedTokenProgram: {
            value: (_j = input.associatedTokenProgram) !== 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;
    // 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.state),
            getAccountMeta(accounts.delegate),
            getAccountMeta(accounts.depositorTokenAccount),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        data: getDepositNowInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
export function parseDepositNowInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        // TODO: Coded error.
        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(),
            state: getNextAccount(),
            delegate: getNextAccount(),
            depositorTokenAccount: getNextAccount(),
            vault: getNextAccount(),
            mint: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getDepositNowInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=depositNow.js.map