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 { toMintAccount, toTokenAccount } from '../accounts';
import { toMint } from '../models/Mint';
import { toTokenWithMint } from '../models/Token';
import { useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'FindTokenWithMintByAddressOperation';
/**
 * Finds a token account and its associated mint account
 * by providing the token address.
 *
 * ```ts
 * const tokenWithMint = await metaplex.tokens().findTokenWithMintByAddress({ address });
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const findTokenWithMintByAddressOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const findTokenWithMintByAddressOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        const { commitment } = scope;
        const { address } = operation.input;
        const tokenAccount = toTokenAccount(yield metaplex.rpc().getAccount(address, commitment));
        const mintAccount = toMintAccount(yield metaplex.rpc().getAccount(tokenAccount.data.mint, commitment));
        return toTokenWithMint(tokenAccount, toMint(mintAccount));
    }),
};
//# sourceMappingURL=findTokenWithMintByAddress.js.map