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 { createApproveUseAuthorityInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'ApproveNftUseAuthorityOperation';
/**
 * Approves a new use authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .approveUseAuthority({ mintAddress, user };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const approveNftUseAuthorityOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const approveNftUseAuthorityOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return approveNftUseAuthorityBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Approves a new use authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .approveUseAuthority({ mintAddress, user });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const approveNftUseAuthorityBuilder = (metaplex, params, options = {}) => {
    var _a, _b, _c;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, user, owner = metaplex.identity() } = params;
    const systemProgram = metaplex.programs().getSystem(programs);
    const tokenProgram = metaplex.programs().getToken(programs);
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const metadata = metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs,
    });
    const useAuthorityRecord = metaplex.nfts().pdas().useAuthorityRecord({
        mint: mintAddress,
        useAuthority: user,
        programs,
    });
    const programAsBurner = metaplex.nfts().pdas().burner({
        programs,
    });
    const ownerTokenAddress = (_a = params.ownerTokenAddress) !== null && _a !== void 0 ? _a : metaplex.tokens().pdas().associatedTokenAccount({
        mint: mintAddress,
        owner: owner.publicKey,
        programs,
    });
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Approve the use authority.
        .add({
        instruction: createApproveUseAuthorityInstruction({
            useAuthorityRecord,
            owner: owner.publicKey,
            payer: payer.publicKey,
            user,
            ownerTokenAccount: ownerTokenAddress,
            metadata,
            mint: mintAddress,
            burner: programAsBurner,
            tokenProgram: tokenProgram.address,
            systemProgram: systemProgram.address,
        }, {
            approveUseAuthorityArgs: {
                numberOfUses: (_b = params.numberOfUses) !== null && _b !== void 0 ? _b : 1,
            },
        }, tokenMetadataProgram.address),
        signers: [owner, payer],
        key: (_c = params.instructionKey) !== null && _c !== void 0 ? _c : 'approveUseAuthority',
    }));
};
//# sourceMappingURL=approveNftUseAuthority.js.map