import { createApproveUseAuthorityInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation } from '../../../types/Operation.mjs';
import { TransactionBuilder } from '../../../utils/TransactionBuilder.mjs';

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

const Key = 'ApproveNftUseAuthorityOperation';

/**
 * Approves a new use authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .approveUseAuthority({ mintAddress, user };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const approveNftUseAuthorityOperation = useOperation(Key);

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

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

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

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

/**
 * Approves a new use authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .approveUseAuthority({ mintAddress, user });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const approveNftUseAuthorityBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    mintAddress,
    user,
    owner = metaplex.identity()
  } = params;
  const systemProgram = metaplex.programs().getSystem(programs);
  const tokenProgram = metaplex.programs().getToken(programs);
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
  const metadata = metaplex.nfts().pdas().metadata({
    mint: mintAddress,
    programs
  });
  const useAuthorityRecord = metaplex.nfts().pdas().useAuthorityRecord({
    mint: mintAddress,
    useAuthority: user,
    programs
  });
  const programAsBurner = metaplex.nfts().pdas().burner({
    programs
  });
  const ownerTokenAddress = params.ownerTokenAddress ?? metaplex.tokens().pdas().associatedTokenAccount({
    mint: mintAddress,
    owner: owner.publicKey,
    programs
  });
  return TransactionBuilder.make().setFeePayer(payer)

  // Approve the use authority.
  .add({
    instruction: createApproveUseAuthorityInstruction({
      useAuthorityRecord,
      owner: owner.publicKey,
      payer: payer.publicKey,
      user,
      ownerTokenAccount: ownerTokenAddress,
      metadata,
      mint: mintAddress,
      burner: programAsBurner,
      tokenProgram: tokenProgram.address,
      systemProgram: systemProgram.address
    }, {
      approveUseAuthorityArgs: {
        numberOfUses: params.numberOfUses ?? 1
      }
    }, tokenMetadataProgram.address),
    signers: [owner, payer],
    key: params.instructionKey ?? 'approveUseAuthority'
  });
};

export { approveNftUseAuthorityBuilder, approveNftUseAuthorityOperation, approveNftUseAuthorityOperationHandler };
//# sourceMappingURL=approveNftUseAuthority.mjs.map
