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 { createApproveCollectionAuthorityInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'ApproveNftCollectionAuthorityOperation';
/**
 * Approves a new collection authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .approveCollectionAuthority({
 *     mintAddress,
 *     collectionAuthority,
 *   };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const approveNftCollectionAuthorityOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const approveNftCollectionAuthorityOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return approveNftCollectionAuthorityBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Approves a new collection authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .approveCollectionAuthority({
 *     mintAddress,
 *     collectionAuthority,
 *   });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const approveNftCollectionAuthorityBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, collectionAuthority, updateAuthority = metaplex.identity(), } = params;
    // Programs.
    const systemProgram = metaplex.programs().getSystem(programs);
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    // PDAs.
    const metadata = metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs,
    });
    const collectionAuthorityRecord = metaplex
        .nfts()
        .pdas()
        .collectionAuthorityRecord({
        mint: mintAddress,
        collectionAuthority,
        programs,
    });
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Approve the collection authority.
        .add({
        instruction: createApproveCollectionAuthorityInstruction({
            collectionAuthorityRecord,
            newCollectionAuthority: collectionAuthority,
            updateAuthority: updateAuthority.publicKey,
            payer: payer.publicKey,
            metadata,
            mint: mintAddress,
            systemProgram: systemProgram.address,
        }, tokenMetadataProgram.address),
        signers: [payer, updateAuthority],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'approveCollectionAuthority',
    }));
};
//# sourceMappingURL=approveNftCollectionAuthority.js.map