'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var mplTokenMetadata = require('@metaplex-solarti/mpl-token-metadata');
var Operation = require('../../../types/Operation.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

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

const Key = 'DeleteNftOperation';

/**
 * Deletes an existing NFT.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .delete({ mintAddress };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const deleteNftOperation = Operation.useOperation(Key);

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

/**
 * @group Operations
 * @category Handlers
 */
const deleteNftOperationHandler = {
  handle: async (operation, metaplex, scope) => {
    return deleteNftBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
  }
};

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

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

/**
 * Deletes an existing NFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .delete({ mintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const deleteNftBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    owner = metaplex.identity(),
    ownerTokenAccount,
    collection
  } = params;
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
  const metadata = metaplex.nfts().pdas().metadata({
    mint: mintAddress,
    programs
  });
  const edition = metaplex.nfts().pdas().masterEdition({
    mint: mintAddress,
    programs
  });
  const tokenAddress = ownerTokenAccount ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: owner.publicKey,
    programs
  });
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: mplTokenMetadata.createBurnNftInstruction({
      metadata,
      owner: owner.publicKey,
      mint: mintAddress,
      tokenAccount: tokenAddress,
      masterEditionAccount: edition,
      splTokenProgram: tokenProgram.address,
      collectionMetadata: collection ? metaplex.nfts().pdas().metadata({
        mint: collection,
        programs
      }) : undefined
    }, tokenMetadataProgram.address),
    signers: [owner],
    key: params.instructionKey ?? 'deleteNft'
  });
};

exports.deleteNftBuilder = deleteNftBuilder;
exports.deleteNftOperation = deleteNftOperation;
exports.deleteNftOperationHandler = deleteNftOperationHandler;
//# sourceMappingURL=deleteNft.cjs.map
