import { createRevokeInstruction } from '@solarti/spl-token';
import { useOperation } from '../../../types/Operation.mjs';
import { isSigner } from '../../../types/Signer.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

// -----------------
// 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 = 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] = 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.make().setFeePayer(payer).add({
    instruction: createRevokeInstruction(tokenAccount, ownerPublicKey, multiSigners, tokenProgram.address),
    signers,
    key: params.instructionKey ?? 'revokeDelegateAuthority'
  });
};

export { revokeTokenDelegateAuthorityBuilder, revokeTokenDelegateAuthorityOperation, revokeTokenDelegateAuthorityOperationHandler };
//# sourceMappingURL=revokeTokenDelegateAuthority.mjs.map
