'use strict';

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

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

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

const Key = 'RevokeTokenDelegateAuthorityOperation';

/**
 * Revokes the current delegate authority for a token account.
 *
 * ```ts
 * await metaplex
 *   .tokens()
 *   .revokeDelegateAuthority({ mintAddress };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const revokeTokenDelegateAuthorityOperation = Operation.useOperation(Key);

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

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

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

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

/**
 * Revokes the current delegate authority for a token account.
 *
 * ```ts
 * await metaplex
 *   .tokens()
 *   .builders()
 *   .revokeDelegateAuthority({ mintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const revokeTokenDelegateAuthorityBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    owner = metaplex.identity(),
    tokenAddress,
    multiSigners = []
  } = params;
  const [ownerPublicKey, signers] = Signer.isSigner(owner) ? [owner.publicKey, [owner]] : [owner, multiSigners];
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenAccount = tokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: ownerPublicKey,
    programs
  });
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer).add({
    instruction: splToken.createRevokeInstruction(tokenAccount, ownerPublicKey, multiSigners, tokenProgram.address),
    signers,
    key: params.instructionKey ?? 'revokeDelegateAuthority'
  });
};

exports.revokeTokenDelegateAuthorityBuilder = revokeTokenDelegateAuthorityBuilder;
exports.revokeTokenDelegateAuthorityOperation = revokeTokenDelegateAuthorityOperation;
exports.revokeTokenDelegateAuthorityOperationHandler = revokeTokenDelegateAuthorityOperationHandler;
//# sourceMappingURL=revokeTokenDelegateAuthority.cjs.map
