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 { createRevokeCollectionAuthorityInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'RevokeNftCollectionAuthorityOperation';
/**
 * Revokes an existing collection authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .revokeCollectionAuthority({ mintAddress, collectionAuthority };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const revokeNftCollectionAuthorityOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const revokeNftCollectionAuthorityOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return revokeNftCollectionAuthorityBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Revokes an existing collection authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .revokeCollectionAuthority({ mintAddress, collectionAuthority });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const revokeNftCollectionAuthorityBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, collectionAuthority, revokeAuthority = metaplex.identity(), } = params;
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const metadata = metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs,
    });
    const collectionAuthorityRecord = metaplex
        .nfts()
        .pdas()
        .collectionAuthorityRecord({
        mint: mintAddress,
        collectionAuthority,
        programs,
    });
    const instruction = createRevokeCollectionAuthorityInstruction({
        collectionAuthorityRecord,
        delegateAuthority: collectionAuthority,
        revokeAuthority: revokeAuthority.publicKey,
        metadata,
        mint: mintAddress,
    }, tokenMetadataProgram.address);
    // Temporary fix. The Shank macro wrongfully ask for the delegateAuthority to be a signer.
    // https://github.com/metaplex-foundation/metaplex-program-library/pull/639
    instruction.keys[1].isSigner = false;
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Revoke the collection authority.
        .add({
        instruction,
        signers: [revokeAuthority],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'revokeCollectionAuthority',
    }));
};
//# sourceMappingURL=revokeNftCollectionAuthority.js.map