'use strict';

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

var gpaBuilders = require('../gpaBuilders.cjs');
var Operation = require('../../../types/Operation.cjs');

// -----------------
// Operation
// -----------------

const Key = 'FindNftsByCreatorOperation';

/**
 * Finds multiple NFTs and SFTs by their creator at a given position.
 *
 * ```ts
 * // Find all by first creator.
 * const nfts = await metaplex
 *   .nfts()
 *   .findAllByCreator({ creator };
 *
 * // Find all by second creator.
 * const nfts = await metaplex
 *   .nfts()
 *   .findAllByCreator({ creator, position: 2 };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const findNftsByCreatorOperation = Operation.useOperation(Key);

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

/**
 * @group Operations
 * @category Handlers
 */
const findNftsByCreatorOperationHandler = {
  handle: async (operation, metaplex, scope) => {
    const {
      programs
    } = scope;
    const {
      creator,
      position = 1
    } = operation.input;
    const gpaBuilder = new gpaBuilders.MetadataV1GpaBuilder(metaplex, metaplex.programs().getTokenMetadata(programs).address);
    const mints = await gpaBuilder.selectMint().whereCreator(position, creator).getDataAsPublicKeys();
    scope.throwIfCanceled();
    const nfts = await metaplex.nfts().findAllByMintList({
      mints
    }, scope);
    scope.throwIfCanceled();
    return nfts.filter(nft => nft !== null);
  }
};

exports.findNftsByCreatorOperation = findNftsByCreatorOperation;
exports.findNftsByCreatorOperationHandler = findNftsByCreatorOperationHandler;
//# sourceMappingURL=findNftsByCreator.cjs.map
