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 { createVerifyCollectionInstruction, createVerifySizedCollectionItemInstruction, } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'VerifyNftCollectionOperation';
/**
 * Verifies the collection of an NFT or SFT.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .verifyCollection({ mintAddress, collectionMintAddress };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const verifyNftCollectionOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const verifyNftCollectionOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return verifyNftCollectionBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Verifies the collection of an NFT or SFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .verifyCollection({ mintAddress, collectionMintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const verifyNftCollectionBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, collectionMintAddress, isSizedCollection = true, isDelegated = false, collectionAuthority = metaplex.identity(), } = params;
    // Programs.
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const accounts = {
        metadata: metaplex.nfts().pdas().metadata({
            mint: mintAddress,
            programs,
        }),
        collectionAuthority: collectionAuthority.publicKey,
        payer: payer.publicKey,
        collectionMint: collectionMintAddress,
        collection: metaplex.nfts().pdas().metadata({
            mint: collectionMintAddress,
            programs,
        }),
        collectionMasterEditionAccount: metaplex.nfts().pdas().masterEdition({
            mint: collectionMintAddress,
            programs,
        }),
    };
    const instruction = isSizedCollection
        ? createVerifySizedCollectionItemInstruction(accounts, tokenMetadataProgram.address)
        : createVerifyCollectionInstruction(accounts, tokenMetadataProgram.address);
    if (isDelegated) {
        instruction.keys.push({
            pubkey: metaplex.nfts().pdas().collectionAuthorityRecord({
                mint: collectionMintAddress,
                collectionAuthority: collectionAuthority.publicKey,
                programs,
            }),
            isWritable: false,
            isSigner: false,
        });
    }
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Verify the collection.
        .add({
        instruction,
        signers: [payer, collectionAuthority],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'verifyCollection',
    }));
};
//# sourceMappingURL=verifyNftCollection.js.map