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 { createThawDelegatedAccountInstruction } from '@metaplex-solarti/mpl-token-metadata';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'ThawDelegatedNftOperation';
/**
 * Thaws a NFT via its delegate authority.
 *
 * ```ts
 * await metaplex
 *   .nfts()
 *   .thawDelegatedNft({ mintAddress, delegateAuthority };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const thawDelegatedNftOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const thawDelegatedNftOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            return thawDelegatedNftBuilder(metaplex, operation.input).sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Thaws a NFT via its delegate authority.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .nfts()
 *   .builders()
 *   .thawDelegatedNft({ mintAddress, delegateAuthority });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const thawDelegatedNftBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { mintAddress, delegateAuthority, tokenOwner = metaplex.identity().publicKey, tokenAddress, } = params;
    // Programs.
    const tokenProgram = metaplex.programs().getToken(programs);
    const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
    const editionAddress = metaplex.nfts().pdas().masterEdition({
        mint: mintAddress,
        programs,
    });
    const tokenAddressOrAta = tokenAddress !== null && tokenAddress !== void 0 ? tokenAddress : metaplex.tokens().pdas().associatedTokenAccount({
        mint: mintAddress,
        owner: tokenOwner,
        programs,
    });
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: createThawDelegatedAccountInstruction({
            delegate: delegateAuthority.publicKey,
            tokenAccount: tokenAddressOrAta,
            edition: editionAddress,
            mint: mintAddress,
            tokenProgram: tokenProgram.address,
        }, tokenMetadataProgram.address),
        signers: [delegateAuthority],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'thawDelegatedNft',
    });
};
//# sourceMappingURL=thawDelegatedNft.js.map