'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 = 'RevokeNftUseAuthorityOperation';

/**
 * Revokes an existing use authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .revokeUseAuthority({ mintAddress, user };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const revokeNftUseAuthorityOperation = Operation.useOperation(Key);

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

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

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

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

/**
 * Revokes an existing use authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .revokeUseAuthority({ mintAddress, user });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const revokeNftUseAuthorityBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    user,
    owner = metaplex.identity()
  } = params;

  // Programs.
  const systemProgram = metaplex.programs().getSystem(programs);
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);

  // PDAs.
  const metadata = metaplex.nfts().pdas().metadata({
    mint: mintAddress,
    programs
  });
  const useAuthorityRecord = metaplex.nfts().pdas().useAuthorityRecord({
    mint: mintAddress,
    useAuthority: user,
    programs
  });
  const ownerTokenAddress = params.ownerTokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: owner.publicKey,
    programs
  });
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer)

  // Revoke the use authority.
  .add({
    instruction: mplTokenMetadata.createRevokeUseAuthorityInstruction({
      useAuthorityRecord,
      owner: owner.publicKey,
      user,
      ownerTokenAccount: ownerTokenAddress,
      mint: mintAddress,
      metadata,
      tokenProgram: tokenProgram.address,
      systemProgram: systemProgram.address
    }, tokenMetadataProgram.address),
    signers: [owner],
    key: params.instructionKey ?? 'revokeUseAuthority'
  });
};

exports.revokeNftUseAuthorityBuilder = revokeNftUseAuthorityBuilder;
exports.revokeNftUseAuthorityOperation = revokeNftUseAuthorityOperation;
exports.revokeNftUseAuthorityOperationHandler = revokeNftUseAuthorityOperationHandler;
//# sourceMappingURL=revokeNftUseAuthority.cjs.map
