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 { createWrapInstruction } from '@metaplex-foundation/mpl-candy-guard';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'WrapCandyGuardOperation';
/**
 * Wraps the given Candy Machine in a Candy Guard.
 *
 * This makes the Candy Guard the mint authority for the Candy Machine
 * which means all minting will have to go through the Candy Guard.
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .wrapCandyGuard({
 *     candyMachine,
 *     candyGuard,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const wrapCandyGuardOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const wrapCandyGuardOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return wrapCandyGuardBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Wraps the given Candy Machine in a Candy Guard.
 *
 * This makes the Candy Guard the mint authority for the Candy Machine
 * which means all minting will have to go through the Candy Guard.
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .candyMachines()
 *   .builders()
 *   .wrapCandyGuard({
 *     candyMachine,
 *     candyGuard,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const wrapCandyGuardBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { candyGuard, candyGuardAuthority = metaplex.identity(), candyMachine, candyMachineAuthority = metaplex.identity(), } = params;
    const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
    const candyGuardProgram = metaplex.programs().getCandyGuard(programs);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createWrapInstruction({
            candyGuard,
            authority: candyGuardAuthority.publicKey,
            candyMachine,
            candyMachineProgram: candyMachineProgram.address,
            candyMachineAuthority: candyMachineAuthority.publicKey,
        }, candyGuardProgram.address),
        signers: [candyGuardAuthority, candyMachineAuthority],
        key: (_a = params.wrapCandyGuardInstructionKey) !== null && _a !== void 0 ? _a : 'wrapCandyGuard',
    });
};
//# sourceMappingURL=wrapCandyGuard.js.map