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 { createRevokeUseAuthorityInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'RevokeNftUseAuthorityOperation';
/**
 * Revokes an existing use authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .revokeUseAuthority({ mintAddress, user };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const revokeNftUseAuthorityOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const revokeNftUseAuthorityOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return revokeNftUseAuthorityBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Revokes an existing use authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .revokeUseAuthority({ mintAddress, user });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const revokeNftUseAuthorityBuilder = (metaplex, params, options = {}) => {
    var _a, _b;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, user, owner = metaplex.identity() } = params;
    // Programs.
    const systemProgram = metaplex.programs().getSystem(programs);
    const tokenProgram = metaplex.programs().getToken(programs);
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    // PDAs.
    const metadata = metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs,
    });
    const useAuthorityRecord = metaplex.nfts().pdas().useAuthorityRecord({
        mint: mintAddress,
        useAuthority: user,
        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)
        // Revoke the use authority.
        .add({
        instruction: createRevokeUseAuthorityInstruction({
            useAuthorityRecord,
            owner: owner.publicKey,
            user,
            ownerTokenAccount: ownerTokenAddress,
            mint: mintAddress,
            metadata,
            tokenProgram: tokenProgram.address,
            systemProgram: systemProgram.address,
        }, tokenMetadataProgram.address),
        signers: [owner],
        key: (_b = params.instructionKey) !== null && _b !== void 0 ? _b : 'revokeUseAuthority',
    }));
};
//# sourceMappingURL=revokeNftUseAuthority.js.map