'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var mplTokenMetadata = require('@metaplex-solarti/mpl-token-metadata');
var Operation = require('../../../types/Operation.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');

// -----------------
// 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
 */
const migrateToSizedCollectionNftOperation = Operation.useOperation(Key);

/**
 * @group Operations
 * @category Types
 */

/**
 * @group Operations
 * @category Handlers
 */
const migrateToSizedCollectionNftOperationHandler = {
  handle: async (operation, metaplex, scope) => {
    return migrateToSizedCollectionNftBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
  }
};

// -----------------
// Builder
// -----------------

/**
 * @group Transaction Builders
 * @category Inputs
 */

/**
 * 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
 */
const migrateToSizedCollectionNftBuilder = (metaplex, params, options = {}) => {
  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.TransactionBuilder.make().setFeePayer(payer)

  // Update the metadata account.
  .add({
    instruction: mplTokenMetadata.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: params.instructionKey ?? 'setCollectionSize'
  });
};

exports.migrateToSizedCollectionNftBuilder = migrateToSizedCollectionNftBuilder;
exports.migrateToSizedCollectionNftOperation = migrateToSizedCollectionNftOperation;
exports.migrateToSizedCollectionNftOperationHandler = migrateToSizedCollectionNftOperationHandler;
//# sourceMappingURL=migrateToSizedCollectionNft.cjs.map
