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 { createUnwrapInstruction } from '@metaplex-foundation/mpl-candy-guard';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'UnwrapCandyGuardOperation';
/**
 * Unwraps the given Candy Machine from its Candy Guard.
 *
 * This makes the Candy Machine authority its own mint authority again
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .unwrapCandyGuard({
 *     candyMachine,
 *     candyGuard,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const unwrapCandyGuardOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const unwrapCandyGuardOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return unwrapCandyGuardBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Unwraps the given Candy Machine from its Candy Guard.
 *
 * This makes the Candy Machine authority its own mint authority again
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .candyMachines()
 *   .builders()
 *   .unwrapCandyGuard({
 *     candyMachine,
 *     candyGuard,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const unwrapCandyGuardBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { candyGuard, candyGuardAuthority = metaplex.identity(), candyMachine, candyMachineAuthority = metaplex.identity(), } = params;
    // Programs.
    const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
    const candyGuardProgram = metaplex.programs().getCandyGuard(programs);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createUnwrapInstruction({
            candyGuard,
            authority: candyGuardAuthority.publicKey,
            candyMachine,
            candyMachineProgram: candyMachineProgram.address,
            candyMachineAuthority: candyMachineAuthority.publicKey,
        }, candyGuardProgram.address),
        signers: [candyGuardAuthority, candyMachineAuthority],
        key: (_a = params.unwrapCandyGuardInstructionKey) !== null && _a !== void 0 ? _a : 'unwrapCandyGuard',
    });
};
//# sourceMappingURL=unwrapCandyGuard.js.map