import { MetadataV1GpaBuilder } from '../gpaBuilders.mjs';
import { useOperation } from '../../../types/Operation.mjs';

// -----------------
// 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 = 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 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);
  }
};

export { findNftsByCreatorOperation, findNftsByCreatorOperationHandler };
//# sourceMappingURL=findNftsByCreator.mjs.map
