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 { assertNftOrSftWithToken } from '../../nftModule';
import { amount, useOperation, } from '../../../types';
import { assert } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'LoadBidOperation';
/**
 * Transforms a `LazyBid` model into a `Bid` model.
 *
 * ```ts
 * const bid = await metaplex
 *   .auctionHouse()
 *   .loadBid({ lazyBid };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const loadBidOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const loadBidOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        const { lazyBid, loadJsonMetadata = true } = operation.input;
        const bid = Object.assign(Object.assign({}, lazyBid), { model: 'bid', lazy: false });
        if (lazyBid.tokenAddress) {
            const asset = yield metaplex
                .nfts()
                .findByToken({ token: lazyBid.tokenAddress, loadJsonMetadata }, scope);
            scope.throwIfCanceled();
            assertNftOrSftWithToken(asset);
            assert(asset.metadataAddress.equals(lazyBid.metadataAddress), `Asset metadata address must be ${lazyBid.metadataAddress}`);
            return Object.assign(Object.assign({}, bid), { isPublic: false, asset, tokens: amount(lazyBid.tokens, asset.mint.currency) });
        }
        const asset = yield metaplex
            .nfts()
            .findByMetadata({ metadata: lazyBid.metadataAddress, loadJsonMetadata }, scope);
        scope.throwIfCanceled();
        return Object.assign(Object.assign({}, bid), { isPublic: true, asset, tokens: amount(lazyBid.tokens, asset.mint.currency) });
    }),
};
//# sourceMappingURL=loadBid.js.map