var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { BidReceiptGpaBuilder } from '../gpaBuilders';
import { toLazyBid } from '../models';
import { toBidReceiptAccount } from '../accounts';
import { useOperation, } from '../../../types';
import { UnreachableCaseError } from '../../../errors';
// -----------------
// 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
 */
export const findBidsByPublicKeyFieldOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const findBidsByPublicKeyFieldOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        const { programs, commitment } = scope;
        const { auctionHouse, type, publicKey } = operation.input;
        const auctionHouseProgram = metaplex.programs().getAuctionHouse();
        let bidQuery = new 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 UnreachableCaseError(type);
        }
        scope.throwIfCanceled();
        return bidQuery.getAndMap((account) => toLazyBid(toBidReceiptAccount(account), auctionHouse));
    }),
};
//# sourceMappingURL=findBidsByPublicKeyField.js.map