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

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

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

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

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

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

/**
 * Thaws a NFT via its delegate authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .thawDelegatedNft({ mintAddress, delegateAuthority });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const thawDelegatedNftBuilder = (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);
  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.createThawDelegatedAccountInstruction({
      delegate: delegateAuthority.publicKey,
      tokenAccount: tokenAddressOrAta,
      edition: editionAddress,
      mint: mintAddress,
      tokenProgram: tokenProgram.address
    }, tokenMetadataProgram.address),
    signers: [delegateAuthority],
    key: params.instructionKey ?? 'thawDelegatedNft'
  });
};

exports.thawDelegatedNftBuilder = thawDelegatedNftBuilder;
exports.thawDelegatedNftOperation = thawDelegatedNftOperation;
exports.thawDelegatedNftOperationHandler = thawDelegatedNftOperationHandler;
//# sourceMappingURL=thawDelegatedNft.cjs.map
