import { createMintToInstruction } from '@solarti/spl-token';
import { useOperation } from '../../../types/Operation.mjs';
import { toPublicKey } from '../../../types/PublicKey.mjs';
import { isSigner } from '../../../types/Signer.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

// -----------------
// Operation
// -----------------

const Key = 'MintTokensOperation';

/**
 * Mint tokens to an account.
 *
 * ```ts
 * await metaplex
 *   .tokens()
 *   .mint({
 *     mintAddress,
 *     toOwner,
 *     amount: token(100),
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const mintTokensOperation = useOperation(Key);

/**
 * @group Operations
 * @category Types
 */

/**
 * @group Operations
 * @category Handlers
 */
const mintTokensOperationHandler = {
  async handle(operation, metaplex, scope) {
    const {
      programs,
      confirmOptions
    } = scope;
    const {
      mintAddress,
      toOwner = metaplex.identity().publicKey,
      toToken
    } = operation.input;
    const destination = toToken ?? metaplex.tokens().pdas().associatedTokenAccount({
      mint: mintAddress,
      owner: toOwner,
      programs
    });
    const destinationAddress = toPublicKey(destination);
    const destinationAccountExists = await metaplex.rpc().accountExists(destinationAddress);
    scope.throwIfCanceled();
    const builder = await mintTokensBuilder(metaplex, {
      ...operation.input,
      toTokenExists: destinationAccountExists
    }, scope);
    scope.throwIfCanceled();
    return builder.sendAndConfirm(metaplex, confirmOptions);
  }
};

// -----------------
// Builder
// -----------------

/**
 * @group Transaction Builders
 * @category Inputs
 */

/**
 * Mint tokens to an account.
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .tokens()
 *   .builders()
 *   .mint({
 *     mintAddress,
 *     toOwner,
 *     amount: token(100),
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const mintTokensBuilder = async (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    amount,
    toOwner = metaplex.identity().publicKey,
    toToken,
    toTokenExists = true,
    mintAuthority = metaplex.identity(),
    multiSigners = []
  } = params;
  const [mintAuthorityPublicKey, signers] = isSigner(mintAuthority) ? [mintAuthority.publicKey, [mintAuthority]] : [mintAuthority, multiSigners];
  const tokenProgram = metaplex.programs().getToken(programs);
  const destination = toToken ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: toOwner,
    programs
  });
  return TransactionBuilder.make()

  // Create token account if missing.
  .add(await metaplex.tokens().builders().createTokenIfMissing({
    ...params,
    mint: mintAddress,
    owner: toOwner,
    token: toToken,
    tokenExists: toTokenExists,
    tokenVariable: 'toToken'
  }, {
    payer,
    programs
  }))

  // Mint tokens.
  .add({
    instruction: createMintToInstruction(mintAddress, toPublicKey(destination), mintAuthorityPublicKey, amount.basisPoints.toNumber(), multiSigners, tokenProgram.address),
    signers,
    key: params.mintTokensInstructionKey ?? 'mintTokens'
  });
};

export { mintTokensBuilder, mintTokensOperation, mintTokensOperationHandler };
//# sourceMappingURL=mintTokens.mjs.map
