import { createVerifySizedCollectionItemInstruction, createVerifyCollectionInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation } from '../../../types/Operation.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

// -----------------
// 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 = 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 ? createVerifySizedCollectionItemInstruction(accounts, tokenMetadataProgram.address) : 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.make().setFeePayer(payer)

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

export { verifyNftCollectionBuilder, verifyNftCollectionOperation, verifyNftCollectionOperationHandler };
//# sourceMappingURL=verifyNftCollection.mjs.map
