'use strict';

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

var mplCandyMachine = require('@metaplex-solarti/mpl-candy-machine');
var helpers = require('../helpers.cjs');
var program = require('../program.cjs');
var assert = require('../../../utils/assert.cjs');
var Amount = require('../../../types/Amount.cjs');
var BigNumber = require('../../../types/BigNumber.cjs');
var common = require('../../../utils/common.cjs');
var DateTime = require('../../../types/DateTime.cjs');

// -----------------
// Program to Model
// -----------------

/** @group Model Helpers */
const isCandyMachineV2 = value => typeof value === 'object' && value.model === 'candyMachineV2';

/** @group Model Helpers */
function assertCandyMachineV2(value) {
  assert["default"](isCandyMachineV2(value), 'Expected CandyMachineV2 type');
}

/** @group Model Helpers */
const toCandyMachineV2 = (account, unparsedAccount, collectionAccount, mint) => {
  assert["default"](mint === null || account.data.tokenMint !== null && mint.address.equals(account.data.tokenMint));
  const itemsAvailable = BigNumber.toBigNumber(account.data.data.itemsAvailable);
  const itemsMinted = BigNumber.toBigNumber(account.data.itemsRedeemed);
  const {
    endSettings
  } = account.data.data;
  const {
    hiddenSettings
  } = account.data.data;
  const {
    whitelistMintSettings
  } = account.data.data;
  const {
    gatekeeper
  } = account.data.data;
  const rawData = unparsedAccount.data;
  const itemsLoaded = hiddenSettings ? BigNumber.toBigNumber(0) : helpers.countCandyMachineV2Items(rawData);
  const items = hiddenSettings ? [] : helpers.parseCandyMachineV2Items(rawData);
  return {
    model: 'candyMachineV2',
    address: account.publicKey,
    programAddress: account.owner,
    version: account.owner.equals(program.CandyMachineV2Program.publicKey) ? 2 : 1,
    authorityAddress: account.data.authority,
    walletAddress: account.data.wallet,
    tokenMintAddress: account.data.tokenMint,
    collectionMintAddress: collectionAccount && collectionAccount.exists ? collectionAccount.data.mint : null,
    uuid: account.data.data.uuid,
    price: Amount.amount(account.data.data.price, mint ? mint.currency : Amount.SOL),
    symbol: common.removeEmptyChars(account.data.data.symbol),
    sellerFeeBasisPoints: account.data.data.sellerFeeBasisPoints,
    isMutable: account.data.data.isMutable,
    retainAuthority: account.data.data.retainAuthority,
    goLiveDate: DateTime.toOptionDateTime(account.data.data.goLiveDate),
    maxEditionSupply: BigNumber.toBigNumber(account.data.data.maxSupply),
    items,
    itemsAvailable,
    itemsMinted,
    itemsRemaining: BigNumber.toBigNumber(itemsAvailable.sub(itemsMinted)),
    itemsLoaded,
    isFullyLoaded: itemsAvailable.lte(itemsLoaded),
    // eslint-disable-next-line no-nested-ternary
    endSettings: endSettings ? endSettings.endSettingType === mplCandyMachine.EndSettingType.Date ? {
      endSettingType: mplCandyMachine.EndSettingType.Date,
      date: DateTime.toDateTime(endSettings.number)
    } : {
      endSettingType: mplCandyMachine.EndSettingType.Amount,
      number: BigNumber.toBigNumber(endSettings.number)
    } : null,
    hiddenSettings,
    whitelistMintSettings: whitelistMintSettings ? {
      ...whitelistMintSettings,
      discountPrice: whitelistMintSettings.discountPrice ? Amount.lamports(whitelistMintSettings.discountPrice) : null
    } : null,
    gatekeeper: gatekeeper ? {
      ...gatekeeper,
      network: gatekeeper.gatekeeperNetwork
    } : null,
    creators: account.data.data.creators
  };
};

// -----------------
// Model to Configs
// -----------------

/**
 * This object provides a common interface for the configurations required
 * to create or update Candy Machines.
 *
 * @group Models
 */

/** @group Model Helpers */
const toCandyMachineV2Configs = candyMachine => {
  return {
    wallet: candyMachine.walletAddress,
    tokenMint: candyMachine.tokenMintAddress,
    ...candyMachine
  };
};

// -----------------
// Configs to Program
// -----------------

/** @group Models */

/** @group Model Helpers */
const toCandyMachineV2InstructionData = (address, configs) => {
  const {
    endSettings
  } = configs;
  const {
    whitelistMintSettings
  } = configs;
  const {
    gatekeeper
  } = configs;
  return {
    wallet: configs.wallet,
    tokenMint: configs.tokenMint,
    data: {
      ...configs,
      uuid: helpers.getCandyMachineV2UuidFromAddress(address),
      price: configs.price.basisPoints,
      maxSupply: configs.maxEditionSupply,
      endSettings: endSettings ? {
        ...endSettings,
        number: endSettings.endSettingType === mplCandyMachine.EndSettingType.Date ? endSettings.date : endSettings.number
      } : null,
      whitelistMintSettings: whitelistMintSettings ? {
        ...whitelistMintSettings,
        discountPrice: whitelistMintSettings.discountPrice?.basisPoints ?? null
      } : null,
      gatekeeper: gatekeeper ? {
        ...gatekeeper,
        gatekeeperNetwork: gatekeeper.network
      } : null
    }
  };
};

exports.assertCandyMachineV2 = assertCandyMachineV2;
exports.isCandyMachineV2 = isCandyMachineV2;
exports.toCandyMachineV2 = toCandyMachineV2;
exports.toCandyMachineV2Configs = toCandyMachineV2Configs;
exports.toCandyMachineV2InstructionData = toCandyMachineV2InstructionData;
//# sourceMappingURL=CandyMachineV2.cjs.map
