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 { createApproveInstruction } from '@solarti/spl-token';
import { isSigner, token, useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'ApproveTokenDelegateAuthorityOperation';
/**
 * Approves a delegate authority for a token account.
 *
 * ```ts
 * await metaplex
 *   .tokens()
 *   .approveDelegateAuthority({
 *     delegateAuthority,
 *     mintAddress,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const approveTokenDelegateAuthorityOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const approveTokenDelegateAuthorityOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return approveTokenDelegateAuthorityBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Approves a delegate authority for a token account.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .tokens()
 *   .builders()
 *   .approveDelegateAuthority({
 *     delegateAuthority,
 *     mintAddress,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const approveTokenDelegateAuthorityBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, delegateAuthority, amount = token(1), owner = metaplex.identity(), tokenAddress, multiSigners = [], } = params;
    const [ownerPublicKey, signers] = isSigner(owner)
        ? [owner.publicKey, [owner]]
        : [owner, multiSigners];
    const tokenProgram = metaplex.programs().getToken(programs);
    const tokenAddressOrAta = tokenAddress !== null && tokenAddress !== void 0 ? tokenAddress : metaplex.tokens().pdas().associatedTokenAccount({
        mint: mintAddress,
        owner: ownerPublicKey,
        programs,
    });
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createApproveInstruction(tokenAddressOrAta, delegateAuthority, ownerPublicKey, amount.basisPoints.toNumber(), multiSigners, tokenProgram.address),
        signers,
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'approveDelegateAuthority',
    });
};
//# sourceMappingURL=approveTokenDelegateAuthority.js.map