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 { createWithdrawFundsInstruction } from '@metaplex-solarti/mpl-candy-machine';
import { findCandyMachineV2CollectionPda } from '../pdas';
import { TransactionBuilder } from '../../../utils';
import { useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'DeleteCandyMachineV2Operation';
/**
 * Deletes an existing Candy Machine.
 *
 * ```ts
 * await metaplex.candyMachinesV2().delete({ candyMachine });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const deleteCandyMachineV2Operation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const deleteCandyMachineV2OperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return deleteCandyMachineV2Builder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Deletes an existing Candy Machine.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachinesV2()
 *   .builders()
 *   .delete({
 *     candyMachine: { address, collectionMintAddress },
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const deleteCandyMachineV2Builder = (metaplex, params, options = {}) => {
    var _a, _b;
    const { payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const authority = (_a = params.authority) !== null && _a !== void 0 ? _a : metaplex.identity();
    const { candyMachine } = params;
    const deleteInstruction = createWithdrawFundsInstruction({
        candyMachine: candyMachine.address,
        authority: authority.publicKey,
    });
    if (candyMachine.collectionMintAddress) {
        const collectionPda = findCandyMachineV2CollectionPda(candyMachine.address);
        deleteInstruction.keys.push({
            pubkey: collectionPda,
            isWritable: true,
            isSigner: false,
        });
    }
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: deleteInstruction,
        signers: [authority],
        key: (_b = params.instructionKey) !== null && _b !== void 0 ? _b : 'withdrawFunds',
    });
};
//# sourceMappingURL=deleteCandyMachineV2.js.map