'use strict';

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

var mplCandyMachine = require('@metaplex-solarti/mpl-candy-machine');
var pdas = require('../pdas.cjs');
var Operation = require('../../../types/Operation.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

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

const Key = 'DeleteCandyMachineV2Operation';

/**
 * Deletes an existing Candy Machine.
 *
 * ```ts
 * await metaplex.candyMachinesV2().delete({ candyMachine });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const deleteCandyMachineV2Operation = Operation.useOperation(Key);

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

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

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

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

/**
 * Deletes an existing Candy Machine.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachinesV2()
 *   .builders()
 *   .delete({
 *     candyMachine: { address, collectionMintAddress },
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const deleteCandyMachineV2Builder = (metaplex, params, options = {}) => {
  const {
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const authority = params.authority ?? metaplex.identity();
  const {
    candyMachine
  } = params;
  const deleteInstruction = mplCandyMachine.createWithdrawFundsInstruction({
    candyMachine: candyMachine.address,
    authority: authority.publicKey
  });
  if (candyMachine.collectionMintAddress) {
    const collectionPda = pdas.findCandyMachineV2CollectionPda(candyMachine.address);
    deleteInstruction.keys.push({
      pubkey: collectionPda,
      isWritable: true,
      isSigner: false
    });
  }
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: deleteInstruction,
    signers: [authority],
    key: params.instructionKey ?? 'withdrawFunds'
  });
};

exports.deleteCandyMachineV2Builder = deleteCandyMachineV2Builder;
exports.deleteCandyMachineV2Operation = deleteCandyMachineV2Operation;
exports.deleteCandyMachineV2OperationHandler = deleteCandyMachineV2OperationHandler;
//# sourceMappingURL=deleteCandyMachineV2.cjs.map
