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 { createUpdateInstruction, updateInstructionDiscriminator, } from '@metaplex-foundation/mpl-candy-guard';
import { emptyDefaultCandyGuardSettings, } from '../guards';
import { TransactionBuilder } from '../../../utils';
import { serializeDiscriminator, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'UpdateCandyGuardOperation';
/**
 * Updates an existing Candy Guard account.
 *
 * Note that the provided `guards` and `groups`
 * will replace the existing ones.
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .updateCandyGuard({
 *     candyGuard: candyGuard.address,
 *     guards: {
 *       startDate: { date: toDateTime('2022-09-05T20:00:00.000Z') },
 *       solPayment: { amount: sol(1.5), },
 *       botTax: { lamports: sol(0.01), lastInstruction: true },
 *     },
 *     groups: [],
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const updateCandyGuardOperation = _updateCandyGuardOperation;
// eslint-disable-next-line @typescript-eslint/naming-convention
function _updateCandyGuardOperation(input) {
    return { key: Key, input };
}
_updateCandyGuardOperation.key = Key;
/**
 * @group Operations
 * @category Handlers
 */
export const updateCandyGuardOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return updateCandyGuardBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Updates an existing Candy Guard account.
 *
 * Note that the provided `guards` and `groups`
 * will replace the existing ones.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachines()
 *   .builders()
 *   .updateCandyGuard({
 *     candyGuard: candyGuard.address,
 *     guards: {
 *       startDate: { date: toDateTime('2022-09-05T20:00:00.000Z') },
 *       solPayment: { amount: sol(1.5), },
 *       botTax: { lamports: sol(0.01), lastInstruction: true },
 *     },
 *     groups: [],
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const updateCandyGuardBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { candyGuard, guards, groups, authority = metaplex.identity(), } = params;
    const candyGuardProgram = metaplex.programs().getCandyGuard(programs);
    const updateInstruction = createUpdateInstruction({
        candyGuard,
        authority: authority.publicKey,
        payer: payer.publicKey,
    }, {
        data: {
            default: emptyDefaultCandyGuardSettings,
            groups: null,
        },
    }, candyGuardProgram.address);
    const serializedSettings = metaplex
        .candyMachines()
        .guards()
        .serializeSettings(guards, groups, programs);
    const discriminator = serializeDiscriminator(updateInstructionDiscriminator);
    updateInstruction.data = Buffer.concat([discriminator, serializedSettings]);
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Update the candy guard account.
        .add({
        instruction: updateInstruction,
        signers: [authority, payer],
        key: (_a = params.updateInstructionKey) !== null && _a !== void 0 ? _a : 'updateCandyGuard',
    }));
};
//# sourceMappingURL=updateCandyGuard.js.map