'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var splToken = require('@solarti/spl-token');
var Operation = require('../../../types/Operation.cjs');
var Amount = require('../../../types/Amount.cjs');
var Signer = require('../../../types/Signer.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

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

const Key = 'ApproveTokenDelegateAuthorityOperation';

/**
 * Approves a delegate authority for a token account.
 *
 * ```ts
 * await metaplex
 *   .tokens()
 *   .approveDelegateAuthority({
 *     delegateAuthority,
 *     mintAddress,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const approveTokenDelegateAuthorityOperation = Operation.useOperation(Key);

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

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

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

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

/**
 * Approves a delegate authority for a token account.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .tokens()
 *   .builders()
 *   .approveDelegateAuthority({
 *     delegateAuthority,
 *     mintAddress,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const approveTokenDelegateAuthorityBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    delegateAuthority,
    amount = Amount.token(1),
    owner = metaplex.identity(),
    tokenAddress,
    multiSigners = []
  } = params;
  const [ownerPublicKey, signers] = Signer.isSigner(owner) ? [owner.publicKey, [owner]] : [owner, multiSigners];
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenAddressOrAta = tokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: ownerPublicKey,
    programs
  });
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: splToken.createApproveInstruction(tokenAddressOrAta, delegateAuthority, ownerPublicKey, amount.basisPoints.toNumber(), multiSigners, tokenProgram.address),
    signers,
    key: params.instructionKey ?? 'approveDelegateAuthority'
  });
};

exports.approveTokenDelegateAuthorityBuilder = approveTokenDelegateAuthorityBuilder;
exports.approveTokenDelegateAuthorityOperation = approveTokenDelegateAuthorityOperation;
exports.approveTokenDelegateAuthorityOperationHandler = approveTokenDelegateAuthorityOperationHandler;
//# sourceMappingURL=approveTokenDelegateAuthority.cjs.map
