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 { createSetCollectionSizeInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'MigrateToSizedCollectionNftOperation';
/**
 * Migrates a legacy Collection NFT to a sized Collection NFT.
 * Both can act as a Collection for NFTs but only the latter
 * keeps track of the size of the collection on chain.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .migrateToSizedCollection({ mintAddress, size: toBigNumber(10000) };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const migrateToSizedCollectionNftOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const migrateToSizedCollectionNftOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return migrateToSizedCollectionNftBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Migrates a legacy Collection NFT to a sized Collection NFT.
 * Both can act as a Collection for NFTs but only the latter
 * keeps track of the size of the collection on chain.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .migrateToSizedCollection({ mintAddress, size: toBigNumber(10000) });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const migrateToSizedCollectionNftBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, collectionAuthority = metaplex.identity(), size, isDelegated = false, } = params;
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const nftPdas = metaplex.nfts().pdas();
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Update the metadata account.
        .add({
        instruction: createSetCollectionSizeInstruction({
            collectionMetadata: nftPdas.metadata({
                mint: mintAddress,
                programs,
            }),
            collectionAuthority: collectionAuthority.publicKey,
            collectionMint: mintAddress,
            collectionAuthorityRecord: isDelegated
                ? nftPdas.collectionAuthorityRecord({
                    mint: mintAddress,
                    collectionAuthority: collectionAuthority.publicKey,
                    programs,
                })
                : undefined,
        }, { setCollectionSizeArgs: { size } }, tokenMetadataProgram.address),
        signers: [collectionAuthority],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'setCollectionSize',
    }));
};
//# sourceMappingURL=migrateToSizedCollectionNft.js.map