"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.createV1 = exports.getCreateV1InstructionDataSerializer = void 0;
const umi_1 = require("@metaplex-foundation/umi");
const serializers_1 = require("@metaplex-foundation/umi/serializers");
const hooked_1 = require("../../hooked");
const accounts_1 = require("../accounts");
const shared_1 = require("../shared");
const types_1 = require("../types");
function getCreateV1InstructionDataSerializer() {
    return (0, serializers_1.mapSerializer)((0, serializers_1.struct)([
        ['discriminator', (0, serializers_1.u8)()],
        ['createV1Discriminator', (0, serializers_1.u8)()],
        ['name', (0, serializers_1.string)()],
        ['symbol', (0, serializers_1.string)()],
        ['uri', (0, serializers_1.string)()],
        ['sellerFeeBasisPoints', (0, umi_1.mapAmountSerializer)((0, serializers_1.u16)(), '%', 2)],
        ['creators', (0, serializers_1.option)((0, serializers_1.array)((0, types_1.getCreatorSerializer)()))],
        ['primarySaleHappened', (0, serializers_1.bool)()],
        ['isMutable', (0, serializers_1.bool)()],
        ['tokenStandard', (0, types_1.getTokenStandardSerializer)()],
        ['collection', (0, serializers_1.option)((0, types_1.getCollectionSerializer)())],
        ['uses', (0, serializers_1.option)((0, types_1.getUsesSerializer)())],
        ['collectionDetails', (0, serializers_1.option)((0, types_1.getCollectionDetailsSerializer)())],
        ['ruleSet', (0, serializers_1.option)((0, serializers_1.publicKey)())],
        ['decimals', (0, serializers_1.option)((0, serializers_1.u8)())],
        ['printSupply', (0, serializers_1.option)((0, types_1.getPrintSupplySerializer)())],
    ], { description: 'CreateV1InstructionData' }), (value) => ({
        ...value,
        discriminator: 42,
        createV1Discriminator: 0,
        symbol: value.symbol ?? '',
        primarySaleHappened: value.primarySaleHappened ?? false,
        isMutable: value.isMutable ?? true,
        tokenStandard: value.tokenStandard ?? types_1.TokenStandard.NonFungible,
        collection: value.collection ?? (0, umi_1.none)(),
        uses: value.uses ?? (0, umi_1.none)(),
        ruleSet: value.ruleSet ?? (0, umi_1.none)(),
    }));
}
exports.getCreateV1InstructionDataSerializer = getCreateV1InstructionDataSerializer;
// Instruction.
function createV1(context, input) {
    // Program ID.
    const programId = context.programs.getPublicKey('mplTokenMetadata', 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
    // Accounts.
    const resolvedAccounts = {
        metadata: {
            index: 0,
            isWritable: true,
            value: input.metadata ?? null,
        },
        masterEdition: {
            index: 1,
            isWritable: true,
            value: input.masterEdition ?? null,
        },
        mint: { index: 2, isWritable: true, value: input.mint ?? null },
        authority: {
            index: 3,
            isWritable: false,
            value: input.authority ?? null,
        },
        payer: {
            index: 4,
            isWritable: true,
            value: input.payer ?? null,
        },
        updateAuthority: {
            index: 5,
            isWritable: false,
            value: input.updateAuthority ?? null,
        },
        systemProgram: {
            index: 6,
            isWritable: false,
            value: input.systemProgram ?? null,
        },
        sysvarInstructions: {
            index: 7,
            isWritable: false,
            value: input.sysvarInstructions ?? null,
        },
        splTokenProgram: {
            index: 8,
            isWritable: false,
            value: input.splTokenProgram ?? null,
        },
    };
    // Arguments.
    const resolvedArgs = { ...input };
    // Default values.
    if (!resolvedAccounts.metadata.value) {
        resolvedAccounts.metadata.value = (0, accounts_1.findMetadataPda)(context, {
            mint: (0, shared_1.expectPublicKey)(resolvedAccounts.mint.value),
        });
    }
    if (!resolvedArgs.tokenStandard) {
        resolvedArgs.tokenStandard = types_1.TokenStandard.NonFungible;
    }
    if (!resolvedAccounts.masterEdition.value) {
        if ((0, hooked_1.resolveIsNonFungible)(context, resolvedAccounts, resolvedArgs, programId, true)) {
            resolvedAccounts.masterEdition.value = (0, accounts_1.findMasterEditionPda)(context, {
                mint: (0, shared_1.expectPublicKey)(resolvedAccounts.mint.value),
            });
        }
    }
    if (!resolvedAccounts.authority.value) {
        resolvedAccounts.authority.value = context.identity;
    }
    if (!resolvedAccounts.payer.value) {
        resolvedAccounts.payer.value = context.payer;
    }
    if (!resolvedAccounts.updateAuthority.value) {
        resolvedAccounts.updateAuthority.value = (0, shared_1.expectSome)(resolvedAccounts.authority.value);
    }
    if (!resolvedAccounts.systemProgram.value) {
        resolvedAccounts.systemProgram.value = context.programs.getPublicKey('splSystem', '11111111111111111111111111111111');
        resolvedAccounts.systemProgram.isWritable = false;
    }
    if (!resolvedAccounts.sysvarInstructions.value) {
        resolvedAccounts.sysvarInstructions.value = (0, umi_1.publicKey)('Sysvar1nstructions1111111111111111111111111');
    }
    if (!resolvedAccounts.splTokenProgram.value) {
        if ((0, hooked_1.resolveIsNonFungibleOrIsMintSigner)(context, resolvedAccounts, resolvedArgs, programId, false)) {
            resolvedAccounts.splTokenProgram.value = context.programs.getPublicKey('splToken', 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
            resolvedAccounts.splTokenProgram.isWritable = false;
        }
    }
    if (!resolvedArgs.creators) {
        resolvedArgs.creators = (0, hooked_1.resolveCreators)(context, resolvedAccounts, resolvedArgs, programId, false);
    }
    if (!resolvedArgs.isCollection) {
        resolvedArgs.isCollection = false;
    }
    if (!resolvedArgs.collectionDetails) {
        resolvedArgs.collectionDetails = (0, hooked_1.resolveCollectionDetails)(context, resolvedAccounts, resolvedArgs, programId, false);
    }
    if (!resolvedArgs.decimals) {
        resolvedArgs.decimals = (0, hooked_1.resolveDecimals)(context, resolvedAccounts, resolvedArgs, programId, false);
    }
    if (!resolvedArgs.printSupply) {
        resolvedArgs.printSupply = (0, hooked_1.resolvePrintSupply)(context, resolvedAccounts, resolvedArgs, programId, 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 = getCreateV1InstructionDataSerializer().serialize(resolvedArgs);
    // Bytes Created On Chain.
    const bytesCreatedOnChain = (0, hooked_1.resolveCreateV1Bytes)(context, resolvedAccounts, resolvedArgs, programId);
    return (0, umi_1.transactionBuilder)([
        { instruction: { keys, programId, data }, signers, bytesCreatedOnChain },
    ]);
}
exports.createV1 = createV1;
//# sourceMappingURL=createV1.js.map