"use strict";
/**
 * 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEPOSIT_NOW_DISCRIMINATOR = void 0;
exports.getDepositNowDiscriminatorBytes = getDepositNowDiscriminatorBytes;
exports.getDepositNowInstructionDataEncoder = getDepositNowInstructionDataEncoder;
exports.getDepositNowInstructionDataDecoder = getDepositNowInstructionDataDecoder;
exports.getDepositNowInstructionDataCodec = getDepositNowInstructionDataCodec;
exports.getDepositNowInstructionAsync = getDepositNowInstructionAsync;
exports.getDepositNowInstruction = getDepositNowInstruction;
exports.parseDepositNowInstruction = parseDepositNowInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.DEPOSIT_NOW_DISCRIMINATOR = new Uint8Array([
    75, 228, 135, 221, 200, 25, 148, 26,
]);
function getDepositNowDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.DEPOSIT_NOW_DISCRIMINATOR);
}
function getDepositNowInstructionDataEncoder() {
    return (0, web3_v2_js_1.transformEncoder)((0, web3_v2_js_1.getStructEncoder)([
        ['discriminator', (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8)],
        ['depositor', (0, web3_v2_js_1.getAddressEncoder)()],
        ['recipient', (0, web3_v2_js_1.getAddressEncoder)()],
        ['inputToken', (0, web3_v2_js_1.getAddressEncoder)()],
        ['outputToken', (0, web3_v2_js_1.getAddressEncoder)()],
        ['inputAmount', (0, web3_v2_js_1.getU64Encoder)()],
        ['outputAmount', (0, web3_v2_js_1.getU64Encoder)()],
        ['destinationChainId', (0, web3_v2_js_1.getU64Encoder)()],
        ['exclusiveRelayer', (0, web3_v2_js_1.getAddressEncoder)()],
        ['fillDeadlineOffset', (0, web3_v2_js_1.getU32Encoder)()],
        ['exclusivityParameter', (0, web3_v2_js_1.getU32Encoder)()],
        ['message', (0, web3_v2_js_1.addEncoderSizePrefix)((0, web3_v2_js_1.getBytesEncoder)(), (0, web3_v2_js_1.getU32Encoder)())],
    ]), (value) => ({ ...value, discriminator: exports.DEPOSIT_NOW_DISCRIMINATOR }));
}
function getDepositNowInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
        ['depositor', (0, web3_v2_js_1.getAddressDecoder)()],
        ['recipient', (0, web3_v2_js_1.getAddressDecoder)()],
        ['inputToken', (0, web3_v2_js_1.getAddressDecoder)()],
        ['outputToken', (0, web3_v2_js_1.getAddressDecoder)()],
        ['inputAmount', (0, web3_v2_js_1.getU64Decoder)()],
        ['outputAmount', (0, web3_v2_js_1.getU64Decoder)()],
        ['destinationChainId', (0, web3_v2_js_1.getU64Decoder)()],
        ['exclusiveRelayer', (0, web3_v2_js_1.getAddressDecoder)()],
        ['fillDeadlineOffset', (0, web3_v2_js_1.getU32Decoder)()],
        ['exclusivityParameter', (0, web3_v2_js_1.getU32Decoder)()],
        ['message', (0, web3_v2_js_1.addDecoderSizePrefix)((0, web3_v2_js_1.getBytesDecoder)(), (0, web3_v2_js_1.getU32Decoder)())],
    ]);
}
function getDepositNowInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getDepositNowInstructionDataEncoder(), getDepositNowInstructionDataDecoder());
}
async function getDepositNowInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        route: { value: input.route ?? null, isWritable: false },
        depositorTokenAccount: {
            value: input.depositorTokenAccount ?? null,
            isWritable: true,
        },
        vault: { value: input.vault ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.depositorTokenAccount.value) {
        accounts.depositorTokenAccount.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectSome)(args.depositor)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
            ],
        });
    }
    if (!accounts.vault.value) {
        accounts.vault.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.state.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.tokenProgram.value)),
                (0, web3_v2_js_1.getAddressEncoder)().encode((0, shared_1.expectAddress)(accounts.mint.value)),
            ],
        });
    }
    if (!accounts.eventAuthority.value) {
        accounts.eventAuthority.value = await (0, web3_v2_js_1.getProgramDerivedAddress)({
            programAddress,
            seeds: [
                (0, web3_v2_js_1.getBytesEncoder)().encode(new Uint8Array([
                    95, 95, 101, 118, 101, 110, 116, 95, 97, 117, 116, 104, 111, 114,
                    105, 116, 121,
                ])),
            ],
        });
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.route),
            getAccountMeta(accounts.depositorTokenAccount),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getDepositNowInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getDepositNowInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        route: { value: input.route ?? null, isWritable: false },
        depositorTokenAccount: {
            value: input.depositorTokenAccount ?? null,
            isWritable: true,
        },
        vault: { value: input.vault ?? null, isWritable: true },
        mint: { value: input.mint ?? null, isWritable: false },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        eventAuthority: { value: input.eventAuthority ?? null, isWritable: false },
        program: { value: input.program ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // Original args.
    const args = { ...input };
    // Resolve default values.
    if (!accounts.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.route),
            getAccountMeta(accounts.depositorTokenAccount),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.mint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getDepositNowInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseDepositNowInstruction(instruction) {
    if (instruction.accounts.length < 9) {
        // TODO: Coded error.
        throw new Error('Not enough accounts');
    }
    let accountIndex = 0;
    const getNextAccount = () => {
        const accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: {
            signer: getNextAccount(),
            state: getNextAccount(),
            route: getNextAccount(),
            depositorTokenAccount: getNextAccount(),
            vault: getNextAccount(),
            mint: getNextAccount(),
            tokenProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getDepositNowInstructionDataDecoder().decode(instruction.data),
    };
}
