'use strict';

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

var web3_js = require('@solarti/web3.js');
var accounts = require('../accounts.cjs');
var pdas = require('../pdas.cjs');
var program = require('../program.cjs');
var common = require('../../../utils/common.cjs');
var Operation = require('../../../types/Operation.cjs');
var SdkError = require('../../../errors/SdkError.cjs');
var Mint = require('../../tokenModule/models/Mint.cjs');
var accounts$1 = require('../../tokenModule/accounts.cjs');
var CandyMachineV2 = require('../models/CandyMachineV2.cjs');

// -----------------
// Operation
// -----------------

const Key = 'FindCandyMachinesV2ByPublicKeyOperation';

/**
 * Find all Candy Machines matching by a given `publicKey` or a given `type`.
 *
 * The following two types are supported.
 *
 * `authority`: Find Candy Machines whose authority is the given `publicKey`.
 * ```ts
 * const someAuthority = new PublicKey('...');
 * const candyMachines = await metaplex
 *   .candyMachinesV2()
 *   .findAllBy({ type: 'authority', someAuthority });
 * ```
 *
 * `wallet`: Find Candy Machines whose wallet address is the given `publicKey`.
 * ```ts
 * const someWallet = new PublicKey('...');
 * const candyMachines = await metaplex
 *   .candyMachinesV2()
 *   .findAllBy({ type: 'wallet', someWallet });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const findCandyMachinesV2ByPublicKeyFieldOperation = Operation.useOperation(Key);

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

/**
 * @group Operations
 * @category Handlers
 */
const findCandyMachinesV2ByPublicKeyFieldOperationHandler = {
  handle: async (operation, metaplex, scope) => {
    const {
      commitment
    } = scope;
    const {
      type,
      publicKey
    } = operation.input;
    const accounts$2 = program.CandyMachineV2Program.accounts(metaplex).mergeConfig({
      commitment
    });
    let candyMachineQuery;
    switch (type) {
      case 'authority':
        candyMachineQuery = accounts$2.candyMachineAccountsForAuthority(publicKey);
        break;
      case 'wallet':
        candyMachineQuery = accounts$2.candyMachineAccountsForWallet(publicKey);
        break;
      default:
        throw new SdkError.UnreachableCaseError(type);
    }
    const unparsedAccounts = await candyMachineQuery.get();
    scope.throwIfCanceled();
    const collectionPdas = unparsedAccounts.map(unparsedAccount => pdas.findCandyMachineV2CollectionPda(unparsedAccount.publicKey));

    // Find mint details for all unique SPL tokens used
    // in candy machines that have non-null `tokenMint`

    const parsedAccounts = Object.fromEntries(unparsedAccounts.map(unparsedAccount => [unparsedAccount.publicKey.toString(), accounts.parseCandyMachineV2Account(unparsedAccount)]));
    const tokenMints = [...new Set(Object.values(parsedAccounts).map(account => account.data.tokenMint?.toString()).filter(tokenMint => tokenMint !== undefined))].map(address => new web3_js.PublicKey(address));
    const result = await metaplex.rpc().getMultipleAccounts(tokenMints.concat(collectionPdas), commitment);
    scope.throwIfCanceled();
    const unparsedMintAccounts = result.slice(0, tokenMints.length);
    const unparsedCollectionAccounts = result.slice(-collectionPdas.length);
    const mints = Object.fromEntries(unparsedMintAccounts.map(account => [account.publicKey.toString(), Mint.toMint(accounts$1.toMintAccount(account))]));
    return common.zipMap(unparsedAccounts, unparsedCollectionAccounts, (unparsedAccount, unparsedCollectionAccount) => {
      const parsedAccount = parsedAccounts[unparsedAccount.publicKey.toString()];
      const collectionAccount = unparsedCollectionAccount ? accounts.parseCandyMachineV2CollectionAccount(unparsedCollectionAccount) : null;
      const tokenMintAddress = parsedAccount.data.tokenMint?.toString();
      return CandyMachineV2.toCandyMachineV2(parsedAccount, unparsedAccount, collectionAccount, tokenMintAddress ? mints[tokenMintAddress] : null);
    });
  }
};

exports.findCandyMachinesV2ByPublicKeyFieldOperation = findCandyMachinesV2ByPublicKeyFieldOperation;
exports.findCandyMachinesV2ByPublicKeyFieldOperationHandler = findCandyMachinesV2ByPublicKeyFieldOperationHandler;
//# sourceMappingURL=findCandyMachinesV2ByPublicKeyField.cjs.map
