"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.RELAY_ROOT_BUNDLE_DISCRIMINATOR = void 0;
exports.getRelayRootBundleDiscriminatorBytes = getRelayRootBundleDiscriminatorBytes;
exports.getRelayRootBundleInstructionDataEncoder = getRelayRootBundleInstructionDataEncoder;
exports.getRelayRootBundleInstructionDataDecoder = getRelayRootBundleInstructionDataDecoder;
exports.getRelayRootBundleInstructionDataCodec = getRelayRootBundleInstructionDataCodec;
exports.getRelayRootBundleInstructionAsync = getRelayRootBundleInstructionAsync;
exports.getRelayRootBundleInstruction = getRelayRootBundleInstruction;
exports.parseRelayRootBundleInstruction = parseRelayRootBundleInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR = new Uint8Array([
    69, 13, 223, 204, 251, 61, 105, 6,
]);
function getRelayRootBundleDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR);
}
function getRelayRootBundleInstructionDataEncoder() {
    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)],
        ['relayerRefundRoot', (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 32)],
        ['slowRelayRoot', (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 32)],
    ]), (value) => ({ ...value, discriminator: exports.RELAY_ROOT_BUNDLE_DISCRIMINATOR }));
}
function getRelayRootBundleInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
        ['relayerRefundRoot', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 32)],
        ['slowRelayRoot', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 32)],
    ]);
}
function getRelayRootBundleInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getRelayRootBundleInstructionDataEncoder(), getRelayRootBundleInstructionDataDecoder());
}
async function getRelayRootBundleInstructionAsync(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        payer: { value: input.payer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        rootBundle: { value: input.rootBundle ?? null, isWritable: true },
        systemProgram: { value: input.systemProgram ?? 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.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    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.payer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.rootBundle),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getRelayRootBundleInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getRelayRootBundleInstruction(input, config) {
    // Program address.
    const programAddress = config?.programAddress ?? programs_1.SVM_SPOKE_PROGRAM_ADDRESS;
    // Original accounts.
    const originalAccounts = {
        signer: { value: input.signer ?? null, isWritable: false },
        payer: { value: input.payer ?? null, isWritable: true },
        state: { value: input.state ?? null, isWritable: true },
        rootBundle: { value: input.rootBundle ?? null, isWritable: true },
        systemProgram: { value: input.systemProgram ?? 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.systemProgram.value) {
        accounts.systemProgram.value =
            '11111111111111111111111111111111';
    }
    const getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, 'programId');
    const instruction = {
        accounts: [
            getAccountMeta(accounts.signer),
            getAccountMeta(accounts.payer),
            getAccountMeta(accounts.state),
            getAccountMeta(accounts.rootBundle),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getRelayRootBundleInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseRelayRootBundleInstruction(instruction) {
    if (instruction.accounts.length < 7) {
        // 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(),
            payer: getNextAccount(),
            state: getNextAccount(),
            rootBundle: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getRelayRootBundleInstructionDataDecoder().decode(instruction.data),
    };
}
