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

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

const Key = 'VerifyNftCreatorOperation';

/**
 * Verifies the creator of an NFT or SFT.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .verifyCreator({ mintAddress, creator };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const verifyNftCreatorOperation = useOperation(Key);

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

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

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

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

/**
 * Verifies the creator of an NFT or SFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .verifyCreator({ mintAddress, creator });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const verifyNftCreatorBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    creator = metaplex.identity()
  } = params;

  // Programs.
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
  return TransactionBuilder.make().setFeePayer(payer)

  // Verify the creator.
  .add({
    instruction: createSignMetadataInstruction({
      metadata: metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs
      }),
      creator: creator.publicKey
    }, tokenMetadataProgram.address),
    signers: [creator],
    key: params.instructionKey ?? 'verifyCreator'
  });
};

export { verifyNftCreatorBuilder, verifyNftCreatorOperation, verifyNftCreatorOperationHandler };
//# sourceMappingURL=verifyNftCreator.mjs.map
