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 { createAddConfigLinesInstruction } from '@metaplex-solarti/mpl-candy-machine';
import { assertAllConfigLineConstraints, assertCanAdd, assertNotFull, } from '../asserts';
import { TransactionBuilder } from '../../../utils';
import { useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'InsertItemsToCandyMachineV2Operation';
/**
 * Insert items into an existing Candy Machine.
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .insertItems({
 *     candyMachine,
 *     items: [
 *       { name: 'My NFT #1', uri: 'https://example.com/nft1' },
 *       { name: 'My NFT #2', uri: 'https://example.com/nft2' },
 *       { name: 'My NFT #3', uri: 'https://example.com/nft3' },
 *     ],
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const insertItemsToCandyMachineV2Operation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const InsertItemsToCandyMachineV2OperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return insertItemsToCandyMachineV2Builder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Insert items into an existing Candy Machine.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachines()
 *   .builders()
 *   .insertItems({ candyMachine, items });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const insertItemsToCandyMachineV2Builder = (metaplex, params, options = {}) => {
    var _a, _b, _c;
    const { payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const authority = (_a = params.authority) !== null && _a !== void 0 ? _a : metaplex.identity();
    const index = (_b = params.index) !== null && _b !== void 0 ? _b : params.candyMachine.itemsLoaded;
    const { items } = params;
    assertNotFull(params.candyMachine, index);
    assertCanAdd(params.candyMachine, index, items.length);
    assertAllConfigLineConstraints(items);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createAddConfigLinesInstruction({
            candyMachine: params.candyMachine.address,
            authority: authority.publicKey,
        }, { index: index.toNumber(), configLines: items }),
        signers: [authority],
        key: (_c = params.instructionKey) !== null && _c !== void 0 ? _c : 'insertItems',
    });
};
//# sourceMappingURL=insertItemsToCandyMachineV2.js.map