var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { createInitializeMintInstruction, MINT_SIZE } from '@solarti/spl-token';
import { Keypair } from '@solarti/web3.js';
import { TransactionBuilder } from '../../../utils';
import { makeConfirmOptionsFinalizedOnMainnet, useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'CreateMintOperation';
/**
 * Creates a new mint account.
 *
 * ```ts
 * const { mint } = await metaplex.tokens().createMint();
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const createMintOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const createMintOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            const builder = yield createMintBuilder(metaplex, operation.input, scope);
            scope.throwIfCanceled();
            const confirmOptions = makeConfirmOptionsFinalizedOnMainnet(metaplex, scope.confirmOptions);
            const output = yield builder.sendAndConfirm(metaplex, confirmOptions);
            scope.throwIfCanceled();
            const mint = yield metaplex
                .tokens()
                .findMintByAddress({ address: output.mintSigner.publicKey }, scope);
            return Object.assign(Object.assign({}, output), { mint });
        });
    },
};
/**
 * Creates a new mint account.
 *
 * ```ts
 * const transactionBuilder = await metaplex.tokens().builders().createMint();
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const createMintBuilder = (metaplex, params, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { decimals = 0, mint = Keypair.generate(), mintAuthority = metaplex.identity().publicKey, freezeAuthority = mintAuthority, } = params;
    const tokenProgram = metaplex.programs().getToken(programs);
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        .setContext({ mintSigner: mint })
        // Create an empty account for the mint.
        .add(yield metaplex
        .system()
        .builders()
        .createAccount({
        newAccount: mint,
        space: MINT_SIZE,
        program: tokenProgram.address,
        instructionKey: (_a = params.createAccountInstructionKey) !== null && _a !== void 0 ? _a : 'createAccount',
    }, { payer, programs }))
        // Initialize the mint.
        .add({
        instruction: createInitializeMintInstruction(mint.publicKey, decimals, mintAuthority, freezeAuthority, tokenProgram.address),
        signers: [mint],
        key: (_b = params.initializeMintInstructionKey) !== null && _b !== void 0 ? _b : 'initializeMint',
    }));
});
//# sourceMappingURL=createMint.js.map