'use strict';

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

var mplCandyMachineCore = require('@metaplex-foundation/mpl-candy-machine-core');
var common = require('../../../utils/common.cjs');
var CandyMachine = require('../models/CandyMachine.cjs');
var assert = require('../../../utils/assert.cjs');
var SdkError = require('../../../errors/SdkError.cjs');
var TransactionBuilder = require('../../../utils/TransactionBuilder.cjs');
var PublicKey = require('../../../types/PublicKey.cjs');

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

const Key = 'UpdateCandyMachineOperation';

/**
 * Updates the every aspect of an existing Candy Machine, including its
 * authorities, collection and guards (when associated with a Candy Guard).
 *
 * ```ts
 * await metaplex
 *   .candyMachines()
 *   .update({
 *     candyMachine,
 *     sellerFeeBasisPoints: 500,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
const updateCandyMachineOperation = _updateCandyMachineOperation;
// eslint-disable-next-line @typescript-eslint/naming-convention
function _updateCandyMachineOperation(input) {
  return {
    key: Key,
    input
  };
}
_updateCandyMachineOperation.key = Key;

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

/**
 * @group Operations
 * @category Handlers
 */
const updateCandyMachineOperationHandler = {
  async handle(operation, metaplex, scope) {
    const builder = updateCandyMachineBuilder(metaplex, operation.input, scope);
    if (builder.isEmpty()) {
      throw new SdkError.NoInstructionsToSendError(Key);
    }
    return builder.sendAndConfirm(metaplex, scope.confirmOptions);
  }
};

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

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

/**
 * Updates the every aspect of an existing Candy Machine, including its
 * authorities, collection and guards (when associated with a Candy Guard).
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .candyMachines()
 *   .builders()
 *   .update({
 *     candyMachine,
 *     sellerFeeBasisPoints: 500,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
const updateCandyMachineBuilder = (metaplex, params, options = {}) => {
  const {
    programs,
    payer = metaplex.rpc().getDefaultFeePayer()
  } = options;
  const {
    authority = metaplex.identity(),
    candyGuardAuthority = authority
  } = params;
  return TransactionBuilder.TransactionBuilder.make().setFeePayer(payer)

  // Update Candy Machine data.
  .add(updateCandyMachineDataBuilder(metaplex, params, authority, programs))

  // Update Candy Machine collection.
  .add(updateCandyMachineCollectionBuilder(metaplex, params, authority, payer, programs))

  // Update Candy Guard's guards and groups, if any.
  .add(updateCandyGuardsBuilder(metaplex, params, candyGuardAuthority, payer, programs))

  // Update Candy Machine mint authority.
  .add(updateCandyMachineMintAuthorityBuilder(metaplex, params, authority, programs))

  // Update Candy Machine authority.
  .add(updateCandyMachineAuthorityBuilder(metaplex, params, authority, programs));
};
const updateCandyMachineDataBuilder = (metaplex, params, authority, programs) => {
  const dataToUpdate = common.removeUndefinedAttributes({
    itemsAvailable: params.itemsAvailable,
    symbol: params.symbol,
    sellerFeeBasisPoints: params.sellerFeeBasisPoints,
    maxEditionSupply: params.maxEditionSupply,
    isMutable: params.isMutable,
    creators: params.creators,
    itemSettings: params.itemSettings
  });
  const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
  let data;
  if (Object.keys(dataToUpdate).length === 0) {
    return TransactionBuilder.TransactionBuilder.make();
  } else if (CandyMachine.isCandyMachine(params.candyMachine)) {
    data = CandyMachine.toCandyMachineData({
      ...params.candyMachine,
      ...dataToUpdate
    });
  } else {
    assert.assertObjectHasDefinedKeys(dataToUpdate, ['itemsAvailable', 'symbol', 'sellerFeeBasisPoints', 'maxEditionSupply', 'isMutable', 'creators', 'itemSettings'], onMissingInputError);
    data = CandyMachine.toCandyMachineData(dataToUpdate);
  }
  return TransactionBuilder.TransactionBuilder.make().add({
    instruction: mplCandyMachineCore.createUpdateInstruction({
      candyMachine: PublicKey.toPublicKey(params.candyMachine),
      authority: authority.publicKey
    }, {
      data
    }, candyMachineProgram.address),
    signers: [authority],
    key: params.updateDataInstructionKey ?? 'updateCandyMachineData'
  });
};
const updateCandyMachineCollectionBuilder = (metaplex, params, authority, payer, programs) => {
  if (!params.collection) {
    return TransactionBuilder.TransactionBuilder.make();
  }
  const currentCollectionAddress = params.collection.currentCollectionAddress ?? (CandyMachine.isCandyMachine(params.candyMachine) ? params.candyMachine.collectionMintAddress : null);
  if (!currentCollectionAddress) {
    throw onMissingInputError(['collection.currentCollectionAddress']);
  }

  // Programs.
  const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
  const candyMachineProgram = metaplex.programs().getCandyMachine(programs);

  // Addresses.
  const candyMachineAddress = PublicKey.toPublicKey(params.candyMachine);
  const collectionAddress = params.collection.address;
  const collectionUpdateAuthority = params.collection.updateAuthority;

  // PDAs.
  const authorityPda = metaplex.candyMachines().pdas().authority({
    candyMachine: candyMachineAddress,
    programs
  });
  const currentCollectionMetadata = metaplex.nfts().pdas().metadata({
    mint: currentCollectionAddress
  });
  const currentCollectionAuthorityRecord = metaplex.nfts().pdas().collectionAuthorityRecord({
    mint: currentCollectionAddress,
    collectionAuthority: authorityPda
  });
  const collectionMetadata = metaplex.nfts().pdas().metadata({
    mint: collectionAddress
  });
  const collectionMasterEdition = metaplex.nfts().pdas().masterEdition({
    mint: collectionAddress
  });
  const collectionAuthorityRecord = metaplex.nfts().pdas().collectionAuthorityRecord({
    mint: collectionAddress,
    collectionAuthority: authorityPda
  });
  return TransactionBuilder.TransactionBuilder.make().add({
    instruction: mplCandyMachineCore.createSetCollectionInstruction({
      candyMachine: candyMachineAddress,
      authority: authority.publicKey,
      authorityPda,
      payer: payer.publicKey,
      collectionMint: currentCollectionAddress,
      collectionMetadata: currentCollectionMetadata,
      collectionAuthorityRecord: currentCollectionAuthorityRecord,
      newCollectionUpdateAuthority: collectionUpdateAuthority.publicKey,
      newCollectionMetadata: collectionMetadata,
      newCollectionMint: collectionAddress,
      newCollectionMasterEdition: collectionMasterEdition,
      newCollectionAuthorityRecord: collectionAuthorityRecord,
      tokenMetadataProgram: tokenMetadataProgram.address
    }, candyMachineProgram.address),
    signers: [authority, payer, collectionUpdateAuthority],
    key: params.setCollectionInstructionKey ?? 'setCandyMachineCollection'
  });
};
const updateCandyGuardsBuilder = (metaplex, params, candyGuardAuthority, payer, programs) => {
  const guardsToUpdate = common.removeUndefinedAttributes({
    candyGuard: params.candyGuard,
    guards: params.guards,
    groups: params.groups
  });
  let args;
  if (Object.keys(guardsToUpdate).length === 0) {
    return TransactionBuilder.TransactionBuilder.make();
  }
  if (CandyMachine.isCandyMachine(params.candyMachine) && params.candyMachine.candyGuard) {
    args = {
      candyGuard: params.candyMachine.candyGuard.address,
      guards: params.candyMachine.candyGuard.guards,
      groups: params.candyMachine.candyGuard.groups,
      ...guardsToUpdate
    };
  } else {
    assert.assertObjectHasDefinedKeys(guardsToUpdate, ['candyGuard', 'guards', 'groups'], onMissingInputError);
    args = guardsToUpdate;
  }
  return metaplex.candyMachines().builders().updateCandyGuard({
    candyGuard: args.candyGuard,
    guards: args.guards,
    groups: args.groups,
    authority: candyGuardAuthority,
    updateInstructionKey: params.updateCandyGuardInstructionKey ?? 'updateCandyGuard'
  }, {
    payer,
    programs
  });
};
const updateCandyMachineMintAuthorityBuilder = (metaplex, params, authority, programs) => {
  if (!params.newMintAuthority) {
    return TransactionBuilder.TransactionBuilder.make();
  }
  const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
  return TransactionBuilder.TransactionBuilder.make().add({
    instruction: mplCandyMachineCore.createSetMintAuthorityInstruction({
      candyMachine: PublicKey.toPublicKey(params.candyMachine),
      authority: authority.publicKey,
      mintAuthority: params.newMintAuthority.publicKey
    }, candyMachineProgram.address),
    signers: [authority, params.newMintAuthority],
    key: params.setAuthorityInstructionKey ?? 'setCandyMachineAuthority'
  });
};
const updateCandyMachineAuthorityBuilder = (metaplex, params, authority, programs) => {
  if (!params.newAuthority) {
    return TransactionBuilder.TransactionBuilder.make();
  }
  const candyMachineProgram = metaplex.programs().getCandyMachine(programs);
  return TransactionBuilder.TransactionBuilder.make().add({
    instruction: mplCandyMachineCore.createSetAuthorityInstruction({
      candyMachine: PublicKey.toPublicKey(params.candyMachine),
      authority: authority.publicKey
    }, {
      newAuthority: params.newAuthority
    }, candyMachineProgram.address),
    signers: [authority],
    key: params.setAuthorityInstructionKey ?? 'setCandyMachineAuthority'
  });
};
const onMissingInputError = missingKeys => new SdkError.MissingInputDataError(missingKeys, {
  problem: 'When passing the Candy Machine as a `PublicKey` instead of a Candy Machine model ' + 'the SDK cannot rely on current data to fill the gaps within the provided input.',
  solutionSuffix: ' Alternatively, you can pass the Candy Machine model instead.'
});

exports.updateCandyMachineBuilder = updateCandyMachineBuilder;
exports.updateCandyMachineOperation = updateCandyMachineOperation;
exports.updateCandyMachineOperationHandler = updateCandyMachineOperationHandler;
//# sourceMappingURL=updateCandyMachine.cjs.map
