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 { createBurnNftInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'DeleteNftOperation';
/**
 * Deletes an existing NFT.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .delete({ mintAddress };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const deleteNftOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const deleteNftOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return deleteNftBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Deletes an existing NFT.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .delete({ mintAddress });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const deleteNftBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, owner = metaplex.identity(), ownerTokenAccount, collection, } = params;
    const tokenProgram = metaplex.programs().getToken(programs);
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const metadata = metaplex.nfts().pdas().metadata({
        mint: mintAddress,
        programs,
    });
    const edition = metaplex.nfts().pdas().masterEdition({
        mint: mintAddress,
        programs,
    });
    const tokenAddress = ownerTokenAccount !== null && ownerTokenAccount !== void 0 ? ownerTokenAccount : metaplex.tokens().pdas().associatedTokenAccount({
        mint: mintAddress,
        owner: owner.publicKey,
        programs,
    });
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createBurnNftInstruction({
            metadata,
            owner: owner.publicKey,
            mint: mintAddress,
            tokenAccount: tokenAddress,
            masterEditionAccount: edition,
            splTokenProgram: tokenProgram.address,
            collectionMetadata: collection
                ? metaplex.nfts().pdas().metadata({ mint: collection, programs })
                : undefined,
        }, tokenMetadataProgram.address),
        signers: [owner],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'deleteNft',
    });
};
//# sourceMappingURL=deleteNft.js.map