'use strict';

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

var accounts = require('../accounts.cjs');
var BidReceiptGpaBuilder = require('../gpaBuilders/BidReceiptGpaBuilder.cjs');
var SdkError = require('../../../errors/SdkError.cjs');
var Operation = require('../../../types/Operation.cjs');
var Bid = require('../models/Bid.cjs');

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

const Key = 'FindBidsByPublicKeyOperation';

/**
 * Finds multiple Bids by specific criteria.
 *
 * ```ts
 * // Find bids by buyer.
 * const bids = await metaplex
 *   .auctionHouse()
 *   .findBidsBy({ auctionHouse, type: 'buyer', publicKey: buyer };
 *
 * // Find bids by metadata.
 * const bids = await metaplex
 *   .auctionHouse()
 *   .findBidsBy({ auctionHouse, type: 'metadata', publicKey: metadata };
 *
 * // Find bids by mint.
 * const bids = await metaplex
 *   .auctionHouse()
 *   .findBidsBy({ auctionHouse, type: 'mint', publicKey: mint };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const findBidsByPublicKeyFieldOperation = Operation.useOperation(Key);

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

/**
 * @group Operations
 * @category Handlers
 */
const findBidsByPublicKeyFieldOperationHandler = {
  handle: async (operation, metaplex, scope) => {
    const {
      programs,
      commitment
    } = scope;
    const {
      auctionHouse,
      type,
      publicKey
    } = operation.input;
    const auctionHouseProgram = metaplex.programs().getAuctionHouse();
    let bidQuery = new BidReceiptGpaBuilder.BidReceiptGpaBuilder(metaplex, auctionHouseProgram.address).mergeConfig({
      commitment
    }).whereAuctionHouse(auctionHouse.address);
    switch (type) {
      case 'buyer':
        bidQuery = bidQuery.whereBuyer(publicKey);
        break;
      case 'metadata':
        bidQuery = bidQuery.whereMetadata(publicKey);
        break;
      case 'mint':
        bidQuery = bidQuery.whereMetadata(metaplex.nfts().pdas().metadata({
          mint: publicKey,
          programs
        }));
        break;
      default:
        throw new SdkError.UnreachableCaseError(type);
    }
    scope.throwIfCanceled();
    return bidQuery.getAndMap(account => Bid.toLazyBid(accounts.toBidReceiptAccount(account), auctionHouse));
  }
};

exports.findBidsByPublicKeyFieldOperation = findBidsByPublicKeyFieldOperation;
exports.findBidsByPublicKeyFieldOperationHandler = findBidsByPublicKeyFieldOperationHandler;
//# sourceMappingURL=findBidsByPublicKeyField.cjs.map
