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 { createWithdrawFromTreasuryInstruction, } from '@metaplex-foundation/mpl-auction-house';
import { useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'WithdrawFromTreasuryAccountOperation';
/**
 * Transfers funds from Auction House Treasury Wallet to the Treasury Withdrawal Destination Wallet set on an Auction House creation.
 * By default Treasury Withdrawal Destination Wallet is set to `metaplex.identity()`.
 *
 * ```ts
 * await metaplex
 *   .auctionHouse()
 *   .withdrawFromTreasuryAccount({ auctionHouse, amount };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const withdrawFromTreasuryAccountOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const withdrawFromTreasuryAccountOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return withdrawFromTreasuryAccountBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Transfers funds from Auction House Treasury Wallet to the Treasury Withdrawal Destination Wallet set on an Auction House creation.
 * By default Treasury Withdrawal Destination Wallet is set to `metaplex.identity()`.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .auctionHouse()
 *   .builders()
 *   .withdrawFromTreasuryAccount({ auctionHouse, amount });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const withdrawFromTreasuryAccountBuilder = (metaplex, params, options = {}) => {
    // Data.
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { auctionHouse, amount, instructionKey, authority = metaplex.identity(), } = params;
    // Accounts.
    const auctionHouseTreasury = metaplex.auctionHouse().pdas().treasury({
        auctionHouse: auctionHouse.address,
        programs,
    });
    const accounts = {
        treasuryMint: auctionHouse.treasuryMint.address,
        authority: auctionHouse.authorityAddress,
        treasuryWithdrawalDestination: auctionHouse.treasuryWithdrawalDestinationAddress,
        auctionHouseTreasury,
        auctionHouse: auctionHouse.address,
    };
    // Withdraw From Treasury Instruction.
    const withdrawFromTreasuryInstruction = createWithdrawFromTreasuryInstruction(accounts, { amount: amount.basisPoints });
    // Signers.
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Withdraw From Treasury.
        .add({
        instruction: withdrawFromTreasuryInstruction,
        signers: [authority],
        key: instructionKey !== null && instructionKey !== void 0 ? instructionKey : 'withdrawFromTreasuryAccount',
    }));
};
//# sourceMappingURL=withdrawFromTreasuryAccount.js.map