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 { toPurchaseReceiptAccount } from '../accounts';
import { PurchaseReceiptGpaBuilder } from '../gpaBuilders';
import { toLazyPurchase, } from '../models';
import { useOperation, } from '../../../types';
import { UnreachableCaseError } from '../../../errors';
// -----------------
// Operation
// -----------------
const Key = 'FindPurchasesByPublicKeyOperation';
/**
 * Finds multiple Purchases by specific criteria.
 *
 * ```ts
 * // Find purchases by seller.
 * const purchases = await metaplex
 *   .auctionHouse()
 *   .findPurchasesBy({ auctionHouse, type: 'seller', publicKey: seller };
 *
 * // Find purchases by buyer.
 * const purchases = await metaplex
 *   .auctionHouse()
 *   .findPurchasesBy({ auctionHouse, type: 'buyer', publicKey: buyer };
 *
 * // Find purchases by metadata.
 * const purchases = await metaplex
 *   .auctionHouse()
 *   .findPurchasesBy({ auctionHouse, type: 'metadata', publicKey: metadata };
 *
 * // Find purchases by mint.
 * const purchases = await metaplex
 *   .auctionHouse()
 *   .findPurchasesBy({ auctionHouse, type: 'mint', publicKey: mint };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const findPurchasesByPublicKeyFieldOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const findPurchasesByPublicKeyFieldOperationHandler = {
    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 purchaseQuery = new PurchaseReceiptGpaBuilder(metaplex, auctionHouseProgram.address)
            .mergeConfig({ commitment })
            .whereAuctionHouse(auctionHouse.address);
        switch (type) {
            case 'buyer':
                purchaseQuery = purchaseQuery.whereBuyer(publicKey);
                break;
            case 'seller':
                purchaseQuery = purchaseQuery.whereSeller(publicKey);
                break;
            case 'metadata':
                purchaseQuery = purchaseQuery.whereMetadata(publicKey);
                break;
            case 'mint':
                purchaseQuery = purchaseQuery.whereMetadata(metaplex.nfts().pdas().metadata({ mint: publicKey, programs }));
                break;
            default:
                throw new UnreachableCaseError(type);
        }
        scope.throwIfCanceled();
        return purchaseQuery.getAndMap((account) => toLazyPurchase(toPurchaseReceiptAccount(account), auctionHouse));
    }),
};
//# sourceMappingURL=findPurchasesByPublicKeyField.js.map