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 { toListingReceiptAccount } from '../accounts';
import { ListingReceiptGpaBuilder } from '../gpaBuilders';
import { toLazyListing } from '../models';
import { useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'FindListingsOperation';
/**
 * Finds Listings by multiple criteria.
 *
 * ```ts
 * // Find all listings in an Auction House.
 * const listings = await metaplex
 *   .auctionHouse()
 *   .findListings({ auctionHouse });
 *
 * // Find listings by seller and mint.
 * const listings = await metaplex
 *   .auctionHouse()
 *   .findListings({ auctionHouse, seller, mint });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const findListingsOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const findListingsOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        const { commitment, programs } = scope;
        const { auctionHouse, seller, metadata, mint } = operation.input;
        const auctionHouseProgram = metaplex.programs().getAuctionHouse(programs);
        let listingQuery = new ListingReceiptGpaBuilder(metaplex, auctionHouseProgram.address)
            .mergeConfig({ commitment })
            .whereAuctionHouse(auctionHouse.address);
        if (seller) {
            listingQuery = listingQuery.whereSeller(seller);
        }
        if (metadata && !mint) {
            listingQuery = listingQuery.whereMetadata(metadata);
        }
        if (mint) {
            listingQuery = listingQuery.whereMetadata(metaplex.nfts().pdas().metadata({ mint, programs }));
        }
        scope.throwIfCanceled();
        return listingQuery.getAndMap((account) => toLazyListing(toListingReceiptAccount(account), auctionHouse));
    }),
};
//# sourceMappingURL=findListings.js.map