"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.CREATE_TOKEN_ACCOUNTS_DISCRIMINATOR = void 0;
exports.getCreateTokenAccountsDiscriminatorBytes = getCreateTokenAccountsDiscriminatorBytes;
exports.getCreateTokenAccountsInstructionDataEncoder = getCreateTokenAccountsInstructionDataEncoder;
exports.getCreateTokenAccountsInstructionDataDecoder = getCreateTokenAccountsInstructionDataDecoder;
exports.getCreateTokenAccountsInstructionDataCodec = getCreateTokenAccountsInstructionDataCodec;
exports.getCreateTokenAccountsInstruction = getCreateTokenAccountsInstruction;
exports.parseCreateTokenAccountsInstruction = parseCreateTokenAccountsInstruction;
const web3_v2_js_1 = require("@solana/web3-v2.js");
const programs_1 = require("../programs");
const shared_1 = require("../shared");
exports.CREATE_TOKEN_ACCOUNTS_DISCRIMINATOR = new Uint8Array([
    163, 216, 49, 204, 97, 16, 80, 167,
]);
function getCreateTokenAccountsDiscriminatorBytes() {
    return (0, web3_v2_js_1.fixEncoderSize)((0, web3_v2_js_1.getBytesEncoder)(), 8).encode(exports.CREATE_TOKEN_ACCOUNTS_DISCRIMINATOR);
}
function getCreateTokenAccountsInstructionDataEncoder() {
    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)]]), (value) => ({
        ...value,
        discriminator: exports.CREATE_TOKEN_ACCOUNTS_DISCRIMINATOR,
    }));
}
function getCreateTokenAccountsInstructionDataDecoder() {
    return (0, web3_v2_js_1.getStructDecoder)([
        ['discriminator', (0, web3_v2_js_1.fixDecoderSize)((0, web3_v2_js_1.getBytesDecoder)(), 8)],
    ]);
}
function getCreateTokenAccountsInstructionDataCodec() {
    return (0, web3_v2_js_1.combineCodec)(getCreateTokenAccountsInstructionDataEncoder(), getCreateTokenAccountsInstructionDataDecoder());
}
function getCreateTokenAccountsInstruction(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 },
        mint: { value: input.mint ?? null, isWritable: false },
        tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
        associatedTokenProgram: {
            value: input.associatedTokenProgram ?? null,
            isWritable: false,
        },
        systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    };
    const accounts = originalAccounts;
    // 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.mint),
            getAccountMeta(accounts.tokenProgram),
            getAccountMeta(accounts.associatedTokenProgram),
            getAccountMeta(accounts.systemProgram),
        ],
        programAddress,
        data: getCreateTokenAccountsInstructionDataEncoder().encode({}),
    };
    return instruction;
}
function parseCreateTokenAccountsInstruction(instruction) {
    if (instruction.accounts.length < 5) {
        // 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(),
            mint: getNextAccount(),
            tokenProgram: getNextAccount(),
            associatedTokenProgram: getNextAccount(),
            systemProgram: getNextAccount(),
        },
        data: getCreateTokenAccountsInstructionDataDecoder().decode(instruction.data),
    };
}
