"use strict";
/**
 * This code was AUTOGENERATED using the kinobi library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun kinobi to update it.
 *
 * @see https://github.com/metaplex-foundation/kinobi
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTokenIfMissing = exports.getCreateTokenIfMissingInstructionDataSerializer = void 0;
const umi_1 = require("@metaplex-foundation/umi");
const serializers_1 = require("@metaplex-foundation/umi/serializers");
const hooked_1 = require("../../hooked");
const shared_1 = require("../shared");
function getCreateTokenIfMissingInstructionDataSerializer() {
    return (0, serializers_1.mapSerializer)((0, serializers_1.struct)([['discriminator', (0, serializers_1.u8)()]], {
        description: 'CreateTokenIfMissingInstructionData',
    }), (value) => ({ ...value, discriminator: 0 }));
}
exports.getCreateTokenIfMissingInstructionDataSerializer = getCreateTokenIfMissingInstructionDataSerializer;
// Instruction.
function createTokenIfMissing(context, input) {
    // Program ID.
    const programId = context.programs.getPublicKey('mplTokenExtras', 'TokExjvjJmhKaRBShsBAsbSvEWMA1AgUNK7ps4SAc2p');
    // Accounts.
    const resolvedAccounts = {
        payer: { index: 0, isWritable: true, value: input.payer ?? null },
        token: { index: 1, isWritable: false, value: input.token ?? null },
        mint: { index: 2, isWritable: false, value: input.mint ?? null },
        owner: { index: 3, isWritable: false, value: input.owner ?? null },
        ata: { index: 4, isWritable: true, value: input.ata ?? null },
        systemProgram: {
            index: 5,
            isWritable: false,
            value: input.systemProgram ?? null,
        },
        tokenProgram: {
            index: 6,
            isWritable: false,
            value: input.tokenProgram ?? null,
        },
        ataProgram: {
            index: 7,
            isWritable: false,
            value: input.ataProgram ?? null,
        },
    };
    // Default values.
    if (!resolvedAccounts.payer.value) {
        resolvedAccounts.payer.value = context.payer;
    }
    if (!resolvedAccounts.owner.value) {
        resolvedAccounts.owner.value = context.identity.publicKey;
    }
    if (!resolvedAccounts.tokenProgram.value) {
        resolvedAccounts.tokenProgram.value = context.programs.getPublicKey('splToken', 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
        resolvedAccounts.tokenProgram.isWritable = false;
    }
    if (!resolvedAccounts.ata.value) {
        resolvedAccounts.ata.value = (0, hooked_1.findAssociatedTokenPda)(context, {
            owner: (0, shared_1.expectPublicKey)(resolvedAccounts.owner.value),
            mint: (0, shared_1.expectPublicKey)(resolvedAccounts.mint.value),
            tokenProgramId: (0, shared_1.expectPublicKey)(resolvedAccounts.tokenProgram.value),
        });
    }
    if (!resolvedAccounts.token.value) {
        resolvedAccounts.token.value = (0, shared_1.expectSome)(resolvedAccounts.ata.value);
    }
    if (!resolvedAccounts.systemProgram.value) {
        resolvedAccounts.systemProgram.value = context.programs.getPublicKey('splSystem', '11111111111111111111111111111111');
        resolvedAccounts.systemProgram.isWritable = false;
    }
    if (!resolvedAccounts.ataProgram.value) {
        resolvedAccounts.ataProgram.value = context.programs.getPublicKey('splAssociatedToken', 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
        resolvedAccounts.ataProgram.isWritable = false;
    }
    // Accounts in order.
    const orderedAccounts = Object.values(resolvedAccounts).sort((a, b) => a.index - b.index);
    // Keys and Signers.
    const [keys, signers] = (0, shared_1.getAccountMetasAndSigners)(orderedAccounts, 'programId', programId);
    // Data.
    const data = getCreateTokenIfMissingInstructionDataSerializer().serialize({});
    // Bytes Created On Chain.
    const bytesCreatedOnChain = 0;
    return (0, umi_1.transactionBuilder)([
        { instruction: { keys, programId, data }, signers, bytesCreatedOnChain },
    ]);
}
exports.createTokenIfMissing = createTokenIfMissing;
//# sourceMappingURL=createTokenIfMissing.js.map