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 { parseCandyMachineV2CollectionAccount, toCandyMachineV2Account, } from '../accounts';
import { toCandyMachineV2 } from '../models';
import { findCandyMachineV2CollectionPda } from '../pdas';
import { assertAccountExists, useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'FindCandyMachineV2ByAddressOperation';
/**
 * Find an existing Candy Machine by its address.
 *
 * ```ts
 * const candyMachine = await metaplex.candyMachinesV2().findbyAddress({ address });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const findCandyMachineV2ByAddressOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const findCandyMachineV2ByAddressOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        const { commitment } = scope;
        const { address } = operation.input;
        const collectionPda = findCandyMachineV2CollectionPda(address);
        const accounts = yield metaplex
            .rpc()
            .getMultipleAccounts([address, collectionPda], commitment);
        const unparsedAccount = accounts[0];
        assertAccountExists(unparsedAccount);
        const account = toCandyMachineV2Account(unparsedAccount);
        const collectionAccount = parseCandyMachineV2CollectionAccount(accounts[1]);
        const mint = account.data.tokenMint
            ? yield metaplex
                .tokens()
                .findMintByAddress({ address: account.data.tokenMint }, scope)
            : null;
        return toCandyMachineV2(account, unparsedAccount, collectionAccount, mint);
    }),
};
//# sourceMappingURL=findCandyMachineV2ByAddress.js.map