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-foundation/mpl-candy-machine-core';
import { assertAllItemConstraints, assertCanAdd, assertNotFull, } from '../asserts';
import { TransactionBuilder } from '../../../utils';
import { useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'InsertCandyMachineItemsOperation';
/**
 * Insert items into an existing Candy Machine.
 *
 * Note that the name and URI of each item should not include
 * the prefixes configured in the config line settings.
 *
 * ```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 insertCandyMachineItemsOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const insertCandyMachineItemsOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return insertCandyMachineItemsBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Insert items into an existing Candy Machine.
 *
 * Note that the name and URI of each item should not include
 * the prefixes configured in the config line settings.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachines()
 *   .builders()
 *   .insertItems({ candyMachine, items });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const insertCandyMachineItemsBuilder = (metaplex, params, options = {}) => {
    var _a, _b, _c;
    const { programs, 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);
    assertAllItemConstraints(params.candyMachine, items);
    // Programs.
    const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createAddConfigLinesInstruction({
            candyMachine: params.candyMachine.address,
            authority: authority.publicKey,
        }, { index, configLines: items }, candyMachineProgram.address),
        signers: [authority],
        key: (_c = params.instructionKey) !== null && _c !== void 0 ? _c : 'insertItems',
    });
};
//# sourceMappingURL=insertCandyMachineItems.js.map