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 { createWithdrawInstruction } from '@metaplex-foundation/mpl-candy-machine-core';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'DeleteCandyMachineOperation';
/**
 * Deletes a Candy Machine account by withdrawing its rent-exempt balance.
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .delete({
 *     candyMachine: candyMachine.address,
 *     candyGuard: candyMachine.candyGuard.address,
 *     authority,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const deleteCandyMachineOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const deleteCandyMachineOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return deleteCandyMachineBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Deletes a Candy Machine account by withdrawing its rent-exempt balance.
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .candyMachines()
 *   .builders()
 *   .delete({
 *     candyMachine: candyMachine.address,
 *     candyGuard: candyMachine.candyGuard.address,
 *     authority,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const deleteCandyMachineBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { candyMachine, candyGuard, authority = metaplex.identity(), candyGuardAuthority = authority, } = params;
    const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
    const builder = TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createWithdrawInstruction({
            candyMachine,
            authority: authority.publicKey,
        }, candyMachineProgram.address),
        signers: [authority],
        key: (_a = params.deleteCandyMachineInstructionKey) !== null && _a !== void 0 ? _a : 'deleteCandyMachine',
    });
    if (candyGuard) {
        builder.add(metaplex
            .candyMachines()
            .builders()
            .deleteCandyGuard({ candyGuard, authority: candyGuardAuthority }, { payer, programs }));
    }
    return builder;
};
//# sourceMappingURL=deleteCandyMachine.js.map