'use strict';

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

var mplTokenMetadata = require('@metaplex-solarti/mpl-token-metadata');
var Operation = require('../../../types/Operation.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

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

const Key = 'VerifyNftCollectionOperation';

/**
 * Verifies the collection of an NFT or SFT.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .verifyCollection({ mintAddress, collectionMintAddress };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const verifyNftCollectionOperation = Operation.useOperation(Key);

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

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

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

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

/**
 * Verifies the collection of an NFT or SFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .verifyCollection({ mintAddress, collectionMintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const verifyNftCollectionBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    collectionMintAddress,
    isSizedCollection = true,
    isDelegated = false,
    collectionAuthority = metaplex.identity()
  } = params;

  // Programs.
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
  const accounts = {
    metadata: metaplex.nfts().pdas().metadata({
      mint: mintAddress,
      programs
    }),
    collectionAuthority: collectionAuthority.publicKey,
    payer: payer.publicKey,
    collectionMint: collectionMintAddress,
    collection: metaplex.nfts().pdas().metadata({
      mint: collectionMintAddress,
      programs
    }),
    collectionMasterEditionAccount: metaplex.nfts().pdas().masterEdition({
      mint: collectionMintAddress,
      programs
    })
  };
  const instruction = isSizedCollection ? mplTokenMetadata.createVerifySizedCollectionItemInstruction(accounts, tokenMetadataProgram.address) : mplTokenMetadata.createVerifyCollectionInstruction(accounts, tokenMetadataProgram.address);
  if (isDelegated) {
    instruction.keys.push({
      pubkey: metaplex.nfts().pdas().collectionAuthorityRecord({
        mint: collectionMintAddress,
        collectionAuthority: collectionAuthority.publicKey,
        programs
      }),
      isWritable: false,
      isSigner: false
    });
  }
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer)

  // Verify the collection.
  .add({
    instruction,
    signers: [payer, collectionAuthority],
    key: params.instructionKey ?? 'verifyCollection'
  });
};

exports.verifyNftCollectionBuilder = verifyNftCollectionBuilder;
exports.verifyNftCollectionOperation = verifyNftCollectionOperation;
exports.verifyNftCollectionOperationHandler = verifyNftCollectionOperationHandler;
//# sourceMappingURL=verifyNftCollection.cjs.map
