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 { CandyMachineBuildersClient } from './CandyMachineBuildersClient';
import { CandyMachineGuardsClient } from './CandyMachineGuardsClient';
import { CandyMachinePdasClient } from './CandyMachinePdasClient';
import { isCandyMachine } from './models';
import { callCandyGuardRouteOperation, createCandyGuardOperation, createCandyMachineOperation, deleteCandyGuardOperation, deleteCandyMachineOperation, findCandyGuardByAddressOperation, findCandyGuardsByAuthorityOperation, findCandyMachineByAddressOperation, insertCandyMachineItemsOperation, mintFromCandyMachineOperation, unwrapCandyGuardOperation, updateCandyGuardOperation, updateCandyMachineOperation, wrapCandyGuardOperation, } from './operations';
import { toPublicKey } from '../../types';
/**
 * This is a client for the Candy Machine V3 module.
 *
 * It enables us to interact with the Candy Machine V3 and Candy Guard programs
 * in order to create, update, delete and mint from Candy Machines as well as
 * registering your own custom Candy Guards.
 *
 * You may access this client via the `candyMachines()` method of your `Metaplex` instance.
 *
 * ```ts
 * const candyMachineClient = metaplex.candyMachines();
 * ```
 *
 * @example
 * You can create a new Candy Machine with minimum input like so.
 * By default, the current identity of the Metaplex instance will be
 * the authority of the Candy Machine and it will immediately create
 * a Candy Guard linked to the new Candy Machine.
 *
 * ```ts
 *  const { candyMachine } = await metaplex
 *    .candyMachines()
 *    .create({
 *      itemsAvailable: toBigNumber(5000),
 *      sellerFeeBasisPoints: 333, // 3.33%
 *      collection: {
 *        address: collectionNft.address,
 *        updateAuthority: collectionUpdateAuthority,
 *      },
 *    });
 * ```
 *
 * @see {@link CandyGuard} The `CandyGuard` model
 * @group Modules
 */
export class CandyMachineClient {
    constructor(metaplex) {
        this.metaplex = metaplex;
        this.guardsClient = new CandyMachineGuardsClient(metaplex);
    }
    /**
     * You may use the `guards()` client to access the default guards
     * available as well as register your own guards.
     *
     * ```ts
     * const guardsClient = metaplex.candyMachines().guards();
     * ```
     */
    guards() {
        return this.guardsClient;
    }
    /**
     * You may use the `builders()` client to access the
     * underlying Transaction Builders of this module.
     *
     * ```ts
     * const buildersClient = metaplex.candyMachines().builders();
     * ```
     */
    builders() {
        return new CandyMachineBuildersClient(this.metaplex);
    }
    /**
     * You may use the `pdas()` client to build PDAs related to this module.
     *
     * ```ts
     * const pdasClient = metaplex.candyMachines().pdas();
     * ```
     */
    pdas() {
        return new CandyMachinePdasClient(this.metaplex);
    }
    /** {@inheritDoc callCandyGuardRouteOperation} */
    callGuardRoute(input, options) {
        return this.metaplex
            .operations()
            .execute(callCandyGuardRouteOperation(input), options);
    }
    /** {@inheritDoc createCandyMachineOperation} */
    create(input, options) {
        return this.metaplex
            .operations()
            .execute(createCandyMachineOperation(input), options);
    }
    /** {@inheritDoc createCandyGuardOperation} */
    createCandyGuard(input, options) {
        return this.metaplex
            .operations()
            .execute(createCandyGuardOperation(input), options);
    }
    /** {@inheritDoc deleteCandyMachineOperation} */
    delete(input, options) {
        return this.metaplex
            .operations()
            .execute(deleteCandyMachineOperation(input), options);
    }
    /** {@inheritDoc deleteCandyGuardOperation} */
    deleteCandyGuard(input, options) {
        return this.metaplex
            .operations()
            .execute(deleteCandyGuardOperation(input), options);
    }
    /** {@inheritDoc findCandyGuardsByAuthorityOperation} */
    findAllCandyGuardsByAuthority(input, options) {
        return this.metaplex
            .operations()
            .execute(findCandyGuardsByAuthorityOperation(input), options);
    }
    /** {@inheritDoc findCandyMachineByAddressOperation} */
    findByAddress(input, options) {
        return this.metaplex
            .operations()
            .execute(findCandyMachineByAddressOperation(input), options);
    }
    /** {@inheritDoc findCandyGuardByAddressOperation} */
    findCandyGuardByAddress(input, options) {
        return this.metaplex
            .operations()
            .execute(findCandyGuardByAddressOperation(input), options);
    }
    /**
     * Helper method that fetches a Candy Guard via the base
     * address used to derived its PDA.
     *
     * ```ts
     * const candyGuard = await metaplex
     *   .candyMachines()
     *   .findCandyGuardByBaseAddress({ address: base });
     * ```
     */
    findCandyGuardByBaseAddress(input, options) {
        const address = this.pdas().candyGuard({ base: input.address });
        return this.findCandyGuardByAddress(Object.assign(Object.assign({}, input), { address }), options);
    }
    /** {@inheritDoc insertCandyMachineItemsOperation} */
    insertItems(input, options) {
        return this.metaplex
            .operations()
            .execute(insertCandyMachineItemsOperation(input), options);
    }
    /** {@inheritDoc mintFromCandyMachineOperation} */
    mint(input, options) {
        return this.metaplex
            .operations()
            .execute(mintFromCandyMachineOperation(input), options);
    }
    /**
     * Helper method that refetches a given Candy Machine or Candy Guard.
     *
     * ```ts
     * const candyMachine = await metaplex.candyMachines().refresh(candyMachine);
     * const candyGuard = await metaplex.candyMachines().refresh(candyGuard);
     * ```
     */
    refresh(model, options) {
        return __awaiter(this, void 0, void 0, function* () {
            const input = { address: toPublicKey(model) };
            const refreshedModel = isCandyMachine(model)
                ? yield this.findByAddress(input, options)
                : yield this.findCandyGuardByAddress(input, options);
            return refreshedModel;
        });
    }
    /** {@inheritDoc unwrapCandyGuardOperation} */
    unwrapCandyGuard(input, options) {
        return this.metaplex
            .operations()
            .execute(unwrapCandyGuardOperation(input), options);
    }
    /** {@inheritDoc updateCandyMachineOperation} */
    update(input, options) {
        return this.metaplex
            .operations()
            .execute(updateCandyMachineOperation(input), options);
    }
    /** {@inheritDoc updateCandyGuardOperation} */
    updateCandyGuard(input, options) {
        return this.metaplex
            .operations()
            .execute(updateCandyGuardOperation(input), options);
    }
    /** {@inheritDoc wrapCandyGuardOperation} */
    wrapCandyGuard(input, options) {
        return this.metaplex
            .operations()
            .execute(wrapCandyGuardOperation(input), options);
    }
}
//# sourceMappingURL=CandyMachineClient.js.map