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

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

const Key = 'UnverifyNftCollectionOperation';

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

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

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

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

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

/**
 * Unverifies the collection of an NFT or SFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .unverifyCollection({ mintAddress, collectionMintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const unverifyNftCollectionBuilder = (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
    }),
    collectionAuthorityRecord: isDelegated ? metaplex.nfts().pdas().collectionAuthorityRecord({
      mint: collectionMintAddress,
      collectionAuthority: collectionAuthority.publicKey,
      programs
    }) : undefined
  };
  const instruction = isSizedCollection ? createUnverifySizedCollectionItemInstruction(accounts, tokenMetadataProgram.address) : createUnverifyCollectionInstruction(accounts, tokenMetadataProgram.address);
  return TransactionBuilder.make().setFeePayer(payer)

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

export { unverifyNftCollectionBuilder, unverifyNftCollectionOperation, unverifyNftCollectionOperationHandler };
//# sourceMappingURL=unverifyNftCollection.mjs.map
