'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var mplCandyMachine = require('@metaplex-solarti/mpl-candy-machine');
var asserts = require('../asserts.cjs');
var Operation = require('../../../types/Operation.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

// -----------------
// 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
 */
const insertItemsToCandyMachineV2Operation = Operation.useOperation(Key);

/**
 * @group Operations
 * @category Types
 */

/**
 * @group Operations
 * @category Handlers
 */
const InsertItemsToCandyMachineV2OperationHandler = {
  async handle(operation, metaplex, scope) {
    return insertItemsToCandyMachineV2Builder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
  }
};

// -----------------
// Builder
// -----------------

/**
 * @group Transaction Builders
 * @category Inputs
 */

/**
 * Insert items into an existing Candy Machine.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .candyMachines()
 *   .builders()
 *   .insertItems({ candyMachine, items });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const insertItemsToCandyMachineV2Builder = (metaplex, params, options = {}) => {
  const {
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const authority = params.authority ?? metaplex.identity();
  const index = params.index ?? params.candyMachine.itemsLoaded;
  const {
    items
  } = params;
  asserts.assertNotFull(params.candyMachine, index);
  asserts.assertCanAdd(params.candyMachine, index, items.length);
  asserts.assertAllConfigLineConstraints(items);
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: mplCandyMachine.createAddConfigLinesInstruction({
      candyMachine: params.candyMachine.address,
      authority: authority.publicKey
    }, {
      index: index.toNumber(),
      configLines: items
    }),
    signers: [authority],
    key: params.instructionKey ?? 'insertItems'
  });
};

exports.InsertItemsToCandyMachineV2OperationHandler = InsertItemsToCandyMachineV2OperationHandler;
exports.insertItemsToCandyMachineV2Builder = insertItemsToCandyMachineV2Builder;
exports.insertItemsToCandyMachineV2Operation = insertItemsToCandyMachineV2Operation;
//# sourceMappingURL=insertItemsToCandyMachineV2.cjs.map
