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 { createAuctioneerDepositInstruction, createDepositInstruction, } from '@metaplex-foundation/mpl-auction-house';
import { AuctioneerAuthorityRequiredError } from '../errors';
import { TransactionBuilder } from '../../../utils';
import { isSigner, toPublicKey, useOperation, } from '../../../types';
// -----------------
// Operation
// -----------------
const Key = 'DepositToBuyerAccountOperation';
/**
 * Adds funds to the user's buyer escrow account for the given auction house.
 *
 * ```ts
 * await metaplex
 *   .auctionHouse()
 *   .depositToBuyerAccount({ auctionHouse, buyer, amount };
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const depositToBuyerAccountOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const depositToBuyerAccountOperationHandler = {
    handle: (operation, metaplex, scope) => __awaiter(void 0, void 0, void 0, function* () {
        return depositToBuyerAccountBuilder(metaplex, operation.input, scope).sendAndConfirm(metaplex, scope.confirmOptions);
    }),
};
/**
 * Adds funds to the user's buyer escrow account for the given auction house.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .auctionHouse()
 *   .builders()
 *   .depositToBuyerAccount({ auctionHouse, buyer, amount });
 * ```
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const depositToBuyerAccountBuilder = (metaplex, params, options = {}) => {
    // Data.
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { auctionHouse, auctioneerAuthority, amount, instructionKey, buyer = metaplex.identity(), } = params;
    if (auctionHouse.hasAuctioneer && !auctioneerAuthority) {
        throw new AuctioneerAuthorityRequiredError();
    }
    // Accounts.
    const paymentAccount = auctionHouse.isNative
        ? toPublicKey(buyer)
        : metaplex
            .tokens()
            .pdas()
            .associatedTokenAccount({
            mint: auctionHouse.treasuryMint.address,
            owner: toPublicKey(buyer),
            programs,
        });
    const escrowPayment = metaplex
        .auctionHouse()
        .pdas()
        .buyerEscrow({
        auctionHouse: auctionHouse.address,
        buyer: toPublicKey(buyer),
        programs,
    });
    const accounts = {
        wallet: toPublicKey(buyer),
        paymentAccount,
        transferAuthority: toPublicKey(buyer),
        escrowPaymentAccount: escrowPayment,
        treasuryMint: auctionHouse.treasuryMint.address,
        authority: auctionHouse.authorityAddress,
        auctionHouse: auctionHouse.address,
        auctionHouseFeeAccount: auctionHouse.feeAccountAddress,
    };
    // Args.
    const args = {
        escrowPaymentBump: escrowPayment.bump,
        amount: amount.basisPoints,
    };
    // Deposit Instruction.
    let depositInstruction = createDepositInstruction(accounts, args);
    if (auctioneerAuthority) {
        const ahAuctioneerPda = metaplex.auctionHouse().pdas().auctioneer({
            auctionHouse: auctionHouse.address,
            auctioneerAuthority: auctioneerAuthority.publicKey,
            programs,
        });
        const accountsWithAuctioneer = Object.assign(Object.assign({}, accounts), { auctioneerAuthority: auctioneerAuthority.publicKey, ahAuctioneerPda });
        depositInstruction = createAuctioneerDepositInstruction(Object.assign({}, accountsWithAuctioneer), args);
    }
    // Signers.
    const depositSigners = [buyer, auctioneerAuthority].filter(isSigner);
    return (TransactionBuilder.make()
        .setFeePayer(payer)
        // Deposit.
        .add({
        instruction: depositInstruction,
        signers: depositSigners,
        key: instructionKey !== null && instructionKey !== void 0 ? instructionKey : 'depositToBuyerAccount',
    }));
};
//# sourceMappingURL=depositToBuyerAccount.js.map