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

/**
 * Freezes a NFT via its delegate authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .freezeDelegatedNft({ mintAddress, delegateAuthority };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const freezeDelegatedNftOperation = Operation.useOperation(Key);

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

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

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

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

/**
 * Freezes a NFT via its delegate authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .freezeDelegatedNft({ mintAddress, delegateAuthority });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const freezeDelegatedNftBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    delegateAuthority,
    tokenOwner = metaplex.identity().publicKey,
    tokenAddress
  } = params;

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

  // PDAs.
  const editionAddress = metaplex.nfts().pdas().masterEdition({
    mint: mintAddress,
    programs
  });
  const tokenAddressOrAta = tokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: tokenOwner,
    programs
  });
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: mplTokenMetadata.createFreezeDelegatedAccountInstruction({
      delegate: delegateAuthority.publicKey,
      tokenAccount: tokenAddressOrAta,
      edition: editionAddress,
      mint: mintAddress,
      tokenProgram: tokenProgram.address
    }, tokenMetadataProgram.address),
    signers: [delegateAuthority],
    key: params.instructionKey ?? 'freezeDelegatedNft'
  });
};

exports.freezeDelegatedNftBuilder = freezeDelegatedNftBuilder;
exports.freezeDelegatedNftOperation = freezeDelegatedNftOperation;
exports.freezeDelegatedNftOperationHandler = freezeDelegatedNftOperationHandler;
//# sourceMappingURL=freezeDelegatedNft.cjs.map
