"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.SET_ENABLE_ROUTE_DISCRIMINATOR = void 0;
exports.getSetEnableRouteDiscriminatorBytes = getSetEnableRouteDiscriminatorBytes;
exports.getSetEnableRouteInstructionDataEncoder = getSetEnableRouteInstructionDataEncoder;
exports.getSetEnableRouteInstructionDataDecoder = getSetEnableRouteInstructionDataDecoder;
exports.getSetEnableRouteInstructionDataCodec = getSetEnableRouteInstructionDataCodec;
exports.getSetEnableRouteInstructionAsync = getSetEnableRouteInstructionAsync;
exports.getSetEnableRouteInstruction = getSetEnableRouteInstruction;
exports.parseSetEnableRouteInstruction = parseSetEnableRouteInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.SET_ENABLE_ROUTE_DISCRIMINATOR = new Uint8Array([
    166, 66, 33, 156, 122, 251, 223, 34,
]);
function getSetEnableRouteDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.SET_ENABLE_ROUTE_DISCRIMINATOR);
}
function getSetEnableRouteInstructionDataEncoder() {
    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)],
        ['originToken', (0, web3_v2_js_1.getAddressEncoder)()],
        ['destinationChainId', (0, web3_v2_js_1.getU64Encoder)()],
        ['enabled', (0, web3_v2_js_1.getBooleanEncoder)()],
    ]), (value) => ({ ...value, discriminator: exports.SET_ENABLE_ROUTE_DISCRIMINATOR }));
}
function getSetEnableRouteInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
        ['originToken', (0, web3_v2_js_1.getAddressDecoder)()],
        ['destinationChainId', (0, web3_v2_js_1.getU64Decoder)()],
        ['enabled', (0, web3_v2_js_1.getBooleanDecoder)()],
    ]);
}
function getSetEnableRouteInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getSetEnableRouteInstructionDataEncoder(), getSetEnableRouteInstructionDataDecoder());
}
async function getSetEnableRouteInstructionAsync(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: false },
        route: { value: input.route ?? null, isWritable: true },
        vault: { value: input.vault ?? null, isWritable: true },
        originTokenMint: {
            value: input.originTokenMint ?? null,
            isWritable: false,
        },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        associatedTokenProgram: {
            value: input.associatedTokenProgram ?? null,
            isWritable: false,
        },
        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.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    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.originTokenMint.value)),
            ],
        });
    }
    if (!accounts.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
    }
    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.route),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.originTokenMint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getSetEnableRouteInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function getSetEnableRouteInstruction(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: false },
        route: { value: input.route ?? null, isWritable: true },
        vault: { value: input.vault ?? null, isWritable: true },
        originTokenMint: {
            value: input.originTokenMint ?? null,
            isWritable: false,
        },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        associatedTokenProgram: {
            value: input.associatedTokenProgram ?? null,
            isWritable: false,
        },
        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.tokenProgram.value) {
        accounts.tokenProgram.value =
            'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
    }
    if (!accounts.associatedTokenProgram.value) {
        accounts.associatedTokenProgram.value =
            'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
    }
    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.route),
            getAccountMeta(accounts.vault),
            getAccountMeta(accounts.originTokenMint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
            getAccountMeta(accounts.eventAuthority),
            getAccountMeta(accounts.program),
        ],
        programAddress,
        data: getSetEnableRouteInstructionDataEncoder().encode(args),
    };
    return instruction;
}
function parseSetEnableRouteInstruction(instruction) {
    if (instruction.accounts.length < 11) {
        // 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(),
            route: getNextAccount(),
            vault: getNextAccount(),
            originTokenMint: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
            eventAuthority: getNextAccount(),
            program: getNextAccount(),
        },
        data: getSetEnableRouteInstructionDataDecoder().decode(instruction.data),
    };
}
